When logging normally in Flask, like Flask logging example,
app.py
app = Flask(__name__)
app.logger.debug("test message")
You can easily do it like this.
However, fileConfig reads the settings from an external file of logging
fileConfig
import logging.config
logging.config.fileConfig("config.ini")
And dictConfig cannot be used
dictConfig
import logging.config
import yaml
logging.config.dictConfig(yaml.load(open("config.yaml").read()))
Naturally, it will be ʻAttribute Error`.
app.logger.fileConfig('./config.ini')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-70995f52c865> in <module>()
----> 1 app.logger.fileConfig('./config.ini')
AttributeError: 'DebugLogger' object has no attribute 'fileConfig'
Therefore, it feels useless unless you write it in code one by one with ʻapp.logger.addHandler`.
 just wraps logging, so if you call logging.config.fileConfig () before ʻapp.run (), the settings will be reflected.Basically, refer to Documentation.
How to write yaml should correspond to the keyword argument of the constructor of class (in this case logging.StreamHandler and logging.TimedRotatingFileHandler) used for handlers.
I prefer yaml to ini so I use yaml
config.yaml
version: 1
formatters:
  customFormatter:
    format: '[%(asctime)s]%(levelname)s - %(filename)s#%(funcName)s:%(lineno)d: %(message)s'
    datefmt: '%Y/%m/%d %H:%M:%S'
loggers:
  file:
    handlers: [fileRotatingHandler]
    level: DEBUG
    qualname: file
    propagate: no
  console:
    handlers: [consoleHandler]
    level: DEBUG
    qualname: console
    propagate: no
handlers:
  fileRotatingHandler:
    formatter: customFormatter
    class: logging.handlers.TimedRotatingFileHandler
    level: DEBUG
    filename: log/debug.log
    encoding: utf8
    when: 'D'
    interval: 1
    backupCount: 14
  consoleHandler:
    class: logging.StreamHandler
    level: DEBUG
    formatter: customFormatter
    stream: ext://sys.stdout
root:
  level: DEBUG
  handlers: [fileRotatingHandler,consoleHandler]
If you forget the last root, it will not work
I got stuck without noticing this
Recommended Posts