In log4j most (if not all) appenders extend AppenderSkeleton which provides a setThreshold(Priority) method for setting the minimal “level” that a message must be set to in order for the appender to log it somewhere.
I want to log messages to different appenders based on the exact level/priority of the message.
For instance, I want DEBUG messages to log to a ConsoleAppender, but nowhere else. I want INFO messages to log to a FileAppender and nowhere else. I want ERROR messages to log to a JMSAppender and nowhere else.
The problem is this setThreshold(Priority) method, which sets the “minimal threshold” necessary to log a message.
Sure, I could set the ConsoleAppender‘s threshold to DEBUG, but since INFO and ERROR messages are “higher” than DEBUG messages, I will also get INFO and ERROR messages sent to the ConsoleAppender as well.
Are there are methods or ways of configuring appender “levels”/priorities exactly or is this minimal threshold my only recourse?
Edit using LevelMatchFilter:
Is this a step in the right direction?
LevelMatchFilter filter = new LevelMatchFilter();
filter.setLevelToMatch(Level.DEBUG.toString());
consoleAppender.addFilter(filter);
Would that code snippet above accomplish the job of making sure the ConsoleAppender logs DEBUGs, and only DEBUGs?
You can attach a LevelMatchFilter to any appender to filter log messages by only the exact level.
From the javadoc:
This is a very simple filter based on level matching.
The filter admits two options
LevelToMatchandAcceptOnMatch. If there is an exact match between the value of theLevelToMatchoption and the level of theLoggingEvent, then thedecide(org.apache.log4j.spi.LoggingEvent)method returnsFilter.ACCEPTin case theAcceptOnMatchoption value is set to true, if it is false thenFilter.DENYis returned. If there is no match,Filter.NEUTRALis returned.