日志模块的使用

import logging

# 为什么要写log:
    # 1.记录用户的行为:用来做数据分析的
    # 2.记录用户的行为:操作审计
    # 3.排错

# logging.basicConfig(level=logging.DEBUG)  # 改变日子显示等级
# 输出内容是有等级的 : 默认处理warning级别以上的所有信息
# logging.basicConfig(level=logging.DEBUG)
# logging.info("xxxxx")  # INFO:root:xxxxx

# logging.debug('debug message')          # 调试
# logging.info('info message')            # 信息
# logging.warning('warning message')      # 警告  WARNING:root:warning message
#                                                 # ERROR:root:error message
#                                                 # CRITICAL:root:critical message
# logging.error('error message')          # 错误
# logging.critical('critical message')    # 批判性的


# 1.无论你希望日志里打印哪些内容,都得你自己写,没有自动生成日志这种事儿

def log_to_screen():
    # 利用 :logging.basicConfig
    # 输出到屏幕,默认就是输出到屏幕上
    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s[line :%(lineno)d]-%(module)s:  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S %p',
    )
    logging.warning('11111')
    logging.error('2222')
    logging.critical('33333')


def log_file():
    # 输出到文件,并且设置信息的等级
    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s[line :%(lineno)d]-%(module)s:  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S %p',
        # 加了这个参数就会输出到文件
        filename='write_to_file.log',
        level= logging.DEBUG
    )
    logging.debug('debug 信息错误 test2')
    logging.info('warning 信息错误 test2')
    logging.warning('warning message test2')
    logging.error('error message test2')
    logging.critical('critical message test2')


def log_file_and_screen():
    # 同时向文件和屏幕上输出 和 乱码
    fh = logging.FileHandler('tmp.log',encoding='utf-8')  # 创建一个文件句柄
    sh = logging.StreamHandler() # 创建一个屏幕的句柄

    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s[line :%(lineno)d]-%(module)s:  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S %p',
        level= logging.DEBUG,
        handlers=[fh,sh]
    )
    logging.debug('debug 信息错误 test2')
    logging.info('warning 信息错误 test2')
    logging.warning('warning message test2')
    logging.error('error message test2')
    logging.critical('critical message test2')


# 做日志的切分
def log_cut():
    import time
    from logging import handlers

    sh = logging.StreamHandler()
    # 按大小切割 区别FileHandler
    rh = handlers.RotatingFileHandler("/Users/heqingqing/log/myapp.log", maxBytes=1024,backupCount=2)   # 按照大小做切割
    # 时间切割
    fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=10, encoding='utf-8')
    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s[line :%(lineno)d]-%(module)s:  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S %p',
        level= logging.DEBUG,
        handlers=[fh,rh,sh]
    )
    for i in range(1,100000):
        time.sleep(1)
        logging.error('KeyboardInterrupt error %s'%str(i))

# log_file()
# log_to_screen()
# log_file_and_screen()
# log_cut()

原文地址:https://www.cnblogs.com/he-qing-qing/p/12892395.html