• python配置日志


    logger_file = "%s%s.log" % (LogPath, Setting.box_id)
    print "Will Log into %s"%logger_file
    logger = logging.getLogger(name)        # 获取或初始化logger,不传name,name就是设置的root,默认logging.info/error就是
    logger.setLevel(logging.DEBUG)
    logger_handler = RotatingFileHandler(filename=logger_file, maxBytes=1024 * 1024 * 100, backupCount=10)
    logger_format = logging.Formatter(fmt='[%(asctime)s]-[%(filename)s:%(lineno)s:%(funcName)s]= %(message)s =!',
                                      datefmt='%Y.%m.%d %H:%M:%S')
    logger_handler.setFormatter(logger_format)
    logger.addHandler(logger_handler)
    

    默认logger是root

    root = RootLogger(WARNING)
    
    def getLogger(name=None):
        """
        Return a logger with the specified name, creating it if necessary.
    
        If no name is specified, return the root logger.
        """
        if name:
            return Logger.manager.getLogger(name)
        else:
            return root
    

    默认直接logging.info/error/debug 也是使用的root

    logging.info()
    
    def info(msg, *args, **kwargs):
        """
        Log a message with severity 'INFO' on the root logger.
        """
        if len(root.handlers) == 0:
            basicConfig()
        root.info(msg, *args, **kwargs)
    

    关于logging.Formatter参数格式:

        %(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
    
  • 相关阅读:
    298. Binary Tree Longest Consecutive Sequence
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    163. Missing Ranges
    336. Palindrome Pairs
    727. Minimum Window Subsequence
    211. Add and Search Word
    年底购物狂欢,移动支付安全不容忽视
    成为程序员前需要做的10件事
    全球首推iOS应用防破解技术!
  • 原文地址:https://www.cnblogs.com/haiton/p/15752793.html
Copyright © 2020-2023  润新知