title: Log42 json logging using slf4j API
body: <p>I am trying figure out JSON logging using log4j2 as documented in <a href="https://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout" rel="nofollow noreferrer">https://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout</a></p>
  
  <p>All works fine except when I am trying to log an object as json by setting <strong>objectMessageAsJsonObject=true</strong> using SLF4J logger object. SLF4j logger simply ignores the object. Whereas when i use an instance of org.apache.logging.log4j.Logger it works.</p>
  
  <p>This feature(Logging an object as json via JSON layout) has been added in Log4j2 with github issue <a href="https://github.com/apache/logging-log4j2/pull/141" rel="nofollow noreferrer">https://github.com/apache/logging-log4j2/pull/141</a></p>
  
  <p>Tried by creating a new instance of org.apache.logging.log4j.message.ObjectMessage and passing to SLF4J , but looks like it simply ignores it.</p>
  
  <p><strong>//THIS DOES NOT WORK</strong></p>
  
  <pre><code>logger_slf4j.info(MarkerFactory.getMarker("SLF4J_LOGGER"), "SLF4J logger log object as json", om);
  </code></pre>
  
  <p><strong>//THIS WORKS</strong></p>
  
  <pre><code>logger_log4j2.info(MarkerManager.getMarker("LOG4J2_LOGGER"), om);
  </code></pre>
  
  <p>More details can be found in code section below.</p>
  
  <p><strong>pom.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;properties&gt;
      &lt;slf4j.version&gt;1.7.28&lt;/slf4j.version&gt;
      &lt;log4j2.version&gt;2.12.1&lt;/log4j2.version&gt;
      &lt;jackson.version&gt;2.9.5&lt;/jackson.version&gt;
  &lt;/properties&gt;
  
  !-- slf4j dependencies --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
      &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
      &lt;version&gt;${slf4j.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;!-- Log4j2 dependencies --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
      &lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
      &lt;version&gt;${log4j2.version}&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
      &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
      &lt;version&gt;${log4j2.version}&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
      &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
      &lt;version&gt;${log4j2.version}&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  
  &lt;!-- This is used by JSONLayout. --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
      &lt;version&gt;${jackson.version}&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>log4j2.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Configuration status="debug" strict="true"&gt;
      &lt;Appenders&gt;
          &lt;Console name="STDOUT" target="SYSTEM_OUT"&gt;
              &lt;JSONLayout compact="true" eventEol="true" objectMessageAsJsonObject="true"/&gt;
          &lt;/Console&gt;
      &lt;/Appenders&gt;
      &lt;loggers&gt;
          &lt;Logger name="com.logging.test" level="all"/&gt;
          &lt;root level="all"&gt;
              &lt;appender-ref ref="STDOUT"/&gt;
          &lt;/root&gt;
      &lt;/loggers&gt;
  &lt;/Configuration&gt;
  
  </code></pre>
  
  <p><strong>User object to be logged as json</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class User implements Serializable {
      private String firstName;
      private String lastName;
  }
  </code></pre>
  
  <p><strong>JUnit</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>
  public class SampleLoggingTest {
   private static final org.apache.logging.log4j.Logger logger_log4j2 = LogManager.getLogger(SampleLoggingTest.class);
  
      private static final org.slf4j.Logger logger_slf4j = LoggerFactory.getLogger(SampleLoggingTest.class);
  
   private static final Marker TEST_MARKER = MarkerFactory.getMarker("SLF_LOG");
  
   @Test
      public void test_logging(){
      User user = new User("john","doe");
      ObjectMessage om=new ObjectMessage(msg);
  
      logger_slf4j.info(MarkerFactory.getMarker("SLF4J_LOGGER"), "SLF4J logger log object as json", om);
      logger_log4j2.info(MarkerManager.getMarker("LOG4J2_LOGGER"), om);
  
      }
  }
  </code></pre>
  
  <p><strong>Expected(Using SLF4J logger):</strong></p>
  
  <pre><code>{"thread":"main","level":"INFO","loggerName":"com.logging.test.SampleLoggingTest","marker":{"name":"SLF4J_LOGGER"},"message":{"firtsName":"john","lastName":"doe"},"endOfBatch":false,"loggerFqcn":"org.apache.logging.slf4j.Log4jLogger","instant":{"epochSecond":1571770198,"nanoOfSecond":746000000},"threadPriority":5,"threadId":1} 
  </code></pre>
  
  <p><strong>Actual(Using SLF4J logger):</strong></p>
  
  <pre><code>{"thread":"main","level":"INFO","loggerName":"com.logging.test.SampleLoggingTest","marker":{"name":"SLF4J_LOGGER"},"message":"Test Message","endOfBatch":false,"loggerFqcn":"org.apache.logging.slf4j.Log4jLogger","instant":{"epochSecond":1571770198,"nanoOfSecond":746000000},"threadPriority":5,"threadId":1} 
  </code></pre>
  
  <p><strong>Expected(Using LOG4J logger):</strong></p>
  
  <pre><code>{"thread":"main","level":"INFO","loggerName":"com.logging.test.SampleLoggingTest","marker":{"name":"LOG42L_LOGGER"},"message":{"firtsName":"john","lastName":"doe"},"endOfBatch":false,"loggerFqcn":"org.apache.logging.slf4j.Log4jLogger","instant":{"epochSecond":1571770198,"nanoOfSecond":746000000},"threadPriority":5,"threadId":1} 
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <ul>
  <li>Is it possible to log an object using log4j2 JSON layout by using an
  SLF4J logger ?? If so any example will be helpful.</li>
  <li>Is there any easier way to change the <strong>instant</strong> to a human readable date and timestamp format ?</li>
  </ul>