flask日志配置

flask日志设置

 

在App/utils/ 新建loggings.py文件

from flask import request
import logging
import logging.handlers
import os


class RequestFormatter(logging.Formatter):
    """
    针对请求信息的日志格式
    """
    def format(self, record):
        record.url = request.url
        record.remote_addr = request.remote_addr
        return super().format(record)


def create_logger(app):
    """
    设置日志
    :param app: Flask app对象
    :return:
    """
    logging_file_dir = app.config['LOGGING_FILE_DIR']
    logging_file_max_bytes = app.config['LOGGING_FILE_MAX_BYTES']
    logging_file_backup = app.config['LOGGING_FILE_BACKUP']
    logging_level = app.config['LOGGING_LEVEL']

    flask_console_handler = logging.StreamHandler()
    flask_console_handler.setFormatter(logging.Formatter('%(levelname)s %(module)s %(lineno)d %(message)s'))

    request_formatter = RequestFormatter('%(levelname)s [%(asctime)s] %(remote_addr)s -> %(url)s '
                                         '%(module)s %(lineno)d: %(message)s')

    cida_formatter = RequestFormatter('%(levelname)s [%(asctime)s]  %(module)s %(lineno)d: %(message)s')

    flask_file_handler = logging.handlers.RotatingFileHandler(
        filename=os.path.join(logging_file_dir, 'flask.log'),
        maxBytes=logging_file_max_bytes,
        backupCount=logging_file_backup
    )
    flask_file_handler.setFormatter(request_formatter)

    log_flask_app = logging.getLogger('flask.app')
    log_flask_app.addHandler(flask_file_handler)
    log_flask_app.setLevel(logging_level)

    cida_file_handler = logging.handlers.RotatingFileHandler(
        filename=os.path.join(logging_file_dir, 'xxx_action.log'),
        maxBytes=logging_file_max_bytes,
        backupCount=logging_file_backup
    )

    cida_file_handler.setFormatter(cida_formatter)
    log_flask_cida = logging.getLogger('xxx.app')
    log_flask_cida.addHandler(cida_file_handler)
    log_flask_cida.setLevel(logging_level)

    if app.debug:
        log_flask_app.addHandler(flask_console_handler)

 

在App/__init__.py 的create_app 中新增配置

def create_app(env):
    # 创建Flask的app对象
    app = Flask(__name__)

    ... ...

    # 配置日志
    from .utils.loggings import create_logger
    create_logger(app)

 

在App/settings.py中新增相关配置

# 日志
    LOGGING_LEVEL = 'DEBUG'
    LOGGING_FILE_DIR = './logs'
    LOGGING_FILE_MAX_BYTES = 300 * 1024 * 1024
    LOGGING_FILE_BACKUP = 10

 

如何在flask中使用???

在view页面,导入,使用

from flask import Blueprint
import logging
logger = logging.getLogger('cida.app')

blue = Blueprint('blue', __name__)


def init_views(app):
    app.register_blueprint(blue)


@blue.route('/')
def index():
    logger.error('Error')
    logger.warning('Warning')
    logger.info("Info")
    return "Index"

 

日志如下:

 

posted @ 2021-02-17 09:59  Linuxbugs  阅读(575)  评论(0编辑  收藏  举报