You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Karol B. (JIRA)" <ji...@apache.org> on 2018/02/06 15:50:00 UTC

[jira] [Created] (LOG4J2-2242) Log4j2 - logger overrides another logger's message

Karol B. created LOG4J2-2242:
--------------------------------

             Summary: Log4j2 - logger overrides another logger's message
                 Key: LOG4J2-2242
                 URL: https://issues.apache.org/jira/browse/LOG4J2-2242
             Project: Log4j 2
          Issue Type: Bug
    Affects Versions: 2.10.0
            Reporter: Karol B.


We are trying to migrate our application from slf4j with log4j to log4j2 (2.10.0). We noticed a problem:

1) We call

LOGGER.warn("Message: object value is {}", object)
 from class A

2) Object overrides toString method invoking static method from other class B

3) Method that generates String representation of object use logger :

LOGGER.warn( "Used ObjectsToStringGenerator for class: {}", o.getClass() )
 We expect logs to looks like:
{code:java}
2018-02-05 06:07:25.233 [main] WARN MainClass - Message: object values is [object string]
 2018-02-05 06:07:25.233 [main] WARN ExampleObject - Used ObjectsToStringGenerator for class: class ExampleObject{code}

 But we get this:
{code:java}
2018-02-05 15:01:43.359 [main] WARN MainClass - Used ObjectsToStringGenerator for class: class ExampleObject[object string]
2018-02-05 15:01:43.373 [main] WARN ObjectsToStringGenerator - ObjectsToStringGenerator for class: ExampleObject{code}

 If we explicity invoke toString method:

LOGGER.warn("Message: object value is {}", object.toString() )
 everything is working as expected, but of course, we would like to avoid explicite use of object.toString(). We tried also using sync loggers/appendrs but we got error:

main ERROR Recursive call to appender CONSOLE.
 Below example code/logger configuration:

MainClass.java:

 
{code:java}
public class MainClass
{
    private static final Logger LOGGER = LogManager.getLogger( MainClass.class );
    public static void main(String[] args) throws InterruptedException {
     //...
      LOGGER.warn( "Message: object value is {}", new ExampleObject() );
      //...
   }
}
{code}

 ExampleObject.java:

 
{code:java}
public class ExampleObject
{
  @Override
  public String toString()
  {
     return ObjectsToStringGenerator.getStringValue( this ); 
  }
}
{code}

 ObjectsToStringGenerator.java:

 
{code:java}
public class ObjectsToStringGenerator
{
   private static final Logger LOGGER = LogManager.getLogger(    ObjectsToStringGenerator.class );
  public static String getStringValue(Object o){
    LOGGER.warn( "Used ObjectsToStringGenerator for class: {}", o.getClass() );
    return "[object string]";
  }
}
{code}

 The configuration file (we tested this also in RollingFile appender and our custom implementation with the same result):

 
{code:java}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
 <Appenders>
   <Console name="CONSOLE" target="SYSTEM_OUT">
     <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}[%t] %p %c{2}- %m%n" />
   </Console>
 </Appenders>
 <Loggers>
    <Root level="debug">
      <AppenderRef ref="CONSOLE" />
    </Root>
 </Loggers>
</Configuration>
{code}
 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)