Python logging - zhongjiajie/zhongjiajie.github.com GitHub Wiki

Python - logging Moudle

The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.

  • Loggers: expose the interface that application code directly uses.
  • Handlers: send the log records (created by loggers) to the appropriate destination.
  • Filters: provide a finer grained facility for determining which log records to output.
  • Formatters: specify the layout of log records in the final output.

Log event information is passed between loggers, handlers, filters and formatters in a LogRecord instance.

logging flow

logging-flow

config logger

Programmers can configure logging in three ways:

  • Creating loggers, handlers, and formatters explicitly using Python code that calls the configuration methods listed above.
  • Creating a logging config file and reading it using the fileConfig() function.
  • Creating a dictionary of configuration information and passing it to the dictConfig() function.logging-config.dictConfig

dictConfig例子

官网的dictConfig有点抽象,这里有一个可以直接复制粘贴的dictConfig的例子

FAQ

log('%s', var) 和 log('{}'.format(var)) 区别

The logger does know str.format() but it's best practice in Python to use this form with logging: logger.info("Hello %s", "world") since that creates a string only when it actually gets logged. str.format() and f-strings create strings before they get passed to the logger, even if the string is never logged. So it's considered inefficient(低效) and not advised(建议).

log.exception是什么级别的日志

参考,专门用于expection-haduler,日志等级是error,但是参数的格式是debug


⚠️ **GitHub.com Fallback** ⚠️