NopeCharon opened a new pull request, #838:
URL: https://github.com/apache/logging-log4cxx/pull/838

   Closes #837
   
   ## Problem
   
   When an error condition repeats in a tight loop, the same message can be 
logged thousands of times per second. That fills the disk or the log shipping 
pipeline, slows the application down and hides the earlier messages that 
explain the cause. The existing filters select events by level, logger, 
location, MDC or message text, so none of them can keep the first occurrences 
while dropping the rest of a flood.
   
   ## Change
   
   A new `log4cxx::filter::BurstFilter` rate limits logging events using a 
token bucket. The option names and defaults match the [Log4j 2 
`BurstFilter`](https://logging.apache.org/log4j/2.x/manual/filters.html#BurstFilter):
   
   | Option | Meaning | Default |
   | --- | --- | --- |
   | `Level` | Events at this level and less severe levels are rate limited. 
More severe events are never dropped. | `WARN` |
   | `Rate` | Tokens added to the bucket per second, i.e. the average number of 
events passed on once a burst is used up | `10` |
   | `MaxBurst` | The bucket capacity, i.e. the maximum number of events passed 
on in a burst | 10 × `Rate` |
   
   - A rate limited event uses one token. `decide()` returns `DENY` when no 
token is available and `NEUTRAL` otherwise, so the remaining filters in the 
chain are still consulted.
   - The bucket is refilled using `std::chrono::steady_clock`, so system clock 
changes do not affect it. Its state is protected by a mutex, so a filter can be 
shared between appenders.
   - `activateOptions()` fills the bucket to its capacity.
   - A `Rate` or `MaxBurst` value that is not a positive integer is ignored 
with a warning in the internal debugging output.
   - The class is registered, so a configuration file can use 
`class="BurstFilter"` (or `org.apache.log4j.filter.BurstFilter`).
   
   ```xml
   <filter class="BurstFilter">
     <param name="Level" value="WARN" />
     <param name="Rate" value="16" />
     <param name="MaxBurst" value="100" />
   </filter>
   ```
   
   Documentation:
   - `BurstFilter` class reference, including the `setOption` table and an XML 
and properties file example
   - A new "Limiting the Rate of Log Output" section on the filters page, and 
an entry in its list of filters
   - A change log entry under the 2.0.0 new features
   
   ## Testing
   
   New `BurstFilterTest` in `filtertests`:
   - `testRegisteredClass`: 
`Class::forName("org.apache.log4j.filter.BurstFilter")` creates a `BurstFilter`
   - `testDefaults`, `testSetOption`: defaults, case-insensitive option names, 
and the default `MaxBurst` follows `Rate` unless it is set explicitly
   - `testInvalidOptions`: zero, negative and non-numeric values leave the 
options unchanged
   - `testMaxBurst`: only `MaxBurst` events of a burst are passed on
   - `testMoreSevereLevelIsNotLimited`: events more severe than `Level` always 
pass and do not use a token
   - `testRefill`: tokens are added over time, capped at `MaxBurst`
   - `testActivateOptionsRefills`: `activateOptions()` refills the bucket to 
the new capacity
   - `testPropertiesConfiguration`: a properties file configured filter limits 
the events reaching an appender
   
   On Ubuntu 24.04 (GCC 13, aarch64), the full `ctest` suite passes (72/72). 
`filtertests` also builds without warnings and passes with 
`-DLOG4CXX_CHAR=wchar_t -DCMAKE_CXX_STANDARD=11 -Wall -Wextra`. I ran it 10 
times on a heavily loaded machine to check the timing-based tests are stable. 
Removing the refill from `tryAcquire()` makes `testRefill` fail.
   
   End to end: a program configured from an XML file (a `FileAppender` with the 
`BurstFilter` above) logged 1000 `WARN` events in a loop and then one `ERROR`. 
After sleeping one second, it logged 1000 `INFO` events. The log file contained 
exactly 100 `WARN` lines (the burst), the `ERROR` line, and 16 `INFO` lines 
(one second of refill at `Rate=16`).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to