zoukankan      html  css  js  c++  java
  • python log的处理方式

    python log的处理方式

    配置文件

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    # __author__ = "Q1mi"
    
    """
    logging配置
    """
    
    import os
    import logging.config
    
    # 定义三种日志输出格式 开始
    
    standard_format = '[%(asctime) -s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' 
                      '[%(levelname)s][%(message)s]'
    
    simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
    
    id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
    
    # 定义日志输出格式 结束
    
    logfile_dir = os.path.dirname(os.path.abspath(__file__))  # log文件的目录
    
    logfile_name = 'all2.log'  # log文件名
    
    # 如果不存在定义的日志目录就创建一个
    if not os.path.isdir(logfile_dir):
        os.mkdir(logfile_dir)
    
    # log文件的全路径
    logfile_path = os.path.join(logfile_dir, logfile_name)
    
    # log配置字典
    LOGGING_DIC = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': standard_format,
                'datefmt': '%Y-%m-%d %H:%M:%S',
            },
            'simple': {
                'format': simple_format
            },
        },
        'filters': {},
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',  # 打印到屏幕
                'formatter': 'simple'
            },
            'default': {
                'level': 'DEBUG',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
                'filename': logfile_path,  # 日志文件
                'maxBytes': 1024*1024*5,  # 日志大小 5M
                'backupCount': 5,
                'formatter': 'standard',
                'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
            },
        },
        'loggers': {
            '': {
                'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
                'level': 'DEBUG',
                'propagate': True,  # 向上(更高level的logger)传递
            },
        },
    }
    logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的配置
    logger = logging.getLogger(__name__)  # 生成一个log实例
    logger.info('It works!')  # 记录该文件的运行状态

    调用测试文件

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    # __author__ = "Q1mi"
    # Email: master@liwenzhou.com
    
    """
    MyLogging Test
    """
    
    import time
    import logging
    from log_demo import my_logging  # 导入自定义的logging配置
    
    logger = logging.getLogger(__file__)  # 生成logger实例
    
    
    def demo():
        logger.debug("start range... time:{}".format(time.time()))
        logger.info("中文测试开始。。。")
        for i in range(10):
            logger.debug("i:{}".format(i))
            time.sleep(2)
        else:
            logger.debug("over range... time:{}".format(time.time()))
        logger.info("中文测试结束。。。")
    
    if __name__ == "__main__":
        demo()
  • 相关阅读:
    direct path write 等待事件导致数据库hang
    Sql Server数据库视图的创建、修改
    MVC视图中Html.DropDownList()辅助方法的使用
    Ubuntu16.04下安装.NET Core
    Ubuntu16.04下部署golang开发环境
    win7环境下安装运行gotour【转载整理】
    一.Windows I/O模型之选择(select)模型
    Windos下的6种IO模型简要介绍
    编码介绍(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
    串口通信知识点详解
  • 原文地址:https://www.cnblogs.com/aslongas/p/6984308.html
Copyright © 2011-2022 走看看