Comments (3)
Hmm filter_by_level
makes no sense in a ProcessorFormatter processor, because filtering at this point is done by logging
.
Generally speaking the execution environment within ProcessorFormatter is very different than within structlog proper.
from structlog.
Hi @hynek , thank you very much for the comment.
Could we have an explanation which processors
cannot be used in stdlib
configuration?
Also I have a small confusion between processors
and foreign_pre_chain
? When I should use which?
Example:
COMMON_PRE_CHAIN: list[structlog.typing.Processor] = [
structlog.contextvars.merge_contextvars,
structlog.stdlib.filter_by_level, # <-- this "common" processor is causing issues in stdlib configuration
structlog.processors.TimeStamper(fmt='iso'),
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.StackInfoRenderer(),
structlog.processors.UnicodeDecoder(),
]
LOGGING: dict[str, t.Any] = {
'version': 1,
'disable_existing_loggers': False,
'filters': {'event_blacklist': {'()': 'app.settings.components.DevelopmentFilter'}},
'formatters': {
'plain_console': {
'()': structlog.stdlib.ProcessorFormatter,
'processors': [structlog.stdlib.ProcessorFormatter.remove_processors_meta, structlog.dev.ConsoleRenderer()], # <-- what is the difference between processors here and `foreign_pre_chain` later on? One is used for `structlog` logs and another for `stdlib` logs? We are trying it to keep "the same"
'foreign_pre_chain': COMMON_PRE_CHAIN, # <-- the issue is caused here with the same example
},
},
}
structlog.configure(
processors=[*COMMON_PRE_CHAIN, structlog.stdlib.ProcessorFormatter.wrap_for_formatter],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
With the following configuration we have exactly the same traceback. In the documentation it is stated that you should keep your structlog
configuration and stdlib
logging configuration as close as possible (to have similar logs).
Could you please explain which processors we can't/shouldn't use in foreign_pre_chain
?
from structlog.
structlog.stdlib.ProcessorFormatter's processors
are executed on every log entry that arrives in ProcessorFormatter. That is: log entries coming from logging
and entries that have been looped back into structlog (see also the graph at https://www.structlog.org/en/stable/standard-library.html#rendering-using-structlog-based-formatters-within-logging)
structlog.stdlib.ProcessorFormatter's foreign_pre_chain
is run only on log entries coming from logging
before handing it over to the processors in the previous paragraph. This chain is mostly to add data that you might want to use on processors
like structlog-style timestamps etc.
I cannot answer your question in general because it's complicated. OP's problem was that they used a processor that needed a stdlib logger to get the log level. I'm not even sure what your problem is.
from structlog.
Related Issues (20)
- Version compatibility check for optional dependencies HOT 4
- "exception" is a possible log level from processors.add_log_level since 23.3.0 HOT 10
- `structlog.get_context` breaks from 23.2.0 to 23.3.0 HOT 1
- Add fallback when loading Protocol HOT 1
- Question: how to make timestamps be time-since-application-started? HOT 1
- Unexpected handling of exc_info in structlog v24 when using .exception HOT 1
- merge_contextvars raises a KeyError HOT 28
- Integrating `structlog` with `logot` HOT 3
- Provide an away to turn off padding log level with blanks HOT 6
- `CallsiteParameterAdder` not pickleable HOT 1
- Ignore frames in `_FixedFindCallerLogger` HOT 1
- Why import non-hinted `get_logger` in `__init__.py`? HOT 2
- How to configure different loggers separately? HOT 1
- Processor `logger` argument is None when using Python's logging. HOT 2
- Async logger calls report incorrect call site parameters HOT 1
- structlog+graylog HOT 1
- stacklevel incorrectly passed as extra in structlog.stdlib.render_to_log_kwargs (typo) HOT 2
- Unexpected log level set by structlog.stdlib.recreate_defaults() HOT 2
- Feature request: re-order output columns without explicitly defining them HOT 1
- Git tag 24.2.0 signed with a different key HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from structlog.