[英]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.