def set_style(style): if style == "keys": formatter.processor = processors.KeyValueRenderer() if style == "json": formatter.processor = processors.JSONRenderer() if style == "dev": formatter.processor = dev.ConsoleRenderer()
def logging_setup(app): # Add file handler file_handler = handlers.RotatingFileHandler( app.config['LOGGING_LOCATION'], maxBytes=4 * 1024 * 1024, # 4 MB backupCount=10 ) file_handler.setLevel(app.config['LOGGING_LEVEL']) file_handler_formatter = Formatter(app.config['LOGGING_FORMAT']) file_handler.setFormatter(file_handler_formatter) app.logger.addHandler(file_handler) # Configure structlog configure( context_class=wrap_dict(dict), logger_factory=lambda: app.logger, processors=[processors.JSONRenderer()] )
def setup_logger(level=INFO, style: str = "keys"): """setup logging""" configure( processors=[stdlib.add_log_level, stdlib.ProcessorFormatter.wrap_for_formatter], logger_factory=stdlib.LoggerFactory(), ) formatter = stdlib.ProcessorFormatter(processor=dev.ConsoleRenderer()) formatter.processor = processors.KeyValueRenderer() if style == "json": formatter.processor = processors.JSONRenderer() else: formatter.processor = processors.KeyValueRenderer() handler = StreamHandler() handler.setFormatter(formatter) root_logger = getLogger() root_logger.addHandler(handler) root_logger.setLevel(level)
def configure_structlog(log_level='WARNING', log_file='kiwicom_wrap.log'): lvl = getattr(logging, log_level.upper()) configure( processors=[ # _drop_debug_logs, stdlib.add_log_level, stdlib.PositionalArgumentsFormatter(), _unix_timestamper, structlog_pretty.NumericRounder(), processors.format_exc_info, processors.UnicodeDecoder(), processors.JSONRenderer(), ], logger_factory=stdlib.LoggerFactory(), wrapper_class=stdlib.BoundLogger, ) handler = logging.FileHandler(filename=log_file) logger = get_logger('kiwiwrap') logger.setLevel(lvl) logger.addHandler(handler) return logger
import os import structlog from structlog import processors from structlog.contextvars import merge_contextvars from structlog.stdlib import BoundLogger, LoggerFactory renderer_kv = processors.KeyValueRenderer( key_order=["timestamp", "severity", "message"], drop_missing=True) renderer_json = processors.JSONRenderer() logging_renderer = { "kv": renderer_kv, "json": renderer_json }.get(os.environ.get("LOGGING_RENDERER", "json"), renderer_json) structlog.configure( processors=[ merge_contextvars, structlog.processors.StackInfoRenderer(), structlog.dev.set_exc_info, structlog.processors.format_exc_info, structlog.processors.TimeStamper(), structlog.stdlib.PositionalArgumentsFormatter(), logging_renderer, ], wrapper_class=BoundLogger, context_class=dict, logger_factory=LoggerFactory(), cache_logger_on_first_use=True, )
"filters": { "required_prod_or_stage": { "()": "utils.structlog.base.RequireProdOrStage", }, "required_dev_or_test": { "()": "utils.structlog.base.RequiredDevOrTest", }, }, "formatters": { "plain_console": { "()": stdlib.ProcessorFormatter, "processor": dev.ConsoleRenderer(colors=True), }, "json": { "()": stdlib.ProcessorFormatter, "processor": processors.JSONRenderer(), }, }, "handlers": { "console_json": { "class": "logging.StreamHandler", "formatter": "json", "filters": ["required_prod_or_stage"], }, "console_text": { "class": "logging.StreamHandler", "formatter": "plain_console", "filters": ["required_dev_or_test"], }, }, "loggers": {
'"message":"",' '"message_json": %(message)s}') } }, 'handlers': { 'json': { 'class': 'logging.StreamHandler', 'formatter': 'json' } }, 'loggers': { 'app': { 'handlers': ['json'], 'propagate': False } } }) configure( context_class=threadlocal.wrap_dict(dict), logger_factory=stdlib.LoggerFactory(), wrapper_class=stdlib.BoundLogger, processors=[ stdlib.filter_by_level, stdlib.PositionalArgumentsFormatter(), processors.StackInfoRenderer(), processors.format_exc_info, processors.JSONRenderer()] )
def configure_logging(debug=False, syslog=False, silenced_loggers=None, level_overrides=None): if silenced_loggers is None: silenced_loggers = [] if level_overrides is None: level_overrides = {} level = 'DEBUG' if debug else 'INFO' renderers = [ dev.ConsoleRenderer(), ] if debug else [ logstash_processor, processors.JSONRenderer(separators=(',', ':')), add_syslog_program(syslog), ] structlog_processors = [ stdlib.filter_by_level, stdlib.add_logger_name, stdlib.add_log_level, fix_logger_name, format_request, ensure_event, stdlib.PositionalArgumentsFormatter(), processors.TimeStamper(fmt="ISO", key='@timestamp'), processors.StackInfoRenderer(), processors.format_exc_info, ] + renderers configure( processors=structlog_processors, context_class=dict, logger_factory=stdlib.LoggerFactory(), wrapper_class=stdlib.BoundLogger, cache_logger_on_first_use=True, ) structlog = {'handlers': ['raw'], 'level': level, 'propagate': False} null = {'handlers': ['null'], 'propagate': False} loggers = { l: root(level_overrides.get(l, level)) for l, _, _ in logging_tree.tree()[2] } loggers['feedhq'] = structlog for nulled_logger in silenced_loggers: loggers[nulled_logger] = null raw = { 'level': level, 'class': 'logging.handlers.SysLogHandler', 'address': '/dev/log', 'facility': 'local0', } if syslog else { 'level': level, 'class': 'logging.StreamHandler', } return { 'version': 1, 'level': level, 'handlers': { 'root': { 'level': level, '()': StructlogHandler, }, 'raw': raw, 'null': { 'class': 'logging.NullHandler', }, }, 'loggers': loggers, 'root': root(level), }