The use of flask's own logger and celery's own logger

There are built-in logger usage methods in both celery and flask frameworks. The related usage is recorded below.

Using logger in flask

The app object FLASK() in flask has its own logger method, which is called as follows:

from flask import current_app

current_app.logger.error('this is a error')
current_app.logger.info('this is a info')
current_app.logger.warning('this is a wraning')
current_app.logger.debug('this is a debug')

The built-in logger is actually obtained by creating a logger object through python's logging module. The source code is:

# logging.py
from logging import getLogger, getLoggerClass
def create_logger(app):
    ...
    # 创建一个调试模式下的日志处理器,级别为debug
    debug_handler = DebugHandler()
    debug_handler.setLevel(DEBUG)
    debug_handler.setFormatter(Formatter(DEBUG_LOG_FORMAT))
    # 创建一个运行过程的日志处理器,级别为error
    prod_handler = ProductionHandler(_proxy_stream)
    prod_handler.setLevel(ERROR)
    prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT))
    # 获取应用的名字,即app = Flask(app.name)传入的参数名,然后创建一个logger对象
    logger = getLogger(app.logger_name)
    # 先清空以前所有的处理器
    del logger.handlers[:]
    logger.__class__ = DebugLogger
    # 加入新的处理器
    logger.addHandler(debug_handler)
    logger.addHandler(prod_handler)

    # 默认情况下不继承
    logger.propagate = False

    return logger

Description: When the program calls current_app.logger, it will get the logger object returned by the create_logger function, using the ProductionHandler processor, and the output format of the log is:

PROD_LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'

But many times we are not satisfied with the built-in log output format, and if we want to customize it, we need to load our own log configuration file.

Load a custom configuration file

  • A logger object named app.logger_name needs to be configured in the configuration file; for example: app.logger_name='app'
[loggers]
keys=root,app

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=consoleHandler

[logger_app]
level=INFO
handlers=consoleHandler
qualname=app
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s [%(pathname)s:%(lineno)d]: %(message)s
  • load file
# app.py

app = Flask(__name__)

# 在载入配置文件之前必须先调用一次app.logger得到一个logger对象,然后才能载入配置文件,否则无效
app.logger.info('this is a info')
logging.config.fileConfig(Config.FILEPATH)

Note: Before loading the configuration file, you must call app.logger once to get a logger object, and then load the configuration file, otherwise it will be invalid.

Using logger in celery

Celery also has its own logger, how to use it:

from celery.utils.log import get_task_logger
# 创建一个logger对象
logger = get_task_logger('name')

Celery's logger still calls the logging module's logger.

# 源码
# get_task_logger函数调用了get_logger函数
# 传入一个字符串获取一个logger对象
def get_logger(logger):
    """Get logger by name."""
    # 判断该参数是不是字符串,是就获取一个logger对象
    if isinstance(logger, string_t):
        logger = logging.getLogger(logger)
    # 没有处理器就添加NullHandler处理器
    if not logger.handlers:
        logger.addHandler(logging.NullHandler())
    return logger
  • Its related configuration can be set in celery's configuration file;
# 在4.0版本后改成了小写,但是原来的还没有弃用
CELERYD_HIJACK_ROOT_LOGGER :默认true,先前所有的logger的配置都会失效,可以通过设置false禁用定制自己的日志处理程序;
CELERYD_LOG_COLOR :是否开启不同级别的颜色标记,默认开启;
CELERYD_LOG_FORMAT :设置celery全局的日志格式;默认格式:"[%(asctime)s: %(levelname)s/%(processName)s] %(message)s"
CELERYD_TASK_LOG_FORMAT:设置任务日志格式,默认:"[%(asctime)s: %(levelname)s/%(processName)s [%(task_name)s(%(task_id)s)] %(message)s"
CELERY_REDIRECT_STDOUTS:设置标准输入输出重定向到当前的处理器,默认为 true
CELERY_REDIRECT_STDOUTS_LEVEL:设定标准输入输出重定向到当前的处理器日志的输出级别;即指定使用print()输出的是什么级别的日志记录;默认wraning;

Notice:

  1. Since the operation of celery is independent, the configuration of the logger object defined in flask is invalid in the celery program, and the logger must be created using get_task_logger;

  2. Specify the output level of the celery log, specified by the --loglevel parameter at startup;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854414&siteId=291194637