使用单个文件进行Python日志记录(函数名,文件名,行号) [英] Python Logging (function name, file name, line number) using a single file

查看:379
本文介绍了使用单个文件进行Python日志记录(函数名,文件名,行号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习应用程序的工作方式.为此,我将调试命令作为每个函数主体的第一行插入,目的是记录该函数的名称以及将消息发送到日志输出的行号(在代码内).最后,由于该应用程序包含许多文件,因此我想创建一个日志文件,以便更好地了解应用程序的控制流程.

I am trying to learn how an application works. And for this I am inserting debug commands as the first line of each function's body with the goal of logging the function's name as well as the line number (within the code) where I send a message to the log output. Finally, since this application comprises of many files, I want to create a single log file so that I can better understand the control flow of the application.

这是我所知道的:

  1. 为了获得函数名,我可以使用function_name.__name__,但是我不想使用function_name(这样我就可以在所有函数的主体中快速复制并粘贴通用Log.info("Message")).我知道可以使用__func__宏在C语言中完成此操作,但是我不确定python.

  1. for getting function name, I can use function_name.__name__ but I don't want to use the function_name (so that I could rapidly copy and paste a generic Log.info("Message") in the body of all functions). I know this could be done in C using __func__ macro but I am not sure about python.

用于获取文件名和行号,我已经看到(并且我相信)我的应用程序正在使用Python locals()函数,但是使用的语法我并没有完全意识到,例如:options = "LOG.debug('%(flag)s : %(flag_get)s' % locals())和我尝试使用类似LOG.info("My message %s" % locals())的方法来产生类似{'self': <__main__.Class_name object at 0x22f8cd0>}的方法.对此有任何输入吗?

for getting the filename and line number, I have seen that (and I believe that) my application is using Python locals() function but in a syntax that I am not completely aware of e.g.: options = "LOG.debug('%(flag)s : %(flag_get)s' % locals()) and I tried it using like LOG.info("My message %s" % locals()) which produces something like {'self': <__main__.Class_name object at 0x22f8cd0>}. Any input on this please?

我知道如何使用日志记录并向其中添加处理程序以将日志记录到文件中,但是我不确定是否可以使用单个文件来按项目中函数调用的正确顺序记录所有日志消息. /p>

I know how to use logging and add handler to it to log to a file but I am not sure if a single file can be used to record all log messages in correct order of function calls in the project.

我将不胜感激.

谢谢!

推荐答案

您在这里有一些边际相关的问题.

You have a few marginally related questions here.

我将从最简单的开始:(3).使用logging,您可以将所有调用聚合到一个日志文件或其他输出目标:它们将按照在处理过程中发生的顺序进行.

I'll start with the easiest: (3). Using logging you can aggregate all calls to a single log file or other output target: they will be in the order they occurred in the process.

下一步:(2). locals()提供当前范围的指示.因此,在没有其他参数的方法中,作用域中的self包含对当前实例的引用.困扰您的窍门是使用dict作为%运算符的RHS的字符串格式. "%(foo)s" % bar将被任何bar["foo"]的值代替.

Next up: (2). locals() provides a dict of the current scope. Thus, in a method that has no other arguments, you have self in scope, which contains a reference to the current instance. The trick being used that is stumping you is the string formatting using a dict as the RHS of the % operator. "%(foo)s" % bar will be replaced by whatever the value of bar["foo"] is.

最后,您可以使用一些自省技巧,类似于pdb可以记录更多信息的那些技巧:

Finally, you can use some introspection tricks, similar to those used by pdb that can log more info:

def autolog(message):
    "Automatically log the current function details."
    import inspect, logging
    # Get the previous frame in the stack, otherwise it would
    # be this function!!!
    func = inspect.currentframe().f_back.f_code
    # Dump the message + the name of this function to the log.
    logging.debug("%s: %s in %s:%i" % (
        message, 
        func.co_name, 
        func.co_filename, 
        func.co_firstlineno
    ))

这将记录传入的消息,以及(原始)函数名称,出现定义的文件名以及该文件中的行.看看检查-检查活动对象了解更多详细信息.

This will log the message passed in, plus the (original) function name, the filename in which the definition appears, and the line in that file. Have a look at inspect - Inspect live objects for more details.

正如我在前面的评论中提到的那样,您还可以随时通过在其中插入import pdb; pdb.set_trace()行并重新运行程序来进入pdb交互式调试提示符.这样一来,您就可以逐步检查代码,并根据需要检查数据.

As I mentioned in my comment earlier, you can also drop into a pdb interactive debugging prompt at any time by inserting the line import pdb; pdb.set_trace() in, and re-running your program. This enables you to step through the code, inspecting data as you choose.

这篇关于使用单个文件进行Python日志记录(函数名,文件名,行号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆