ok

Mini Shell

Direktori : /opt/imunify360/venv/lib64/python3.11/site-packages/restore_infected/
Upload File :
Current File : //opt/imunify360/venv/lib64/python3.11/site-packages/restore_infected/log.py

import logging.config
import os
from logging.handlers import WatchedFileHandler

LOG_FILE = '/var/log/cloudlinux-backup-utils.log'

DEBUG = 3
VERBOSE = 2
INFO = 1
CONCISE = 0

_format_concise = logging.Formatter('%(message)s')
_format_verbose = logging.Formatter(
    '%(levelname)-7s [%(asctime)s] %(name)s: %(message)s'
)

_config = {
    CONCISE: [_format_concise, logging.WARNING],
    INFO: [_format_concise, logging.INFO],
    VERBOSE: [_format_verbose, logging.INFO],
    DEBUG: [_format_verbose, logging.DEBUG],
}

MAIN_LOGGER = 'restore_infected'
CLI_LOGGER = 'restore_infected.cli'

FILE_HANDLER = 'file'
CONSOLE_HANDLER = 'console'
HANDLERS = {}

stream_handler = logging.StreamHandler()

ALL_LOGGERS = [
    MAIN_LOGGER,
    CLI_LOGGER,
    'AcronisClientInstaller',
    'requests',
]


def clear_log_handlers():
    for logger_name in ALL_LOGGERS:
        logger = logging.getLogger(logger_name)
        for handler in logger.handlers:
            logger.removeHandler(handler)


def _add_log_handler(handler):
    for logger_name in ALL_LOGGERS:
        logger = logging.getLogger(logger_name)
        logger.addHandler(handler)


def _remove_log_handler(handler):
    for logger_name in ALL_LOGGERS:
        logger = logging.getLogger(logger_name)
        logger.removeHandler(handler)


def log_only_cli_module():
    clear_log_handlers()

    logger = logging.getLogger(CLI_LOGGER)
    logger.setLevel(logging.DEBUG)
    stream_handler.setLevel(logging.INFO)
    stream_handler.setFormatter(_format_concise)
    logger.addHandler(stream_handler)


def log_to_file(output=LOG_FILE):
    _remove_log_handler(HANDLERS.get(FILE_HANDLER, None))
    if output:
        if os.access(os.path.dirname(output), os.W_OK | os.X_OK):
            file_handler = WatchedFileHandler(output, encoding='utf8')
        else:
            file_handler = logging.NullHandler()

        set_config(file_handler, DEBUG)
        HANDLERS[FILE_HANDLER] = file_handler
        _add_log_handler(file_handler)


def log_to_console(verbosity):
    _remove_log_handler(stream_handler)

    if verbosity > CONCISE:
        set_config(stream_handler, verbosity)
        HANDLERS[CONSOLE_HANDLER] = stream_handler
        _add_log_handler(stream_handler)


def set_config(handler, verbosity):
    fmt, lvl = _config.get(verbosity, _config[CONCISE])
    handler.setFormatter(fmt)
    handler.setLevel(lvl)

Zerion Mini Shell 1.0