I have some Python code I need to add logging to.
I've always preferred nice big C macro looking statements like "DEBUG()", "ERROR()", etc for logging. I feel it makes the code easier to read (no objects) when trace-points are visually differentiable from the actual code.
I also would like to be able to set logging levels at a per-module level.
How could I make a module called "log" that is capable of doing this (while making use of the Python std library logging module)?
E.g.:
File: main.py
# This imports LOG_MODULE_NAME, DEBUG, WARN, etc
from log import *
import my_module
LOG_MODULE_NAME("main")
log.set_level("main", log.LVL_DEBUG)
log.set_level("my_module", log.LVL_WARN)
if __name__ == "__main__":
foo = my_module.myFunc(2)
DEBUG("Exiting main.py")
File: my_module.py
from log import *
LOG_MODULE_NAME("my_module")
def myFunc(x):
DEBUG("Entering function")
if x != 1:
WARN("I thought it would be 1")
DEBUG("Exiting function")
return x+1
I'd expect output to look something like:
[WARN:my_module - my_module.py:9] I thought it would be 1
[DEBUG:main - main.py:11] Exiting main.py