Java JaninoEventEvaluatorBase-class And Method Code Example


Here's an example of how to use the JaninoEventEvaluatorBase class from the Logback library to implement an event evaluator:

import ch.qos.logback.core.boolex.JaninoEventEvaluator;
import ch.qos.logback.core.spi.ContextAwareBase;

public class MyJaninoEventEvaluator extends JaninoEventEvaluator {

  private static final String EXPRESSION = "return event.getLevel() == ch.qos.logback.classic.Level.ERROR;";

  public MyJaninoEventEvaluator() {
    super();
    setExpression(EXPRESSION);
    start();
  }

  @Override
  public boolean evaluate(Object event) {
    return super.evaluate(event);
  }
}

In this example, MyJaninoEventEvaluator extends JaninoEventEvaluatorBase and overrides the evaluate method. It also sets a expression "return event.getLevel() == ch.qos.logback.classic.Level.ERROR;" to only evaluate events with ERROR level.

You can customize this expression to fit your needs.

You can use this evaluator with a Appender, like this:

<appender name="MYAPPENDER" class="ch.qos.logback.core.ConsoleAppender">
    <evaluator class="com.example.MyJaninoEventEvaluator"/>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

It's important to notice that this is just one example of how the JaninoEventEvaluatorBase can be used and is just one of several evaluators provided by logback.

And the JaninoEventEvaluator in particular, uses the Janino compiler to evaluate the expressions.