2021-11-18 19:49:32 +00:00
|
|
|
# https://www.structlog.org/en/stable/standard-library.html
|
|
|
|
def setup_logging(filename):
|
2022-03-04 15:50:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
2022-05-09 00:41:49 +00:00
|
|
|
filename:
|
2022-03-04 15:50:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
2021-11-18 19:49:32 +00:00
|
|
|
import logging.config
|
|
|
|
import structlog
|
|
|
|
|
|
|
|
timestamper = structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S")
|
|
|
|
pre_chain = [
|
|
|
|
# Add the log level and a timestamp to the event_dict if the log entry
|
|
|
|
# is not from structlog.
|
|
|
|
structlog.stdlib.add_log_level,
|
|
|
|
timestamper,
|
|
|
|
]
|
|
|
|
|
2022-04-11 09:10:32 +00:00
|
|
|
logging.config.dictConfig({
|
2021-11-18 19:49:32 +00:00
|
|
|
"version": 1,
|
|
|
|
"disable_existing_loggers": False,
|
|
|
|
"formatters": {
|
|
|
|
"plain": {
|
|
|
|
"()": structlog.stdlib.ProcessorFormatter,
|
|
|
|
"processor": structlog.dev.ConsoleRenderer(colors=False),
|
|
|
|
"foreign_pre_chain": pre_chain,
|
|
|
|
},
|
|
|
|
"colored": {
|
|
|
|
"()": structlog.stdlib.ProcessorFormatter,
|
|
|
|
"processor": structlog.dev.ConsoleRenderer(colors=True),
|
|
|
|
"foreign_pre_chain": pre_chain,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"handlers": {
|
|
|
|
"default": {
|
|
|
|
"level": "DEBUG",
|
|
|
|
"class": "logging.StreamHandler",
|
|
|
|
"formatter": "colored",
|
|
|
|
},
|
|
|
|
"file": {
|
|
|
|
"level": "DEBUG",
|
|
|
|
"class": "logging.handlers.WatchedFileHandler",
|
2022-04-11 09:10:32 +00:00
|
|
|
"filename": filename + '.log',
|
2021-11-18 19:49:32 +00:00
|
|
|
"formatter": "plain",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"loggers": {
|
|
|
|
"": {
|
|
|
|
"handlers": ["default", "file"],
|
|
|
|
"level": "DEBUG",
|
|
|
|
"propagate": True,
|
|
|
|
},
|
2022-04-11 09:10:32 +00:00
|
|
|
}
|
|
|
|
})
|
2021-11-18 19:49:32 +00:00
|
|
|
structlog.configure(
|
|
|
|
processors=[
|
|
|
|
structlog.stdlib.add_log_level,
|
|
|
|
structlog.stdlib.PositionalArgumentsFormatter(),
|
|
|
|
timestamper,
|
|
|
|
structlog.processors.StackInfoRenderer(),
|
|
|
|
structlog.processors.format_exc_info,
|
|
|
|
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
|
|
|
|
],
|
|
|
|
logger_factory=structlog.stdlib.LoggerFactory(),
|
|
|
|
wrapper_class=structlog.stdlib.BoundLogger,
|
|
|
|
cache_logger_on_first_use=True,
|
|
|
|
)
|