如何使用Python的logging.basicConfig()函数配置日志?

2026-05-22 18:561阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1434个文字,预计阅读时间需要6分钟。

目录 + 1、basicConfig() 函数说明 + 2、应用 + 1、basicConfig() 函数说明 + 本函数,通过创建一个带默认 Formatter(格式化器)的 StreamHandler(处理器),并将其添加到根日志记录器中,初始化基本配置。

目录
  • 1、basicConfig()函数说明
  • 2、应用

1、basicConfig()函数说明
  • 此函数,通过创建一个带有默认Formatter(格式器)的StreamHandler(处理器),并将其添加到根日志记录器中来初始化基本配置。
  • 如果根日志记录器没有定义处理器,则logger.debug()logger.info()logger.warning()logger.error()logger.critical()函数会自动调用 basicConfig()函数中的配置 。
  • 如果根日志记录器已经配置了处理器,则此函数不起作用。

说明:

  • 此函数应该在主线程中调用,且在其他线程开始之前。
  • 在Python2.7.1和3.2之前,此函数被多线程调用。
  • 有可能(极少数)处理器会被多次添加到根日志记录器,导致意外结果比如日志中信息重复。

支持以下关键字参数:

格式 描述 filename 使用指定的文件名filemode 如果指定filename参数,则以此模式打开文件(‘r’、‘w’、‘a’),默认为“a”format 使用指定字符串格式输出。 datefmt 指定时间格式,同time.strftime()函数. style I如果指定了format,请对该格式字符串使用此样式。 '%''{''$'可以分别使用在 printf-style, str.format() or string.Template 中,默认 '%'level 指定根日志记录器级别,默认为logging.WARNINGstream 指定将日志的输出流,可以指定输出到sys.stderr(重定向日志信息,也就是把日志信息不输出到控制台,改为其他地方),sys.stdout或者文件,默认输出到sys.stderr,当streamfilename同时指定时,stream被忽略或者报错ValueError。(一般不用) handlers 指定日志处理器,如果根日志器没有执行新的日志处理器,默认使用此处配置。所有尚未设置格式器的处理程序,都将被分配此函数中创建的默认格式器。 请注意,此参数与filenamestream不兼容,如果两者都存在,则会引发ValueError。(一般不用) 2、应用

注意:这种方式对中文日志信息处理不友好,编码错乱,我们一般也很少用,都使用定义好的日志处理来输入日志。

# coding=utf8 import logging """ 参数含义: level:指定日志级别,默认为logging.INFO format:指定日志输出格式。 datefmt:指定时间格式。 filename:指定写入日志文件名。 filemode:指定写入方式,'w'或'a' 输出格式挺好看的: format = 'levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', """ # 配置basicConfig函数 logging.basicConfig(level=logging.INFO, datefmt='[%d/%b/%Y %H:%M:%S]', filename='../log/info.log') # 创建一个日志器,就是一个logger对象 logger = logging.getLogger('logger') logger.debug('logger debug message') logger.info('logger info message') logger.warning('logger warning message') logger.error('logger error message') logger.critical('logger critical message') # 或者 logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')

输出结果为:info以上的日志级别都输出了。

[17/Jan/2021 01:02:13] demo_log.py[line:24] INFO logger info message [17/Jan/2021 01:02:13] demo_log.py[line:25] WARNING logger warning message [17/Jan/2021 01:02:13] demo_log.py[line:26] ERROR logger error message [17/Jan/2021 01:02:13] demo_log.py[line:27] CRITICAL logger critical message [17/Jan/2021 01:02:13] demo_log.py[line:31] INFO info message [17/Jan/2021 01:02:13] demo_log.py[line:32] WARNING warning message [17/Jan/2021 01:02:13] demo_log.py[line:33] ERROR error message [17/Jan/2021 01:02:13] demo_log.py[line:34] CRITICAL critical message

当我们给日志器定义一个处理器,在看看效果:

# coding=utf8 import logging """ 参数含义: level:指定日志级别,默认为logging.INFO format:指定日志输出格式。 datefmt:指定时间格式。 filename:指定写入日志文件名。 filemode:指定写入方式,'w'或'a' """ logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='../log/info.log') # 创建一个日志器,就是一个logger对象 logger = logging.getLogger('logger') # 1.创建日志处理器 fh = logging.StreamHandler() # 2.设置日志处理器输出级别 fh.setLevel(logging.ERROR) # 3.把处理器添加到日志器中 logger.addHandler(fh) logger.debug('logger debug message') logger.info('logger info message') logger.warning('logger warning message') logger.error('logger error message') logger.critical('logger critical message') # 或者 logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')

输出结果:

logger error message logger critical message

说明:新定义的处理器,代替了basicConfig函数中定义的内容。同时logging.debug('debug message')这种直接调用的方式也不执行了。

本文共计1434个文字,预计阅读时间需要6分钟。

目录 + 1、basicConfig() 函数说明 + 2、应用 + 1、basicConfig() 函数说明 + 本函数,通过创建一个带默认 Formatter(格式化器)的 StreamHandler(处理器),并将其添加到根日志记录器中,初始化基本配置。

目录
  • 1、basicConfig()函数说明
  • 2、应用

1、basicConfig()函数说明
  • 此函数,通过创建一个带有默认Formatter(格式器)的StreamHandler(处理器),并将其添加到根日志记录器中来初始化基本配置。
  • 如果根日志记录器没有定义处理器,则logger.debug()logger.info()logger.warning()logger.error()logger.critical()函数会自动调用 basicConfig()函数中的配置 。
  • 如果根日志记录器已经配置了处理器,则此函数不起作用。

说明:

  • 此函数应该在主线程中调用,且在其他线程开始之前。
  • 在Python2.7.1和3.2之前,此函数被多线程调用。
  • 有可能(极少数)处理器会被多次添加到根日志记录器,导致意外结果比如日志中信息重复。

支持以下关键字参数:

格式 描述 filename 使用指定的文件名filemode 如果指定filename参数,则以此模式打开文件(‘r’、‘w’、‘a’),默认为“a”format 使用指定字符串格式输出。 datefmt 指定时间格式,同time.strftime()函数. style I如果指定了format,请对该格式字符串使用此样式。 '%''{''$'可以分别使用在 printf-style, str.format() or string.Template 中,默认 '%'level 指定根日志记录器级别,默认为logging.WARNINGstream 指定将日志的输出流,可以指定输出到sys.stderr(重定向日志信息,也就是把日志信息不输出到控制台,改为其他地方),sys.stdout或者文件,默认输出到sys.stderr,当streamfilename同时指定时,stream被忽略或者报错ValueError。(一般不用) handlers 指定日志处理器,如果根日志器没有执行新的日志处理器,默认使用此处配置。所有尚未设置格式器的处理程序,都将被分配此函数中创建的默认格式器。 请注意,此参数与filenamestream不兼容,如果两者都存在,则会引发ValueError。(一般不用) 2、应用

注意:这种方式对中文日志信息处理不友好,编码错乱,我们一般也很少用,都使用定义好的日志处理来输入日志。

# coding=utf8 import logging """ 参数含义: level:指定日志级别,默认为logging.INFO format:指定日志输出格式。 datefmt:指定时间格式。 filename:指定写入日志文件名。 filemode:指定写入方式,'w'或'a' 输出格式挺好看的: format = 'levelname:%(levelname)s filename: %(filename)s ' 'outputNumber: [%(lineno)d] thread: %(threadName)s output msg: %(message)s' ' - %(asctime)s', """ # 配置basicConfig函数 logging.basicConfig(level=logging.INFO, datefmt='[%d/%b/%Y %H:%M:%S]', filename='../log/info.log') # 创建一个日志器,就是一个logger对象 logger = logging.getLogger('logger') logger.debug('logger debug message') logger.info('logger info message') logger.warning('logger warning message') logger.error('logger error message') logger.critical('logger critical message') # 或者 logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')

输出结果为:info以上的日志级别都输出了。

[17/Jan/2021 01:02:13] demo_log.py[line:24] INFO logger info message [17/Jan/2021 01:02:13] demo_log.py[line:25] WARNING logger warning message [17/Jan/2021 01:02:13] demo_log.py[line:26] ERROR logger error message [17/Jan/2021 01:02:13] demo_log.py[line:27] CRITICAL logger critical message [17/Jan/2021 01:02:13] demo_log.py[line:31] INFO info message [17/Jan/2021 01:02:13] demo_log.py[line:32] WARNING warning message [17/Jan/2021 01:02:13] demo_log.py[line:33] ERROR error message [17/Jan/2021 01:02:13] demo_log.py[line:34] CRITICAL critical message

当我们给日志器定义一个处理器,在看看效果:

# coding=utf8 import logging """ 参数含义: level:指定日志级别,默认为logging.INFO format:指定日志输出格式。 datefmt:指定时间格式。 filename:指定写入日志文件名。 filemode:指定写入方式,'w'或'a' """ logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='[%d/%b/%Y %H:%M:%S]', filename='../log/info.log') # 创建一个日志器,就是一个logger对象 logger = logging.getLogger('logger') # 1.创建日志处理器 fh = logging.StreamHandler() # 2.设置日志处理器输出级别 fh.setLevel(logging.ERROR) # 3.把处理器添加到日志器中 logger.addHandler(fh) logger.debug('logger debug message') logger.info('logger info message') logger.warning('logger warning message') logger.error('logger error message') logger.critical('logger critical message') # 或者 logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')

输出结果:

logger error message logger critical message

说明:新定义的处理器,代替了basicConfig函数中定义的内容。同时logging.debug('debug message')这种直接调用的方式也不执行了。