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
{{ message }}
Repository navigation
Logging: how to propagate 'extra' passed to stdlib's log methods? #6488
The Python standard library logging module is responsible for how extra is handled: its docs state:
The third keyword argument is extra which can be used to pass a dictionary which is used to populate the dict of the LogRecord created for the logging event with user-defined attributes. These custom attributes can then be used as you like. For example, they could be incorporated into logged messages.
The last bit is handled by the "formatter" using a format string (configured by you) which knows the attribute names. Becuase we don't know the attribute names, there is no safe / clean way to pick them out of the LogRecord's dictionary, which has more than a dozen "standard" attributes (and they vary across Python versions).
The most straightforward way to send structured logs is to use our logger.log_struct, e.g.:
The most straightforward way to send structured logs is to use our logger.log_struct, e.g.:
It is nice, but I want to configure/hack your logger to have behavior similar to Sentry which collect all extra parameters and show them in user interface.
The Python standard library logging module is responsible for how extra is handled: its docs state:
The third keyword argument is extra which can be used to pass a dictionary which is used to populate the dict of the LogRecord created for the logging event with user-defined attributes. These custom attributes can then be used as you like. For example, they could be incorporated into logged messages.
The last bit is handled by the "formatter" using a format string (configured by you) which knows the attribute names. Becuase we don't know the attribute names, there is no safe / clean way to pick them out of the LogRecord's dictionary, which has more than a dozen "standard" attributes (and they vary across Python versions).
Sure there is: create a dummy LogRecord with no extra and then use vars to filter out fields in one that aren't in the other. Without having a stdlib logging compatible adapter this library is awkward to use. It's not a big deal if fields are over-included!
Activity
tseaver commentedon Nov 14, 2018
The Python standard library
loggingmodule is responsible for howextrais handled: its docs state:The last bit is handled by the "formatter" using a format string (configured by you) which knows the attribute names. Becuase we don't know the attribute names, there is no safe / clean way to pick them out of the
LogRecord's dictionary, which has more than a dozen "standard" attributes (and they vary across Python versions).The most straightforward way to send structured logs is to use our
logger.log_struct, e.g.:[-]logging extra[/-][+]Logging: how to propagate 'extra' passed to stdlib's log methods?[/+]iho commentedon Nov 14, 2018
It is nice, but I want to configure/hack your logger to have behavior similar to Sentry which collect all extra parameters and show them in user interface.
tseaver commentedon Dec 7, 2018
This is the same issue then as #6354.
ewhauser commentedon Dec 23, 2018
One option is to replace the current Stackdriver formatter with monkeypatching or your own configuration methods. Here's what I'm using:
nimish commentedon Jun 19, 2020
Sure there is: create a dummy LogRecord with no
extraand then usevarsto filter out fields in one that aren't in the other. Without having a stdlib logging compatible adapter this library is awkward to use. It's not a big deal if fields are over-included!