Python File logger也记录到stdout [英] Python File logger also logging to stdout

查看:55
本文介绍了Python File logger也记录到stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python 3.8中,我已将记录器配置为:

In python 3.8, I've configured logger as:

logging.root.handlers = []

fileHandler = RotatingFileHandler(logpath, maxBytes = 500000, backupCount = 10)
localLogFormatter = logging.Formatter("%(asctime)s [%(levelname)s] %(filename)s - %(funcName)s - %(message)s")
fileHandler.setFormatter(localLogFormatter)

file_logger = logging.getLogger('file_logger')
file_logger.setLevel(logging.INFO)
file_logger.addHandler(fileHandler)
constants.file_logger = file_logger

constants.file_logger.info("Fetching access token")

以上内容记录到文件,但也将以下内容输出到stdout:

The above logs to a file, but also prints following to stdout:

INFO:file_logger:获取访问令牌

INFO:file_logger:Fetching access token

我不希望将日志打印到标准输出.如何避免这种情况,并使日志仅打印到文件上?

I don't wish the log to be printed to stdout. How can I avoid this and make the log printed only to the file?

推荐答案

尝试使用basicConfig参数,并仅在其中传递Filehandler.不要通过 StreamHandler()

Try to use the basicConfig parameter and pass only the Filehandler in it. Don't pass the StreamHandler()

logging.basicConfig(handlers=[
        logging.FileHandler(<path to file>)
], format='%(asctime)s %(env)s %(levelname)s %(message)s')

这篇关于Python File logger也记录到stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆