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
posted @ 2021-12-31 13:52  华腾海神  阅读(70)  评论(0编辑  收藏  举报