zoukankan      html  css  js  c++  java
  • Python__logging模块

    日志模块:

    用于便捷记录日志且线程安全的模块

    CRITICAL = 50

    FATAL = CRITICAL

    ERROR = 40

    WARNING = 30

    WARN = WARNING

    INFO =20

    DEBUG = 10

    NOTSET = 0

    设置为debug

     1 #Author wangmengzhu
     2 import logging
     3 # logging.debug('debug')
     4 # logging.info('info')
     5 # logging.warning('warning')
     6 # logging.error('error')
     7 # logging.critical('critical')
     8 
     9 # logging.basicConfig(filename = 'access.log',format = '%(asctime)s-%(name)s-%(levelname)s-%(module)s:%(message)s',
    10 #                     datefmt = '%Y-%m-%d %H:%M:%S %p',
    11 #                     level = 10)
    12 # logging.debug('debug')
    13 formatter1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
    14                     datefmt='%Y-%m-%d %H:%M:%S %p',)
    15 fh1 = logging.FileHandler('test1.log')
    16 fh2 = logging.FileHandler('test2.log')
    17 fh3 = logging.FileHandler('test3.log')
    18 ch = logging.StreamHandler()
    19 
    20 fh1.setFormatter(formatter1)
    21 fh2.setFormatter(formatter1)
    22 fh3.setFormatter(formatter1)
    23 ch.setFormatter(formatter1)
    24 
    25 logger1 = logging.getLogger('egon')
    26 logger1.setLevel(10)
    27 logger1.addHandler(fh1)
    28 logger1.addHandler(fh2)
    29 logger1.addHandler(fh3)
    30 logger1.addHandler(ch)
    31 
    32 
    33 logger1.debug('debug')
    34 logger1.info('info')
    35 logger1.warning('warning')
    36 logger1.critical('critical')

    logger的实际应用,套模板

     1 """
     2 logging配置
     3 """
     4 
     5 import os
     6 import logging.config
     7 
     8 # 定义三种日志输出格式 开始
     9 
    10 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' 
    11                   '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字
    12 
    13 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
    14 
    15 id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
    16 
    17 # 定义日志输出格式 结束
    18 
    19 logfile_dir = os.path.dirname(os.path.abspath(__file__))  # log文件的目录
    20 
    21 logfile_name = 'all2.log'  # log文件名
    22 
    23 # 如果不存在定义的日志目录就创建一个
    24 if not os.path.isdir(logfile_dir):
    25     os.mkdir(logfile_dir)
    26 
    27 # log文件的全路径
    28 logfile_path = os.path.join(logfile_dir, logfile_name)
    29 
    30 # log配置字典
    31 LOGGING_DIC = {
    32     'version': 1,
    33     'disable_existing_loggers': False,
    34     'formatters': {
    35         'standard': {
    36             'format': standard_format
    37         },
    38         'simple': {
    39             'format': simple_format
    40         },
    41     },
    42     'filters': {},
    43     'handlers': {
    44         #打印到终端的日志
    45         'console': {
    46             'level': 'DEBUG',
    47             'class': 'logging.StreamHandler',  # 打印到屏幕
    48             'formatter': 'simple'
    49         },
    50         #打印到文件的日志,收集info及以上的日志
    51         'default': {
    52             'level': 'DEBUG',
    53             'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
    54             'formatter': 'standard',
    55             'filename': logfile_path,  # 日志文件
    56             'maxBytes': 1024*1024*5,  # 日志大小 5M
    57             'backupCount': 5,
    58             'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
    59         },
    60     },
    61     'loggers': {
    62         #logging.getLogger(__name__)拿到的logger配置
    63         '': {
    64             'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
    65             'level': 'DEBUG',
    66             'propagate': True,  # 向上(更高level的logger)传递
    67         },
    68     },
    69 }
    70 
    71 
    72 def load_my_logging_cfg():
    73     logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
    74     logger = logging.getLogger(__name__)  # 生成一个log实例
    75     logger.info('It works!')  # 记录该文件的运行状态
    76 
    77 if __name__ == '__main__':
    78     load_my_logging_cfg()
    79 
    80 logging配置文件
  • 相关阅读:
    HDU 1058 Humble Numbers
    HDU 1160 FatMouse's Speed
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1003 Max Sum
    HDU 1297 Children’s Queue
    UVA1584环状序列 Circular Sequence
    UVA442 矩阵链乘 Matrix Chain Multiplication
    DjangoModels修改后出现You are trying to add a non-nullable field 'download' to book without a default; we can't do that (the database needs something to populate existing rows). Please select a fix:
    opencv做的简单播放器
    c++文件流输入输出
  • 原文地址:https://www.cnblogs.com/wangmengzhu/p/7268333.html
Copyright © 2011-2022 走看看