简体   繁体   English

将sys.stdout重定向到python日志记录

[英]redirecting sys.stdout to python logging

So right now we have a lot of python scripts and we are trying to consolidate them and fix and redundancies. 所以现在我们有很多python脚本,我们正在尝试整合它们并修复和裁员。 One of the things we are trying to do, is to ensure that all sys.stdout/sys.stderr goes into the python logging module. 我们要做的一件事是确保所有sys.stdout / sys.stderr进入python日志记录模块。

Now the main thing is, we want the following printed out: 现在最重要的是,我们希望打印出以下内容:

[<ERROR LEVEL>] | <TIME> | <WHERE> | <MSG>

Now all sys.stdout / sys.stderr msgs pretty much in all of the python error messages are in the format of [LEVEL] - MSG, which are all written using sys.stdout/sys.stderr. 现在所有的python错误消息中的所有sys.stdout / sys.stderr消息都是[LEVEL] - MSG的格式,它们都是使用sys.stdout / sys.stderr编写的。 I can parse the fine, in my sys.stdout wrapper and in the sys.stderr wrapper. 我可以在我的sys.stdout包装器和sys.stderr包装器中解析这个问题。 Then call the corresponding logging level, depending on the parsed input. 然后根据解析的输入调用相应的日志记录级别。

So basically we have a package called foo, and a subpackage called log. 所以基本上我们有一个名为foo的包,以及一个名为log的子包。 In __init__.py we define the following: __init__.py我们定义以下内容:

def initLogging(default_level = logging.INFO, stdout_wrapper = None, \
                stderr_wrapper = None):
    """
        Initialize the default logging sub system
    """
    root_logger = logging.getLogger('')
    strm_out = logging.StreamHandler(sys.__stdout__)
    strm_out.setFormatter(logging.Formatter(DEFAULT_LOG_TIME_FORMAT, \
                                            DEFAULT_LOG_TIME_FORMAT))
    root_logger.setLevel(default_level)
    root_logger.addHandler(strm_out)

    console_logger = logging.getLogger(LOGGER_CONSOLE)
    strm_out = logging.StreamHandler(sys.__stdout__)
    #strm_out.setFormatter(logging.Formatter(DEFAULT_LOG_MSG_FORMAT, \
    #                                        DEFAULT_LOG_TIME_FORMAT))
    console_logger.setLevel(logging.INFO)
    console_logger.addHandler(strm_out)

    if stdout_wrapper:
        sys.stdout = stdout_wrapper
    if stderr_wrapper:
        sys.stderr = stderr_wrapper


def cleanMsg(msg, is_stderr = False):
    logy = logging.getLogger('MSG')
    msg = msg.rstrip('\n').lstrip('\n')
    p_level = r'^(\s+)?\[(?P<LEVEL>\w+)\](\s+)?(?P<MSG>.*)$'
    m = re.match(p_level, msg)
    if m:
        msg = m.group('MSG')
        if m.group('LEVEL') in ('WARNING'):
            logy.warning(msg)
            return
        elif m.group('LEVEL') in ('ERROR'):
            logy.error(msg)
            return
    if is_stderr:
        logy.error(msg)
    else:
        logy.info(msg)

class StdOutWrapper:
    """
        Call wrapper for stdout
    """
    def write(self, s):
        cleanMsg(s, False)

class StdErrWrapper:
    """
        Call wrapper for stderr
    """
    def write(self, s):
        cleanMsg(s, True)

Now we would call this in one of our scripts for example: 现在我们将在我们的一个脚本中调用它,例如:

import foo.log
foo.log.initLogging(20, foo.log.StdOutWrapper(), foo.log.StdErrWrapper())
sys.stdout.write('[ERROR] Foobar blew')

Which would be converted into an error log message. 哪个将转换为错误日志消息。 Like: 喜欢:

[ERROR] | 20090610 083215 | __init__.py | Foobar Blew

Now the problem is when we do that, The module where the error message was logged is now the __init__ (corresponding to foo.log.__init__.py file) which defeats the whole purpose. 现在的问题是,当我们这样做时,记录错误消息的模块现在是__init__ (对应于foo.log.__init__.py文件),这违背了整个目的。

I tried doing a deepCopy/shallowCopy of the stderr/stdout objects, but that does nothing, it still says the module the message occured in __init__.py . 我尝试对stderr / stdout对象执行deepCopy / shallowCopy,但这没有做任何事情,它仍然在模块中显示__init__.py的消息。 How can i make it so this doesn't happen? 我怎么能这样做,所以这不会发生?

The problem is that the logging module is looking a single layer up the call stack to find who called it, but now your function is an intermediate layer at that point (Though I'd have expected it to report cleanMsg , not __init__ , as that's where you're calling into log()). 问题是日志记录模块在调用堆栈中查找单个层以查找谁调用它,但现在您的函数是该点的中间层(虽然我cleanMsg希望它报告cleanMsg ,而不是__init__ ,因为它是你在哪里调用log())。 Instead, you need it to go up two levels, or else pass who your caller is into the logged message. 相反,你需要它上升两个级别,否则将你的调用者传递给记录的消息。 You can do this by inspecting up the stack frame yourself and grabbing the calling function, inserting it into the message. 您可以通过自己检查堆栈帧并抓取调用函数,将其插入到消息中来完成此操作。

To find your calling frame, you can use the inspect module: 要查找调用框架,可以使用inspect模块:

import inspect
f = inspect.currentframe(N)

will look up N frames, and return you the frame pointer. 将查找N帧,并返回帧指针。 ie your immediate caller is currentframe(1), but you may have to go another frame up if this is the stdout.write method. 即你的直接调用者是currentframe(1),但如果这是stdout.write方法,你可能不得不再去另一帧。 Once you have the calling frame, you can get the executing code object, and look at the file and function name associated with it. 拥有调用框架后,您可以获取正在执行的代码对象,并查看与其关联的文件和函数名称。 eg: 例如:

code = f.f_code
caller = '%s:%s' % (code.co_filename, code.co_name)

You may also need to put some code to handle non-python code calling into you (ie. C functions or builtins), as these may lack f_code objects. 您可能还需要放置一些代码来处理调用您的非python代码(即C函数或内置函数),因为这些代码可能缺少f_code对象。

Alternatively, following up mikej's answer , you could use the same approach in a custom Logger class inheriting from logging.Logger that overrides findCaller to navigate several frames up, rather than one. 或者,按照mikej的回答 ,您可以在继承自logging.Logger的自定义Logger类中使用相同的方法,该类覆盖findCaller以向上导航多个帧,而不是一个。

I think the problem is that your actual log messages are now being created by the logy.error and logy.info calls in cleanMsg , hence that method is the source of the log messages and you are seeing this as __init__.py 我认为问题是您的实际日志消息现在由logy.errorlogy.info调用cleanMsg ,因此该方法是日志消息的来源,您将其视为__init__.py

If you look in the source of Python's lib/logging/__init__.py you will see a method defined called findCaller which is what the logging module uses to derive the caller of a logging request. 如果查看Python的lib/logging/__init__.py的源代码,您将看到一个名为findCaller的方法,它是日志记录模块用来派生日志记录请求调用者的方法。
Perhaps you can override this on your logging object to customise the behaviour? 也许您可以在日志记录对象上覆盖它以自定义行为?

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

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