MAC address in the log format
Opened this issue · 1 comments
jotauses commented
Currently the formats are declared using JSON or YAML, but I need to pass a variable, specifically the MAC of the machine. How could I insert a variable into the formatter?
Dragon2fly commented
Hi @jotauses
Assume that you add %(MAC)s
into the format
fields of formatters
in either JSON or YAML.
Then at runtime, you simply add the same attribute name into the logging record by adding a filter to a handler.
The set_context_injector
below is a shortcut to add a filter to every handler of the root logger.
my_mac = 'AA:BB:CC:DD:EE:FF'
def mac_filter(record):
# Add a MAC property to the record
record.MAC = my_mac
# return False will discard this record
return True
log_config = setup_logging()
log_config.set_context_injector(mac_filter)
Not tested yet, but hope it works well for you.
Have a good day!