hynek / structlog

Simple, powerful, and fast logging for Python.
https://www.structlog.org/
Other
3.49k stars 221 forks source link

DropEvent does not work in foreign_pre_chain #113

Open gilbsgilbs opened 7 years ago

gilbsgilbs commented 7 years ago

Steps to reproduce

  1. Take this example from the doc.
  2. Append a processor that raises DropEvent in pre_chain.
  3. Run the example.

Expected behaviour

The log should be skipped.

Actual behaviour

Raises DropEvent; The exception is not caught.

Comments

I don't know if this is really expected or not. There should be at least a disclaimer in the documentation that states clearly that you shouldn't throw DropEvent in pre_chains processors, and you should instead write a stdlib Filter.

However, I reckon there is a way to handle the DropEvent in pre_chain. We could declare a custom Handler that proxies everything to the StreamHandler (or whatever handler is configured), except that we catch/ignore the DropEvent exception in the overidden emit method. This solution implies a tiny overhead on each log, since we have to forward each log entry to the underlying handler.

mdgilene commented 3 weeks ago

Based upon the closed PR, it seems like this is a "wontfix" issue? If so I still don't see any documentation about ProcessorFormatter not handling DropEvent.

hynek commented 3 weeks ago

I wouldn't call it a wontfix, but the approach in the PR added too much complexity/moving parts. I would love to support this, but it would have to be a simpler approach since the stdlib integration already is a huge pain to maintain and use. Of course, I don't know if such an approach exists.

mdgilene commented 3 weeks ago

It seems like the root of the issue stems from the fact that ProcessorFormatter is implemented as a logging.Formatter. Thus, it has no ability to make decisions about whether a record should be logged at all. In the PR is a suggestion that seems like the most straightforward approach which would be to deprecate the ProcessorFormatter in favor of a ProcessorHandler which can handle everything the formatter was doing but the ability to drop the record entirely if desired.