structlog icon indicating copy to clipboard operation
structlog copied to clipboard

DropEvent does not work in foreign_pre_chain

Open gilbsgilbs opened this issue 8 years ago • 3 comments

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.

gilbsgilbs avatar May 07 '17 15:05 gilbsgilbs

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.

mdgilene avatar Sep 27 '24 20:09 mdgilene

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.

hynek avatar Sep 28 '24 08:09 hynek

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.

mdgilene avatar Sep 28 '24 14:09 mdgilene