可用于日志格式的调用堆栈
因此,我有一个返回调用堆栈的简单字符串的方法。
def call_stack():
if "inspect" not in globals().keys():
import inspect
stack = [frame[3] for frame in inspect.stack() if frame[3] not in [inspect.stack()[0][3],"<module>"]]
s = ""
for method in stack:
if len(s) > 0:
s += "."
s += method
return s
def modify_logger(logger):
logger.args["call_stack"] = call_stack()
是否有可能创建这种行为?
import logging
logging.basicConfig(level=logging.DEBUG, format="%(call_stack)s -- %(levelname)s: %(message)s")
def bar():
logging.debug("test")
def foo():
bar()
def monkey():
foo()
# modify root logger.
modify_logger(logging.getLogger())
# write to the log.
monkey()
这会产生以下条目:
monkey.foo.bar -- DEBUG: test
So, I have a method that returns a simple string of the call-stack.
def call_stack():
if "inspect" not in globals().keys():
import inspect
stack = [frame[3] for frame in inspect.stack() if frame[3] not in [inspect.stack()[0][3],"<module>"]]
s = ""
for method in stack:
if len(s) > 0:
s += "."
s += method
return s
def modify_logger(logger):
logger.args["call_stack"] = call_stack()
Is it possible to create this behavior?
import logging
logging.basicConfig(level=logging.DEBUG, format="%(call_stack)s -- %(levelname)s: %(message)s")
def bar():
logging.debug("test")
def foo():
bar()
def monkey():
foo()
# modify root logger.
modify_logger(logging.getLogger())
# write to the log.
monkey()
Which results in the following entry:
monkey.foo.bar -- DEBUG: test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许最简单的方法就是定义一个自定义的
debug
函数:
Perhaps the easiest way is just to define a custom
debug
function:yields
昨天对这个模式进行了相当多的研究之后,我意识到我想要的对于 Python 语言来说是不合理的。毕竟,它是一种解释性语言,这意味着总有一个文件存在于已知路径和可以引用的行号上。在 C#、Java、C 或 C++ 等编译语言中,这是有意义的,因为您拥有命名空间、对象和方法,但没有文件/路径和行号的好处。
故事的寓意
当人们告诉您您的要求没有意义时,不要只是忽略他们。在积极寻找在您的特定用例或上下文中可能没有意义的问题的答案之前,请花一些时间仔细检查您最初的请求以及您的观点。
After having researched this pattern quite a bit yesterday, I realized that what I wanted wasn't reasonable for the Python language. Its an interpreted language, after all, which means that there is always a file that exists on a known path and a line number which can be referenced. It would make sense, in a compiled language like C#, Java, C, or C++ because you have namespaces, objects, and methods without the benefit of having a file/path and line number.
Moral of the story
When people tell you that what you're asking for doesn't make sense, don't just write them off. Take a few moments and scrutinize your original request along with your perspective before vigorously pursuing an answer to a question that might not make sense in your specific use-case or context.
我尝试将你的解决方案与另一个解决方案混合,它给了我我正在寻找的结果。
I tried your solution mixed with another, it gave me the results I was looking for.