OTP Logger: Per‑module filter not updating after handler reload
0 reputation · 09 May 2022, 16:34 UTC
0 reputation · 09 May 2022, 16:34 UTC
Since OTP 21, Logger supports custom filter modules that can be attached per handler. The handler’s filter is set to the module’s return value for each log message, allowing fine‑grained suppression of noise.
When a filter module is edited and recompiled, the usual approach is to call logger:reload_handler/2 to apply the changes. However, the OTP documentation does not clarify whether the reload picks up the new module code or if the handler still references the old bytecode until a full node restart.
Additionally, the coexistence of the legacy error_logger can produce duplicate messages if both systems are active, further complicating noise reduction.
Given these uncertainties, we need to know how to reliably update filter logic at runtime and avoid silent loss of critical alerts.
logger:reload_handler/2 load the new code, or must the node be restarted?error_logger?34775 reputation · 09 May 2022, 21:27 UTC
logger:reload_handler/2 does not load recompiled filter code from disk. It reapplies the handler configuration already stored in Logger, so a recompiled filter module keeps executing the previously loaded bytecode until you explicitly load the new beam and reattach the filter. A node restart is not required.
The handler invokes a filter fun captured when the handler was configured. After recompilation, running Erlang processes continue with the old beam unless the code server loads the new one. Reloading the handler without reloading the module therefore reinstalls the old filter fun.
code:load_abs("/path/to/your_filter.beam").
code:which(your_filter).
logger:remove_handler_filter(HandlerId, your_filter),
logger:add_handler_filter(HandlerId, your_filter, #{}).
Or rewrite the handler config and reload:
logger:set_handler_config(HandlerId, #{filter => {your_filter, []}}),
logger:reload_handler(HandlerId, #{filter => {your_filter, []}}).
If alerts appear twice, the legacy error_logger tty handler is likely still active alongside Logger. Confirm whether the node was started interactively or embedded; interactive starts usually enable the legacy tty sink. Disable the legacy sink only after verifying Logger captures every alert severity you need.
Assumption: this behavior applies to OTP 21 and later. Verify on your target release before applying to production.
Missing diagnostic detail: Is the legacy error_logger tty handler still active on your node? This determines whether you must suppress it to eliminate duplicates.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.