I've got a couple questions regarding plugins I'm hoping someone might be able
to help me with. I checked the docs and it's still not quite clear.
1. I'm unsure what to set printObject to in my Plugin annotation. From
http://logging.apache.org/log4j/2.x/manual/extending.html it says:
"Specifying the printObject attribute with a value of "true" indicates that a
call to toString will format the arguments to the filter as the configuration
is being processed."
which unfortunately doesn't clear things up any. I looked at the docs for
printObject and that doesn't say anything other than it's a Boolean and its
default is false.
2. I created a LevelRangeFiler and I'm trying to figure out how to get it
loaded by log4j. I read over the instructions on plugins located at
http://logging.apache.org/log4j/2.x/manual/plugins.html but since I'm a java
noob I'm still a bit lost. I'm wondering, if I just have a .java class which I
compile to a .class file, can that be used? If so, how?
Below is the filter I wrote based on looking at the threshold filter example at
http://logging.apache.org/log4j/2.x/manual/extending.html.
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.message.Message;
@Plugin(name="LevelRangeFilter", category="Core", elementType="filter",
printObject=true)
public class LevelRangeFilter extends AbstractFilter
{
private final Level minLevel;
private final Level maxLevel;
public LevelRangeFilter(Level minLevel, Level maxLevel,
Filter.Result onMatch, Filter.Result onMismatch)
{
super(onMatch, onMismatch);
this.minLevel = minLevel;
this.maxLevel = maxLevel;
}
private Result filter(Level level)
{
if (level.isMoreSpecificThan(this.minLevel) &&
level.isLessSpecificThan(this.maxLevel))
{
return this.onMatch;
}
return this.onMismatch;
}
public Filter.Result filter(Logger logger, Level level,
Marker marker, String msg, Object[] params)
{
return filter(level);
}
public Filter.Result filter(Logger logger, Level level,
Marker marker, Object msg, Throwable t)
{
return filter(level);
}
public Filter.Result filter(Logger logger, Level level,
Marker marker, Message msg, Throwable t)
{
return filter(level);
}
public Filter.Result filter(LogEvent event)
{
return filter(event.getLevel());
}
@PluginFactory
public static LevelRangeFilter createFilter(
@PluginAttribute(value="minLevel",
defaultString="DEBUG") Level minLevel,
@PluginAttribute(value="maxLevel",
defaultString="FATAL") Level maxLevel,
@PluginAttribute(value="onMatch",
defaultString="NEUTRAL") Result onMatch,
@PluginAttribute(value="onMismatch",
defaultString="DENY") Result onMismatch)
{
return new LevelRangeFilter(minLevel, maxLevel, onMatch, onMismatch);
}
}
Thanks,
Nick