def configure_logging(log_to_console=False): logging_config = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s', }, }, 'handlers': { 'console': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'formatter': 'standard', }, }, 'loggers': { '': { 'handlers': [], 'level': 'DEBUG', 'propagate': True, }, }, } if log_to_console: logging_config['loggers']['']['handlers'].append('console') dictConfig(logging_config)
def _configure_logging(application, verbosity=0, syslog=False): """Configure logging for the application, setting the appropriate verbosity and adding syslog if it's enabled. :param str application: The application module/package name :param int verbosity: 1 == INFO, 2 == DEBUG :param bool syslog: Enable the syslog handler """ # Create a new copy of the logging config that will be modified config = dict(LOGGING) # Increase the logging verbosity if verbosity == 1: config['loggers']['sprockets']['level'] = logging.INFO elif verbosity == 2: config['loggers']['sprockets']['level'] = logging.DEBUG # Add syslog if it's enabled if syslog: config['loggers']['sprockets']['handlers'].append('syslog') # Copy the sprockets logger to the application config['loggers'][application] = dict(config['loggers']['sprockets']) # Configure logging logging_config.dictConfig(config)
def getTableLogger(table_name): LOG_CONFIG['handlers']['{}_file'.format(table_name)] = { 'level': 'DEBUG', 'class': 'logging.handlers.TimedRotatingFileHandler', 'formatter': 'standard', 'filename': os.path.expanduser('./logs/{}-table.log'.format(table_name)), 'when': 'midnight', 'backupCount': 5 } if config_handler.get_global_option('dry_run'): LOG_CONFIG['handlers']['{}_file'.format( table_name)]['formatter'] = 'dry-run' if config_handler.get_logging_option('log_level'): level = config_handler.get_logging_option('log_level').upper() else: level = 'DEBUG' LOG_CONFIG['loggers']['{}_table'.format(table_name)] = { 'handlers': ['{}_file'.format(table_name)], 'level': level, 'propagate': False } try: dictconfig.dictConfig(LOG_CONFIG) except ValueError as error: print('Error configuring logger: {0}'.format(error)) sys.exit(1) except: raise return logging.getLogger('{}_table'.format(table_name))
def _configure_loggers(self, config): loggers_config = config.logging.loggers logfile = config.logging.filename logger_dict = copy.deepcopy(LOGGER_CONFIG_TEMPLATE) if logfile: # set filename on file handler logger_dict['handlers']['file']['filename'] = logfile logfile_dir = os.path.dirname(logfile) if not os.path.exists(logfile_dir): os.makedirs(logfile_dir) self._log_file = logfile else: del logger_dict['handlers']['file'] # add handlers to all loggers loggers = {} for logger_name in loggers_config: loggers[logger_name] = dict(handlers=list(logger_dict['handlers'].keys())) self._all_loggers_names.append(logger_name) logger_dict['loggers'] = loggers # set level for all loggers for logger_name, logging_level in loggers_config.iteritems(): log = logging.getLogger(logger_name) level = logging._levelNames[logging_level.upper()] log.setLevel(level) dictconfig.dictConfig(logger_dict)
def glances_logger(): """Build and return the logger.""" temp_path = tempfile_name() _logger = logging.getLogger() LOGGING_CFG['handlers']['file']['filename'] = temp_path dictConfig(LOGGING_CFG) return _logger
def configure(self): """Configure the Python stdlib logger""" if self.debug is not None and not self.debug: self._remove_debug_handlers() self._remove_debug_only() logging_config.dictConfig(self.config) try: logging.captureWarnings(True) except AttributeError: pass
def configure(extra_config=None): """ Configures logging for the application based on the config. :param dict extra_config: options to update the default config with. """ composite_config = {} composite_config.update(config) if extra_config: composite_config.update(extra_config) dictConfig(composite_config)
def setUp(self): dictconfig.dictConfig(cfg) self.log = logging.getLogger('test.logging')
def apply_config(self, conf): dictConfig(conf)
log_file = os.path.expanduser( config_handler.get_logging_option('log_file')) LOG_CONFIG['handlers']['file'] = { 'level': 'DEBUG', 'class': 'logging.handlers.TimedRotatingFileHandler', 'formatter': 'standard', 'filename': log_file, 'when': 'midnight', 'backupCount': 5 } LOG_CONFIG['loggers']['']['handlers'].append('file') LOG_CONFIG['loggers']['dynamic-dynamodb']['handlers'].append('file') # Configure a custom log level if config_handler.get_logging_option('log_level'): LOG_CONFIG['handlers']['console']['level'] = \ config_handler.get_logging_option('log_level').upper() if 'file' in LOG_CONFIG['handlers']: LOG_CONFIG['handlers']['file']['level'] = \ config_handler.get_logging_option('log_level').upper() # Add dry-run to the formatter if in dry-run mode if config_handler.get_global_option('dry_run'): LOG_CONFIG['handlers']['console']['formatter'] = 'dry-run' if 'file' in LOG_CONFIG['handlers']: LOG_CONFIG['handlers']['file']['formatter'] = 'dry-run' dictconfig.dictConfig(LOG_CONFIG) LOGGER = logging.getLogger('dynamic-dynamodb')
def init_logging(log_dir): """ initial logging """ LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'standard': { 'format': '%(asctime)s - %(filename)s:%(lineno)d(%(module)s:%(funcName)s) - %(levelname)s - %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S' }, 'simple': { 'format': '%(asctime)s - %(levelname)s - %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S' }, }, 'filters': {}, 'handlers': { 'null': { 'level': 'DEBUG', 'class': 'logging.NullHandler', }, 'console': { 'class': 'logging.StreamHandler', 'level': 'DEBUG', 'formatter': 'standard', 'stream': 'ext://sys.stderr', }, 'syslog': { 'level': 'DEBUG', 'class': 'logging.handlers.SysLogHandler', 'facility': 'logging.handlers.SysLogHandler.LOG_LOCAL7', 'formatter': 'standard', }, # 'syslog2': { # 'level': 'DEBUG', # 'class': 'logging.handlers.SysLogHandler', # 'facility': 'logging.handlers.SysLogHandler.LOG_LOCAL7', # 'formatter': 'standard', # }, 'access': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.path.join(log_dir, 'access.log'), 'maxBytes': 1024 * 1024 * 2, 'backupCount': 5, 'formatter': 'standard', }, 'application': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.path.join(log_dir, 'app.log'), 'maxBytes': 1024 * 1024 * 2, 'backupCount': 5, 'formatter': 'standard', }, }, 'loggers': { 'werkzeug': { 'handlers': ['access', 'console'], 'level': 'DEBUG', 'propagate': False, }, 'myapp': { 'handlers': ['application'], 'level': 'DEBUG', 'propagate': True, }, }, } dictConfig(LOGGING)