简体   繁体   English

登录 python 时包含 stderr 和 stdout

[英]Include stderr and stdout when logging in python

Having the following logging:具有以下日志记录:

log_name = pd.datetime.now().date().strftime(format="%Y-%m-%d")+".log" #Current day
log_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))+"/logs/" #Path
logging.basicConfig(filename=log_path+log_name,filemode="a",level = logging.INFO)

is there a way to catch "Unknown" exceptions (such as pd.to_sql errors due to eg typo in the column name) and write them to the same logger file specified in logging.basicConfig without having try/catches all over the place?有没有办法捕获“未知”异常(例如,由于列名中的拼写错误导致的pd.to_sql错误)并将它们写入logging.basicConfig中指定的同一个记录器文件,而无需到处进行尝试/捕获?

Most of the times i use a combination of traceback library(standard) and a very big try-except clause under lets say main() function.So any uncaught error can be logged without special treatment.大多数时候,我结合使用回溯库(标准)和一个非常大的 try-except 子句,比如 main() function。因此,任何未捕获的错误都可以在没有特殊处理的情况下记录下来。 I hope fits your needs.我希望能满足你的需求。 Example:例子:

import logging
import traceback

try:
    main()
except:
    logger.warning(traceback.format_exc())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中记录和旋转stdout和stderr - Logging and rotating stdout and stderr in Python 在Python守护程序中维护日志记录和/或stdout / stderr - Maintaining Logging and/or stdout/stderr in Python Daemon Python日志记录默认是写入stdout还是stderr? - Does Python logging write to stdout or stderr by default? Python 脚本打印输出仅出现在执行结束时,在外部调用它并将 stdout/stderr 记录到文件中时 - Python script printouts just appearing at the end of execution when calling it externally and logging stdout/stderr into file 使用 Popen 启动的 Python 服务有记录 stderr 和 stdout 重定向问题 - Python Service launched with Popen have logging stderr & stdout redirection problems 使用Python'swith'语句将stdout和stderr记录到日志文件中 - Logging stdout and stderr to log file using Python 'with' statement Python 捕获 stdout/stderr 并在查看输出时记录到文件 - Python capturing stdout/stderr and logging to file while seeing output Python:结合日志记录和wx,以便将日志记录流重定向到stdout / stderr框架 - Python: combine logging and wx so that logging stream is redirectet to stdout/stderr frame Python监视子进程的stderr和stdout - Python monitoring stderr and stdout of a subprocess 在 python setUpClass 中输出 stdout 或 stderr - Outputting stdout or stderr in python setUpClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM