You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Tukesh (Jira)" <ji...@apache.org> on 2022/03/12 19:54:00 UTC
[jira] [Comment Edited] (LOG4J2-3431) log4j bridge API :- Log message filtering based on UtilLoggingLevel fails and all messages are written into log files
[ https://issues.apache.org/jira/browse/LOG4J2-3431?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17505348#comment-17505348 ]
Tukesh edited comment on LOG4J2-3431 at 3/12/22, 7:53 PM:
----------------------------------------------------------
In OptionConverter.java
replacing convertLevel(final Level level) with following implementation solves the problem of custom level/s filtering. I am assuming user has created log2-x level objects of each log4j1-x custom levels using "org.apache.logging.log4j.Level.forName()" as mentioned by Ragini in comments.
public static org.apache.logging.log4j.Level convertLevel(final Level level)
{ return org.apache.logging.log4j.Level.forName(level.toString(),level.toInt()); }
Do you agree with this change ? If yes then can you please commit this change.
was (Author: JIRAUSER282001):
Following implementation of convertLevel resolves the problem of custom levels in bridge API.
public static org.apache.logging.log4j.Level convertLevel(final Level level) {
return org.apache.logging.log4j.Level.forName(level.toString(),level.toInt());
}
> log4j bridge API :- Log message filtering based on UtilLoggingLevel fails and all messages are written into log files
> ---------------------------------------------------------------------------------------------------------------------
>
> Key: LOG4J2-3431
> URL: https://issues.apache.org/jira/browse/LOG4J2-3431
> Project: Log4j 2
> Issue Type: Bug
> Environment: 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
>
>
> Reporter: Tukesh
> Assignee: Piotr Karwasz
> Priority: Major
>
> log4j bridge API :- Log message filtering based on UtilLoggingLevel fails and all messages are written into log files
>
> {code:java}
> // 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();
> }
> }
> }
> } {code}
> {code:java}
> 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
> {code}
>
>
> +*Investigation :-*+
> convertLevel function from OptionConverter.java doesn't consider UtilLoggingLevel level for mapping. so it sets Level as DEBUG for UtilLoggingLevel classes.
> {code:java}
> // 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;
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)