Reputation: 51
I am using structlog - http://www.structlog.org/en/stable/ in my Python Project. I have one if the processors in the configuration to be
stdlib.add_logger_name
This adds the key in the event_dict to be logger
. But, I want to change the key string to something else like namespace
rather than logger
. How can I do that?
I have checked the function for
stdlib.add_logger_name(logger, method_name, event_dict)
but that function uses hardcoded string logger
as
event_dict["logger"] = logger.name
Upvotes: 1
Views: 1379
Reputation: 51
Thanks to hynek's answer. I solved this by adding a local function:
def add_logger_name(logger, method_name, event_dict):
"""
Add the logger name to the event dict with namespace as the key as per logging convention
"""
record = event_dict.get("_record")
if record is None:
event_dict["namespace"] = logger.name
else:
event_dict["namespace"] = record.name
return event_dict
Setting this in the
processors=[add_logger_name,...]
Upvotes: 1
Reputation: 4126
Currently, structlog.stdlib.add_logger_name()
is 6 LoC, of which you most likely only need two:
def add_logger_name(logger, method_name, event_dict):
"""
Add the logger name to the event dict.
"""
record = event_dict.get("_record")
if record is None:
event_dict["logger"] = logger.name
else:
event_dict["logger"] = record.name
return event_dict
Just copy and paste it and adapt it to your needs.
It wouldn't be worth it to add options to the processor and slow it down for everybody since it didn't come up until today, but structlog
has been engineered purposefully to make such customizations easy.
Upvotes: 1