Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Duplicate
-
None
-
None
-
None
-
None
-
JDK 1.8
-Dlog4j1.compatibility=true
- log4j-1.2-api-2.17.2.jar
- log4j-api-2.17.2.jar
- log4j-core-2.17.2.jar
Description
log4j bridge API :- Log message filtering based on UtilLoggingLevel fails and all messages are written into log files
// demo code package test.logger;import org.apache.log4j.Logger; import org.apache.log4j.helpers.UtilLoggingLevel;import java.io.*; import java.sql.SQLException; import java.util.concurrent.TimeUnit; public class LoggerExample{ static Logger log = Logger.getLogger(LoggerExample.class.getName()); public static void main(String[] args)throws IOException,SQLException{ System.out.println("I'm running..."); while(true){ System.out.println("I'm running..."); log.info("Log4j-shell this is a info message"); log.log(UtilLoggingLevel.INFO,"INFO"); log.log(UtilLoggingLevel.FINE,"FINE"); log.log(UtilLoggingLevel.FINER,"FINER"); log.log(UtilLoggingLevel.FINEST,"FINEST"); log.log(UtilLoggingLevel.CONFIG,"CONIG"); try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
log4j.rootLogger=FINE#org.apache.log4j.helpers.UtilLoggingLevel,DMCC_TRACE_LOGGER
log4j.appender.DMCC_TRACE_LOGGER.Threshold=FINE#org.apache.log4j.helpers.UtilLoggingLevel
log4j.appender.DMCC_TRACE_LOGGER=org.apache.log4j.RollingFileAppender log4j.appender.DMCC_TRACE_LOGGER.File=Dmcc-trace.log log4j.appender.DMCC_TRACE_LOGGER.Append=true log4j.appender.DMCC_TRACE_LOGGER.layout=org.apache.log4j.PatternLayout log4j.appender.DMCC_TRACE_LOGGER.layout.ConversionPattern=%d DmccMain[%X{PID}] :%t: %c %-4p - %m%n log4j.appender.DMCC_TRACE_LOGGER.maxFileSize=10MB log4j.appender.DMCC_TRACE_LOGGER.maxBackupIndex=20
Investigation :-
convertLevel function from OptionConverter.java doesn't consider UtilLoggingLevel level for mapping. so it sets Level as DEBUG for UtilLoggingLevel classes.
// code placeholder final org.apache.logging.log4j.Level level = levelStr == null ? org.apache.logging.log4j.Level.ERROR : OptionConverter.convertLevel(levelStr, org.apache.logging.log4j.Level.DEBUG); loggerConfig.setLevel(level); LOGGER.debug("Logger {} level set to {}", loggerName, level); OptionConverter.java :- public static org.apache.logging.log4j.Level convertLevel(final Level level) { if (level == null) { return org.apache.logging.log4j.Level.ERROR; } if (level.isGreaterOrEqual(Level.FATAL)) { return org.apache.logging.log4j.Level.FATAL; } else if (level.isGreaterOrEqual(Level.ERROR)) { return org.apache.logging.log4j.Level.ERROR; } else if (level.isGreaterOrEqual(Level.WARN)) { return org.apache.logging.log4j.Level.WARN; } else if (level.isGreaterOrEqual(Level.INFO)) { return org.apache.logging.log4j.Level.INFO; } else if (level.isGreaterOrEqual(Level.DEBUG)) { return org.apache.logging.log4j.Level.DEBUG; } else if (level.isGreaterOrEqual(Level.TRACE)) { return org.apache.logging.log4j.Level.TRACE; } return org.apache.logging.log4j.Level.ALL; }
Attachments
Issue Links
- duplicates
-
LOG4J2-3419 Unable to create custom logging level using log4j 2 Bridge API
- Closed