提问者:小点点

Python日志打印到文件,但没有控制台[重复]


登录到文件和打印到stdout的日志配置不适用于我,所以:

我想在prog_log中打印日志详细信息。在控制台中,我有:

# 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')

以上内容会打印到程序日志中。txt文件记录详细信息,但控制台上没有记录。。

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

欢迎任何帮助


共1个答案

匿名用户

重复的记录器配置,以记录到文件并打印到标准输出

您需要添加和处理程序以将日志设置为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()
    ]
)