zoukankan      html  css  js  c++  java
  • Python. logging

    import logging
    logger = logging.getLogger('test')
    logger.setLevel(logging.INFO)
    #设置日志输出格式
    fmt = logging.Formatter('%(name)s - %(levelname)s -%(asctime)s - %(message)s')
    
    #添加StreamHandler
    stream_hdl = logging.StreamHandler()
    stream_hdl.setLevel(logging.WARNING)
    logger.addHandler(stream_hdl)
    
    #添加FileHandler
    file_hd1 = logging.FileHandler('/Users/nali/Downloads/test/test0821.log')
    file_hd1.setLevel(logging.DEBUG)
    file_hd1.setFormatter(fmt)
    logger.addHandler(file_hd1)
    
    
    logger.warning('<warning> message.')
    logger.info('<info> message.')
    logger.debug('<debug> message.')
    

     logger的format格式参数

    %(name)s ——Name of the logger (logging channel)
    %(levelno)s ——Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL)
    %(levelname)s ——Text logging level for the message (“DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”)
    %(pathname)s ——Full pathname of the source file where the logging call was issued (if available)
    %(filename)s ——Filename portion of pathname
    %(module)s ——Module (name portion of filename)
    %(lineno)d ——Source line number where the logging call was issued(if available)
    %(funcName)s ——Function name
    %(created)f ——Time when the LogRecord was created (time.time() return value)
    %(asctime)s ——Textual time when the LogRecord was created
    %(msecs)d ——Millisecond portion of the creation time
    %(relativeCreated)d ——Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time)
    %(thread)d ——Thread ID (if available)
    %(threadName)s ——Thread name (if available)
    %(process)d ——Process ID (if available)
    %(message)s ——The result of record.getMessage(), computed just as the record is emitted
    

     也可以给Formatter传入第二个参数,修改日期格式,比如 %Y-%m-%d ,则输出的log就没有时分秒了。

  • 相关阅读:
    Building a Space Station POJ
    Networking POJ
    POJ 1251 Jungle Roads
    CodeForces
    CodeForces
    kuangbin专题 专题一 简单搜索 POJ 1426 Find The Multiple
    The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk's pool
    The Preliminary Contest for ICPC Asia Shenyang 2019 H. Texas hold'em Poker
    The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team
    robotparser (File Formats) – Python 中文开发手册
  • 原文地址:https://www.cnblogs.com/xiyuan2016/p/9512495.html
Copyright © 2011-2022 走看看