• 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
    
  • 相关阅读:
    skywalking简介
    .Net Core微服务——Consul(4):搭建集群
    .Net Core微服务——Consul(3):健康检查
    .Net Core微服务——Consul(2):自动扩展、服务调用
    .Net Core微服务——Consul(1):服务发现
    SpringBoot数据访问之整合Mybatis配置文件
    SpringBoot数据访问之Druid启动器的使用
    SpringBoot数据访问之Druid数据源的自定义使用
    Spring Boot核心技术之Restful映射以及源码的分析
    SpringBoot之yaml语法及静态资源访问
  • 原文地址:https://www.cnblogs.com/haiton/p/15752793.html
Copyright © 2020-2023  润新知