From e0f1e1b5fde3f4716346378ff9f14c250f0c2bcf Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Mon, 16 Mar 2020 20:49:15 +0100 Subject: Trying to weed out dual logging. --- archinstall.py | 87 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 39 deletions(-) (limited to 'archinstall.py') diff --git a/archinstall.py b/archinstall.py index 559b81d0..9e2b30c1 100644 --- a/archinstall.py +++ b/archinstall.py @@ -26,45 +26,54 @@ worker_history = oDict() instructions = oDict() args = {} -import logging -from systemd.journal import JournalHandler - -# Custom adapter to pre-pend the 'origin' key. -# TODO: Should probably use filters: https://docs.python.org/3/howto/logging-cookbook.html#using-filters-to-impart-contextual-information -class CustomAdapter(logging.LoggerAdapter): - def process(self, msg, kwargs): - return '[{}] {}'.format(self.extra['origin'], msg), kwargs - -logger = logging.getLogger() # __name__ -journald_handler = JournalHandler() -journald_handler.setFormatter(logging.Formatter('[{levelname}] {message}', style='{')) -logger.addHandler(journald_handler) -logger.setLevel(logging.DEBUG) - -class LOG_LEVELS: - CRITICAL = 1 - ERROR = 2 - WARNING = 3 - INFO = 4 - DEBUG = 5 - -LOG_LEVEL = 4 - -def log(*msg, origin='UNKNOWN', level=5, **kwargs): - if level <= LOG_LEVEL: - msg = [item.decode('UTF-8', errors='backslashreplace') if type(item) == bytes else item for item in msg] - msg = [str(item) if type(item) != str else item for item in msg] - log_adapter = CustomAdapter(logger, {'origin': origin}) - if level <= 1: - log_adapter.critical(' '.join(msg)) - elif level <= 2: - log_adapter.error(' '.join(msg)) - elif level <= 3: - log_adapter.warning(' '.join(msg)) - elif level <= 4: - log_adapter.info(' '.join(msg)) - else: - log_adapter.debug(' '.join(msg)) +create_log = True + +try: + if 'log' in __builtins__.__dict__: + create_log = False +except: + if 'log' in __builtins__: + create_log = False + +if create_log: + import logging + from systemd.journal import JournalHandler + + # Custom adapter to pre-pend the 'origin' key. + # TODO: Should probably use filters: https://docs.python.org/3/howto/logging-cookbook.html#using-filters-to-impart-contextual-information + class CustomAdapter(logging.LoggerAdapter): + def process(self, msg, kwargs): + return '[{}] {}'.format(self.extra['origin'], msg), kwargs + + logger = logging.getLogger() # __name__ + journald_handler = JournalHandler() + journald_handler.setFormatter(logging.Formatter('[{levelname}] {message}', style='{')) + logger.addHandler(journald_handler) + logger.setLevel(logging.DEBUG) + + class LOG_LEVELS: + CRITICAL = 1 + ERROR = 2 + WARNING = 3 + INFO = 4 + DEBUG = 5 + + LOG_LEVEL = 4 + def log(*msg, origin='UNKNOWN', level=5, **kwargs): + if level <= LOG_LEVEL: + msg = [item.decode('UTF-8', errors='backslashreplace') if type(item) == bytes else item for item in msg] + msg = [str(item) if type(item) != str else item for item in msg] + log_adapter = CustomAdapter(logger, {'origin': origin}) + if level <= 1: + log_adapter.critical(' '.join(msg)) + elif level <= 2: + log_adapter.error(' '.join(msg)) + elif level <= 3: + log_adapter.warning(' '.join(msg)) + elif level <= 4: + log_adapter.info(' '.join(msg)) + else: + log_adapter.debug(' '.join(msg)) ## == Profiles Path can be set via --profiles-path=/path ## This just sets the default path if the parameter is omitted. -- cgit v1.2.3-70-g09d2