zoukankan      html  css  js  c++  java
  • yaml文件配置logger

    yaml

    今天用yaml文件写了一下logging的配置,文件如下:

    version: 1
    disable_existing_loggers: False
    formatters:
        simple:
            format: "%(asctime)s - %(filename)s - %(levelname)s - %(message)s"
    handlers:
        console:
            class: logging.StreamHandler
            level: ERROR
            formatter: simple
            stream: ext://sys.stdout
    
        info_file_handler:
            class: logging.handlers.RotatingFileHandler
            level: INFO
            formatter: simple
            filename: ./mylog/info.log
            maxBytes: 10485760
            backupCount: 20
            encoding: utf8
    
        error_file_handler:
            class: logging.handlers.RotatingFileHandler
            level: ERROR
            formatter: simple
            filename: errors.log
            maxBytes: 10485760
            backupCount: 20
            encoding: utf8
    
    loggers:
        my_module:
            level: ERROR
            handlers: [console]
            propagate: no
    
    root:
        level: INFO
        handlers: [console, info_file_handler]
    
    

    只是做个demo,所以写的比较粗略,然后中间就是包含了formatter,handlers,并没有使用filters,最终的效果是ERROR级别的信息将会在命令台进行打印并写入日志文件当中。

    try_log.py

    # -*- coding: utf-8 -*-
    
    
    import os
    import yaml
    import logging
    from logging import config as logger_config
    
    
    config_file = os.path.join(os.path.dirname(__file__), "config.yaml")
    
    if os.path.exists("mylog"):
        pass
    else:
        os.mkdir("mylog")
    
    with open(config_file, "rt") as stream:
        config = yaml.unsafe_load(stream.read())
    
    logger_config.dictConfig(config)
    logger = logging.getLogger()
    
    
    if __name__ == "__main__":
        logger.error("This is a test error message for my first logger.")
    

    运行这个命令就可以看到相应的结果了。

  • 相关阅读:
    UVA 11859
    [OpenGL]OpenGL坐标系和坐标变换
    树状数组
    编程算法
    乞讨 间隔[a,b]在见面p^k*q*^m(k>m)中数号码
    解析Android的 消息传递机制Handler
    Atitit.故障排除系列---php 计划网站数据库错误排除过程
    Remove Element
    [Angular Directive] Write a Structural Directive in Angular 2
    [Compose] 18. Maintaining structure whilst asyncing
  • 原文地址:https://www.cnblogs.com/zzy0306/p/10713926.html
Copyright © 2011-2022 走看看