简体   繁体   English

Python Fabric记录错误

[英]Python Fabric logging errors

I am trying to understand how fabric's logger module works. 我试图了解Fabric的记录器模块如何工作。 I run on the command line: 我在命令行上运行:

 $ fabfile -I task-1

I of course get output to the console showing me the execution of the task on each of the remote hosts connected to. 我当然会输出到控制台,向我显示在连接的每个远程主机上任务的执行情况。 Bu how can I redirect the output of errors to a logfile on my local machine and put a timestamp on it? Bu如何将错误输出重定向到本地计算机上的日志文件并在其上加上时间戳? Does fabric's logger module provide this? Fabric的记录器模块是否提供此功能? Or should I use Python's logging module. 还是应该使用Python的日志记录模块。 Either one, I am not sure how to implement. 无论哪种,我都不知道如何实现。

Unfortunately, Fabric does not feature logging to a file (see issue #57 ) 不幸的是,Fabric没有记录到文件的功能(请参阅问题#57

But there is a workaround using the logging module, which I find pretty nice. 但是有一种使用logging模块的解决方法,我觉得很好。

First, configure your logger: 首先,配置您的记录器:

import logging

logging.basicConfig(
    level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
    filename="out.log",
    filemode='a'
)

And then wrap the portions of your code which are likely to throw errors with a try/catch block like this: 然后使用try/catch块包装可能会引发错误的代码部分,如下所示:

try:
    #code
except:
    logging.exception('Error:')

The logger will print 'Error:' and the exception's stacktrace to "out.log" 记录器将打印'Error:' ,并将异常的堆栈跟踪输出到“ out.log”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM