question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Avoid using log filter to modify/enrich log record

See original GitHub issue

The current implementation provides log filter classes which can be used to enrich the log records with correlation IDs. This is a surprising use of a filter since it has a side effect and modifies a log record. For example, when attaching the filter to one handler, another handler may see the modified log record.

According to the documentation (just below https://docs.python.org/3/library/logging.html?highlight=logrecord#logging.LogRecord.getMessage), the intended pattern for custom log record generation is to use a modified log record factory function like this:

old_factory = logging.getLogRecordFactory()

def record_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.custom_attribute = 0xdecafbad
    return record

logging.setLogRecordFactory(record_factory)

The only annoying thing here is that this one-off setting of the factory cannot be done using logging.config.

Short of asking app developers to bootstrap the record factory, manually, there may be an alternative whereby the ASGI middleware could be responsible for this. When using a class-based middleware implementation, the middleware’s __init__() constructor may be able to set the log record factory?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
faphcommented, May 3, 2022

I will still find some time to play with the log record factory. Just to see if we can simplify the log setup.

1reaction
faphcommented, Apr 30, 2022

I guess I’m just thinking out loud whether we can do better than the pragmatic approach of wiring up filters like this…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connecting log data to traces | Dynatrace Docs
Enable/disable log enrichment for a specific technology​​ In the Dynatrace menu, go to Settings and select Preferences > OneAgent features. Filter for enrichment...
Read more >
Python Logging Guide - Best Practices and Hands-on Examples
Beyond logging levels, filters provide a finer means to filter log requests based on the information in a log record, e.g., ignore log...
Read more >
Build queries by using the Logging query language
Select options from filter menus. Write or modify queries by using the query editor. View, edit, or run the queries in the Recent,...
Read more >
Enriching Data with Lookups | Logstash Reference [8.5] - Elastic
The elasticsearch filter copies fields from previous log events in Elasticsearch to current events. The following config shows a complete example of how...
Read more >
Log Management - Datadog Docs
In this situation, you need to choose which logs to send to a log management solution, and which logs to archive. Filtering your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found