Skip to content

Instantly share code, notes, and snippets.

@composite
Created February 13, 2020 09:29
Show Gist options
  • Select an option

  • Save composite/1791554f24f76766c54bca3b25cafa0c to your computer and use it in GitHub Desktop.

Select an option

Save composite/1791554f24f76766c54bca3b25cafa0c to your computer and use it in GitHub Desktop.
Logback Appender filter with Spring EL based evaluator. If you are using Spring, no other dependency required.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator class="your.logging.util.SpelLoggerEvaluator">
<expression>
#logger.startsWith("your.logger.name.startswith.here") || level.toInt() >= #INFO.toInt()
</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>NEUTRAL</OnMatch>
</filter>
<encoder>
<pattern>
[%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n
</pattern>
</encoder>
</appender>
</configuration>
package your.logging.util;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.boolex.EventEvaluatorBase;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class SpelLoggerEvaluator extends EventEvaluatorBase<ILoggingEvent> {
final ExpressionParser parser = new SpelExpressionParser();
String expression;
Expression result;
public String getExpression() {
return expression;
}
public void setExpression(String expression) {
this.expression = expression;
}
public void start() {
int errors = 0;
if (expression == null || expression.length() == 0) {
addError("Empty expression");
return;
} else {
addInfo("Expression to evaluate [" + expression + "] with Spring EL");
}
try {
result = parser.parseExpression(expression);
} catch (ParseException e) {
addError("Failed to compile expression [" + expression + "] with Spring EL", e);
errors++;
}
if (errors == 0)
super.start();
}
public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
if (result == null) {
return false;
}
StandardEvaluationContext context = new StandardEvaluationContext(event);
context.setVariable("logger", event.getLoggerName());
context.setVariable("context", event.getLoggerContextVO());
context.setVariable("event", event);
context.setVariable("TRACE", Level.TRACE);
context.setVariable("DEBUG", Level.DEBUG);
context.setVariable("INFO", Level.INFO);
context.setVariable("WARN", Level.WARN);
context.setVariable("ERROR", Level.ERROR);
context.setVariable("ALL", Level.ALL);
context.setVariable("OFF", Level.OFF);
return result.getValue(context, Boolean.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment