Is it possible to decorate/extend the python standard logging system, so that when a logging method is invoked it also logs the file and the line number where it was invoked or maybe the method that invoked it?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
On top of Seb's very useful answer, here is a handy code snippet that demonstrates the logger usage with a reasonable format:
Generates this output:
To build on the above in a way that sends debug logging to standard out:
Then if you want to turn that off comment out
root.setLevel(logging.DEBUG)
.For single files (e.g. class assignments) I've found this a far better way of doing this as opposed to using
print()
statements. Where it allows you to turn the debug output off in a single place before you submit it.Sure, check formatters in logging docs. Specifically the lineno and pathname variables.
Looks something like this: