You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to log context variables when logging messages. An example (without rich) would be to add new keys to json object logging via filter and somehow json logger prints out the whole object.
Example of context filter that adds attributes to json object:
ifisinstance(context, dict):
# Add the context variables to the log recordforkey, valueincontext.items():
ifnothasattr(record, key):
# Add individual context variables as attributessetattr(record, key, value)
But when doing that with rich it does not print these variables that are added as attributes. Right now I could only "append" them to the message field, but that really defeats the purpose of structured logging.
logging_config= {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"json": {
"()": "pythonjsonlogger.json.JsonFormatter",
"format": """ %(asctime)s %(created)f %(filename)s %(lineno)d %(funcName)s %(levelname)s %(message)s """,
"datefmt": "%Y-%m-%d %H:%M:%S",
},
"rich": {
"format": "%(message)s",
### Is there a way to make the rich handle the secondary arguments and create a structured log?
},
},
"filters": {
"enhanced_context_filter": {
"()": ContextFilter,
},
},
"handlers": {
"json": {
"formatter": "json",
"class": "logging.StreamHandler",
"stream": stream,
},
"rich": {
"class": "rich.logging.RichHandler",
"formatter": "rich",
"markup": True,
"show_time": True,
"show_path": True,
"enable_link_path": False,
# "rich_tracebacks": True,"filters": ["enhanced_context_filter"],
},
},
}
logging.config.dictConfig(logging_config)
Maybe this was answered before therefore let me know so I will close the discussion, but couldn't find anything. Thank you all for you input.
Here is an example with the context vars put into "message" field, but I would rather make the richHandler to print them but keep them in some different field.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I would like to log context variables when logging messages. An example (without rich) would be to add new keys to json object logging via filter and somehow json logger prints out the whole object.
Example of context filter that adds attributes to json object:
But when doing that with rich it does not print these variables that are added as attributes. Right now I could only "append" them to the message field, but that really defeats the purpose of structured logging.
Maybe this was answered before therefore let me know so I will close the discussion, but couldn't find anything. Thank you all for you input.

Here is an example with the context vars put into "message" field, but I would rather make the richHandler to print them but keep them in some different field.
Beta Was this translation helpful? Give feedback.
All reactions