问题描述
我正在尝试了解应用程序的工作原理.为此,我将调试命令作为每个函数主体的第一行插入,目的是记录函数的名称以及向日志输出发送消息的行号(在代码中).最后,由于此应用程序包含许多文件,因此我想创建一个日志文件,以便更好地了解应用程序的控制流程.
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.
这是我所知道的:
为了获取函数名称,我可以使用
function_name.__name__
但我不想使用 function_name(这样我就可以快速复制和粘贴通用的Log.info("Message")
在所有函数的主体中).我知道这可以使用__func__
宏在 C 中完成,但我不确定 python.
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 genericLog.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("Mymessage %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?
我知道如何使用日志记录并向其添加处理程序以记录到文件,但我不确定是否可以使用单个文件以项目中函数调用的正确顺序记录所有日志消息.
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.
我将不胜感激任何帮助.
I would greatly appreciate any help.
谢谢!
推荐答案
您在这里有几个与此相关的问题.
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
))
这将记录传入的消息,加上(原始)函数名、定义出现的文件名以及该文件中的行.查看inspect - 检查活动对象了解更多详情.
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
交互式调试提示;pdb.set_trace()
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 日志记录(函数名、文件名、行号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!