Read logging settings from an external file in Flask

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`.

However, ʻapp.logger just wraps logging, so if you call logging.config.fileConfig () before ʻapp.run (), the settings will be reflected.

setting file

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.

Personal template

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

Read logging settings from an external file in Flask
[Django] How to read variables / constants defined in an external file
Check if the configuration file is read in an easy-to-understand manner
Read a file in Python with a relative path from the program
Configuration file best practices in Flask
How to read environment variables from .env file in PyCharm (on Mac)
[Note] Read a file from another directory
Settings when adding an HDD in Linux
Read the file line by line in Python
Read the file line by line in Python
Automatically map controllers from URLs in Flask
[Python] Read the specified line in the file
Django template reads Makdown and reStructuredText written in an external file as HTML
Read file
Read a file containing garbled lines in Python
Create an executable file in a scripting language
From file to graph drawing in Python. Elementary elementary
Logging settings for daily log rotation in python
Read line by line from a file with Python
How to read a file in a different directory
Recommended books read in 2 years from new graduates
View images in OpenCV from Python using an external USB camera on your MacBook