You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Marceau Dewilde <ma...@duo.be> on 2015/09/24 10:51:47 UTC
TriggeringEventEvaluator alternative for Log4j2
In Log4j 1.2, we use a custom TriggeringEventEvaluator implementation on
our SMTPAppender to bundle log events into email.
Our use case is to be notified only of messages of the ERROR level, and
only on every 20th error. Each log email would then contain 20 ERROR
messages.
In Log4j 2, it is not immediately clear what the alternative is.
As has been noted in other places, the BufferSize property seems to
buffer only events that are below the ERROR level.
Filters would seem to address this need in Log4j 2, but the following
implementation does not do what we require:
@Plugin(name = "ErrorGatheringFilter", category = "Core",
elementType = "filter", printObject = true)
public class GatheringFilter extends AbstractFilter {
private static final long serialVersionUID = 1441897450897L;
private final int number;
private final AtomicInteger count;
private GatheringFilter(int number) {
this.count = new AtomicInteger(0);
this.number = number;
}
public Result filter() {
count.incrementAndGet();
if (count.compareAndSet(number, 0)) {
return Result.ACCEPT;
} else {
return Result.DENY;
}
}
@Override
public Result filter(LogEvent event) {
return this.filter();
}
@Override
public Result filter(Logger logger, Level level, Marker marker,
Message msg, Throwable t) {
return this.filter();
}
@Override
public Result filter(Logger logger, Level level, Marker marker,
Object msg, Throwable t) {
return this.filter();
}
@Override
public Result filter(Logger logger, Level level, Marker marker,
String msg, Object... params) {
return this.filter();
}
@PluginBuilderFactory
public static Builder newBuilder() {
return new Builder();
}
public static class Builder implements
org.apache.logging.log4j.core.util.Builder<GatheringFilter> {
@PluginBuilderAttribute
private int number = 0;
public void setNumber(int number) {
this.number = number;
}
@Override
public GatheringFilter build() {
return new GatheringFilter(number);
}
}
}
Re: TriggeringEventEvaluator alternative for Log4j2
Posted by Ralph Goers <ra...@dslextreme.com>.
In hindsight I think it was a mistake to use a filter in place of the TriggeringEventEvaluator when SmtpAppender was ported from Log4j 1. The impact of this is that Filters on the SmtpAppender no longer work the way you want.
Your GatheringFilter is going to cause events 1-19 to be buffered and then the 20th event will cause the email to be sent. However, nothing is restricting the logging level of the events that are being stored in the buffer.
Today, the only way to achieve that is to add a logging level to the AppenderRefs that are directing the loggers to the SmtpAppender. If you set it to ERROR then only ERROR and FATAL messages will arrive at the SmtpAppender and your Filter should do what you want.
Ralph
> On Sep 24, 2015, at 1:51 AM, Marceau Dewilde <ma...@duo.be> wrote:
>
> In Log4j 1.2, we use a custom TriggeringEventEvaluator implementation on our SMTPAppender to bundle log events into email.
>
> Our use case is to be notified only of messages of the ERROR level, and only on every 20th error. Each log email would then contain 20 ERROR messages.
>
> In Log4j 2, it is not immediately clear what the alternative is.
>
> As has been noted in other places, the BufferSize property seems to buffer only events that are below the ERROR level.
>
> Filters would seem to address this need in Log4j 2, but the following implementation does not do what we require:
>
> @Plugin(name = "ErrorGatheringFilter", category = "Core",
> elementType = "filter", printObject = true)
> public class GatheringFilter extends AbstractFilter {
>
> private static final long serialVersionUID = 1441897450897L;
>
> private final int number;
> private final AtomicInteger count;
>
> private GatheringFilter(int number) {
> this.count = new AtomicInteger(0);
> this.number = number;
> }
>
> public Result filter() {
> count.incrementAndGet();
> if (count.compareAndSet(number, 0)) {
> return Result.ACCEPT;
> } else {
> return Result.DENY;
> }
> }
>
> @Override
> public Result filter(LogEvent event) {
> return this.filter();
> }
>
> @Override
> public Result filter(Logger logger, Level level, Marker marker,
> Message msg, Throwable t) {
> return this.filter();
> }
>
> @Override
> public Result filter(Logger logger, Level level, Marker marker,
> Object msg, Throwable t) {
> return this.filter();
> }
>
> @Override
> public Result filter(Logger logger, Level level, Marker marker,
> String msg, Object... params) {
> return this.filter();
> }
>
> @PluginBuilderFactory
> public static Builder newBuilder() {
> return new Builder();
> }
>
> public static class Builder implements
> org.apache.logging.log4j.core.util.Builder<GatheringFilter> {
>
> @PluginBuilderAttribute
> private int number = 0;
>
> public void setNumber(int number) {
> this.number = number;
> }
>
> @Override
> public GatheringFilter build() {
> return new GatheringFilter(number);
> }
> }
>
> }
>
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org