def setup_logging(default_path=None, default_level=logging.WARN, logfile=None, enable_file_logging=False, env_key='EXTRACT_MSG_LOG_CFG'): """ Setup logging configuration Args: default_path (str): Default path to use for the logging configuration file default_level (int): Default logging level env_key (str): Environment variable name to search for, for setting logfile path Returns: bool: True if the configuration file was found and applied, False otherwise """ shipped_config = getContFileDir(__file__) + '/logging-config/' if os.name == 'nt': null = 'NUL' shipped_config += 'logging-nt.json' elif os.name == 'posix': null = '/dev/null' shipped_config += 'logging-posix.json' # Find logging.json if not provided if not default_path: default_path = shipped_config paths = [ default_path, 'logging.json', '../logging.json', '../../logging.json', shipped_config, ] path = None for config_path in paths: if os.path.exists(config_path): path = config_path break value = os.getenv(env_key, None) if value and os.path.exists(value): path = value if path is None: print('Unable to find logging.json configuration file') print('Make sure a valid logging configuration file is referenced in the default_path' ' argument, is inside the extract_msg install location, or is available at one ' 'of the following file-paths:') print(str(paths[1:])) logging.basicConfig(level=default_level) logging.warning('The extract_msg logging configuration was not found - using a basic configuration.' 'Please check the extract_msg installation directory for "logging-{}.json".'.format(os.name)) return False with open(path, 'rt') as f: config = json.load(f) for x in config['handlers']: if 'filename' in config['handlers'][x]: if enable_file_logging: config['handlers'][x]['filename'] = tmp = os.path.expanduser( os.path.expandvars(logfile if logfile else config['handlers'][x]['filename'])) tmp = getContFileDir(tmp) if not os.path.exists(tmp): os.makedirs(tmp) else: config['handlers'][x]['filename'] = null try: logging.config.dictConfig(config) except ValueError as e: print('Failed to configure the logger. Did your installation get messed up?') print(e) logging.getLogger().setLevel(default_level) return True
def setup_logging(default_path=None, default_level=logging.WARN, logfile=None, enable_file_logging=False, env_key='EXTRACT_MSG_LOG_CFG'): """ Setup logging configuration Args: default_path (str): Default path to use for the logging configuration file default_level (int): Default logging level env_key (str): Environment variable name to search for, for setting logfile path Returns: bool: True if the configuration file was found and applied, False otherwise """ shipped_config = getContFileDir(__file__) + '/logging-config/' if os.name == 'nt': null = 'NUL' shipped_config += 'logging-nt.json' elif os.name == 'posix': null = '/dev/null' shipped_config += 'logging-posix.json' # Find logging.json if not provided if not default_path: default_path = shipped_config paths = [ default_path, 'logging.json', '../logging.json', '../../logging.json', shipped_config, ] path = None for config_path in paths: if os.path.exists(config_path): path = config_path break value = os.getenv(env_key, None) if value and os.path.exists(value): path = value if path is None: print('Unable to find logging.json configuration file') print('Make sure a valid logging configuration file is referenced in the default_path' ' argument, is inside the extract_msg install location, or is available at one ' 'of the following file-paths:') print(str(paths[1:])) logging.basicConfig(level=default_level) logging.warning('The extract_msg logging configuration was not found - using a basic configuration.' 'Please check the extract_msg installation directory for "logging-{}.json".'.format(os.name)) return False with open(path, 'rt') as f: config = json.load(f) for x in config['handlers']: if 'filename' in config['handlers'][x]: if enable_file_logging: config['handlers'][x]['filename'] = tmp = os.path.expanduser( os.path.expandvars(logfile if logfile else config['handlers'][x]['filename'])) tmp = getContFileDir(tmp) if not os.path.exists(tmp): os.makedirs(tmp) else: config['handlers'][x]['filename'] = null try: logging.config.dictConfig(config) except ValueError as e: print('Failed to configure the logger. Did your installation get messed up?') print(e) logging.getLogger().setLevel(default_level) return True