• 模块 logging


    日志打印

    logging.debug("aaaaa")
    logging.info("bbbbb")
    logging.warning("ccc")
    logging.error("ddd")
    logging.critical("eee")

    日志级别,严重程度为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

    LevelNumeric valueWhen it’s used
    DEBUG 10 Detailed information, typically of interest only when diagnosing problems.
    INFO 20 Confirmation that things are working as expected.
    WARNING 30 An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
    ERROR 40 Due to a more serious problem, the software has not been able to perform some function.
    CRITICAL 50 A serious error, indicating that the program itself may be unable to continue running.
    NOSET 0 getattr(logging, loglevel.upper())

     输出到多处

    # logger提供了应用程序可以直接使用的接口
    # handler将日志记录发送给合适的目的输出
    # filter提供了细度设备来决定输出哪条日志
    # formatter决定日志记录的最终输出形式
    
    logger = logging.getLogger("wook-log")
    logger.setLevel(logging.DEBUG)
    logger.warning("2222")
    
    sh = logging.StreamHandler()
    sh.setLevel(logging.WARNING)
    
    fh = logging.FileHandler("access.log")
    fh.setLevel(logging.WARNING)
    formatter = logging.Formatter('%(asctime)s %(message)s  %(filename)s: %(lineno)d %(process)d')
    
    sh.setFormatter(formatter)
    fh.setFormatter(formatter)
    
    logger.addHandler(sh)
    logger.addHandler(fh)
    
    logger.warning("sss")

    输出到文件

    logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(message)s %(filename)s: %(lineno)d %(module)s %(funcName)s %(process)d',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='myapp.log',
                    filemode='w')
        
    logging.debug('This is debug message')
    logging.info('This is info message')
    logging.warning('This is warning message')

    filename: 指定日志文件名
    filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
    format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:

    日志格式

    %(name)s

    Logger的名字

    %(levelno)s

    数字形式的日志级别

    %(levelname)s

    文本形式的日志级别

    %(pathname)s

    调用日志输出函数的模块的完整路径名,可能没有

    %(filename)s

    调用日志输出函数的模块的文件名

    %(module)s

    调用日志输出函数的模块名

    %(funcName)s

    调用日志输出函数的函数名

    %(lineno)d

    调用日志输出函数的语句所在的代码行

    %(created)f

    当前时间,用UNIX标准的表示时间的浮 点数表示

    %(relativeCreated)d

    输出日志信息时的,自Logger创建以 来的毫秒数

    %(asctime)s

    字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒

    %(thread)d

    线程ID。可能没有

    %(threadName)s

    线程名。可能没有

    %(process)d

    进程ID。可能没有

    %(message)s

    用户输出的消息


    日志切割

    from logging import handlers
    import time
    # 日志切割,根据时间或打大小
    # rotatingFileHandler,管理文件大小,当文件达到一定大小之后,它会自动将当前日志文件改名,然后创建一个新的同名日志文件继续输出
    logger = logging.getLogger("TEST")
    log_file = "access.log"
    by_size = handlers.RotatingFileHandler(filename=log_file, maxBytes=10, backupCount=3)
    by_time = handlers.TimedRotatingFileHandler(filename=log_file, when='S', interval=5, backupCount=2)
    formatter = logging.Formatter('%(asctime)s %(message)s  %(filename)s: %(lineno)d')
    by_time.setFormatter(formatter)
    logger.addHandler(by_time)
    
    logger.critical("123")
    logger.critical("456")
    time.sleep(6)
    logger.critical("789")
    logger.critical("112233445566")

    其中filename参数和backupCount参数和RotatingFileHandler具有相同的意义。
    interval是时间间隔。
    when参数是一个字符串。表示时间间隔的单位,不区分大小写。它有以下取值:
    S 秒
    M 分
    H 小时
    D 天
    W 每星期(interval==0时代表星期一)
    midnight 每天凌晨

  • 相关阅读:
    [POJ 1417] True Liars
    [POJ 2912] Rochambeau
    [NOIP 2010] 关押罪犯
    [BZOJ 3365] Distance Statistics
    【BZOJ 2821】作诗
    【IOI 2011】Race
    【POJ 2828】Buy Tickets
    FATFS 文件系统
    w25q128 优化读写函数
    keil5编译时出现 MDK-Pro middleware is not allowed with this license
  • 原文地址:https://www.cnblogs.com/hinimix/p/8515824.html
Copyright © 2020-2023  润新知