【问题标题】:python logging prints to file but nothing to console [duplicate]python日志记录打印到文件但没有控制台[重复]
【发布时间】:2021-10-07 16:01:06
【问题描述】:

logger configuration to log to file and print to stdout 不适合我,所以:

我想在控制台中的 prog_log.txt 中打印日志记录详细信息,我有:

# Debug Settings
Import logging
#logging.disable()  #when program ready, un-indent this line to remove all logging messages
logging.basicConfig(level=logging.DEBUG,
                    filename = 'prog_log.txt',
                    format='%(asctime)s - %(levelname)s - %(message)s',
                    filemode = 'w')
logging.debug('Start of Program')

上面确实将日志记录详细信息打印到 prog_log.txt 文件中但控制台上没有任何内容..

In [3]: runfile('/Volumes/GoogleDrive/Mon Drive/MAC_test.py', wdir='/Volumes/GoogleDrive/Mon Drive/')
...nothing...

欢迎任何帮助

【问题讨论】:

标签: python logging


【解决方案1】:

logger configuration to log to file and print to stdout 的副本

您需要添加和处理程序以将日志设置为 StreamHandler :

logging.basicConfig(level=logging.DEBUG,
                    filename = 'prog_log.txt',
                    format='%(asctime)s - %(levelname)s - %(message)s',
                    filemode = 'w')
logger.addHandler(logging.StreamHandler())

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s',
    filemode = 'w',
    handlers=[
        logging.FileHandler('prog_log.txt'),
        logging.StreamHandler()
    ]
)

【讨论】:

  • 感谢@Julien Sorin 的帮助。当我首先使用 logger = logging.getLogger('') 创建一个记录器时,第一个选项会导致某个地方。但是当多次运行时,我得到“程序开始”“程序开始”“程序开始”(多次显示且未格式化)..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 2020-08-26
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 2013-09-25
相关资源
最近更新 更多