Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAC address in the log format #29

Open
jotauses opened this issue Feb 5, 2024 · 1 comment
Open

MAC address in the log format #29

jotauses opened this issue Feb 5, 2024 · 1 comment

Comments

@jotauses
Copy link

jotauses commented Feb 5, 2024

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
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants