设置日志级别会导致结构抱怨“无法找到记录器处理程序”ssh.transport“'

时间:2012-03-29 14:21:13

标签: python logging fabric

以下脚本:

#!/usr/bin/env python
from   fabric.api import env, run
import logging

logging.getLogger().setLevel(logging.INFO)

env.host_string = "%s@%s:%s" % ('myuser', 'myhost', '22')
res = run('date', pty = False)

产生以下输出:

[myuser@myhost:22] run: date
No handlers could be found for logger "ssh.transport"
[myuser@myhost:22] out: Thu Mar 29 16:15:15 CEST 2012

我想摆脱这个恼人的错误消息:No handlers could be found for logger "ssh.transport" 设置日志级别(setLevel)时会发生此问题。

我该如何解决这个问题?我需要设置日志级别,因此跳过这将无济于事。

2 个答案:

答案 0 :(得分:2)

您需要初始化日志记录系统。您可以在应用程序中这样做错误消失:

import logging
logging.basicConfig( level=logging.INFO )

注意:这使用默认的Formatter,它不是非常有用。你可能会考虑更像:

import logging
FORMAT="%(name)s %(funcName)s:%(lineno)d %(message)s"
logging.basicConfig(format=FORMAT, level=logging.INFO)

答案 1 :(得分:0)

我的黑客很丑,但有效:

# This is here to avoid the mysterious messages: 'No handlers could be found for logger "ssh.transport"'

class MyNullHandler(logging.Handler):
    def emit(self, record):
        pass

bugfix_loggers = { }
def bugfix(name):
    global bugfix_loggers
    if not name in bugfix_loggers:
        # print "Setting dummy logger for '%s'" % (name)
        logging.getLogger(name).addHandler(MyNullHandler())
        bugfix_loggers[name] = True