-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Log stdlib's logging extra
-s
#209
Comments
To double-check, this is about pumping stdlib logs through structlog, correct? I guess that shouldn't be too hard to implement. |
I'd love to see this go further than just having stdlib logs through structlog. I'd really love it if I could tell structlog to use the extras dictionary for its own extra data, almost as a stdlib in structlog clothing. But I'm guessing this is probably not a workable option for many reasons. |
Really? Why isn't logging structured logs through stdlib supported by structlog? I assumed that it would work with nothing mentioned in the documentation. |
So I've looked at this and it…interesting. You'd think you can access This means there is no way to know what extra attributes have been added. There's two ways to go around this:
Both are already possible – here's an example for #2: def copy_record(_, __, ed):
ignores = ["msg", "message", "args"]
for k, v in ed["_record"].__dict__.items():
if k in ignores:
continue
ed[k] = v
return ed It removes "msg" and "message" because they're redundant and copies it over into the event dict:
I guess I could add something like |
Full example: from __future__ import annotations
import logging
import structlog
def copy_record(_, __, ed):
ignores = ["msg", "message", "args"]
for k, v in ed["_record"].__dict__.items():
if k in ignores:
continue
ed[k] = v
return ed
shared_processors: list[structlog.types.Processor] = [
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso", utc=True),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
]
formatter = structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=[*shared_processors, copy_record], # <----
processor=structlog.processors.KeyValueRenderer(),
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(logging.INFO)
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
*shared_processors,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
logger = structlog.get_logger()
structlog.get_logger().info("from structlog", foo=42)
logging.getLogger().info("from %s", "stdlib", extra={"foo": 42}) |
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes hynek#209
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes hynek#209
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes hynek#209
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes hynek#209
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes hynek#209
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes hynek#209
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes hynek#209
This processor function will add extra attributres from LogRecord objects to the event_dict, and can be useful for making values passed in the extra parameter of the logging module's log methods pass through to output as additional properties. Fixes #209
Quite many people use python's stdlib logging with extras, say,
logger.info("message", extra={'a': 'b'})
. It's quite obvious how to format these messages in a customer way with stdlib, and many formatters (say,LogstashV1Formatter
) render them by default. However, these are ignored when the rest of the logging and rendering is done via structlog.Would be great to have the extra dictionary contents from stdlib logging passed to structlog, so
and
would produce equivalent results.
Typical use-case:
The text was updated successfully, but these errors were encountered: