title: How to get trace id and span id in the log4j2, in the format [traceId, spanId ]?
body: <p>How to get trace id and span id in the log4j2, not the in the [traceId, spanId ] ?</p>
  <p>Expected Output :
  [APPNAME,5a59b2372d9a3814,5a59b2372d9a3814]</p>
  <p>Actual Output :
  [logLevel=ERROR] -- 2021-01-21 11:30:32,489 +0530 -- http-nio-8080-exec-1 com.springboot.test.aspect.MyAspect -- asnId= - message=&quot;Logging key:&quot;, traceId=f19556b82d98bf86, executionTimeSeconds=23</p>
  <p>My log4j2 : tried adding below commented property but not seeing trace id in logs</p>
  <pre><code>
          &lt;Property name=&quot;LOG_PATTERN&quot;&gt;[logLevel=%-5p] -- %d %d{Z} -- %t %c -- asnId=%X{ASN} - %m%n&lt;/Property&gt;
          &lt;property name=&quot;log.level&quot;&gt;${bundle:DEV:logLevel}&lt;/property&gt;
           &lt;!--  &lt;property name=&quot;rolling.file.encoder.pattern&quot;
                value=&quot;%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %-5level : loggerName=&amp;quot;%logger{36}&amp;quot; threadName=&amp;quot;%thread&amp;quot;  appName=&amp;quot;${springAppName:-}&amp;quot; trace=&amp;quot;%X{X-B3-TraceId:-}&amp;quot; span=&amp;quot;%X{X-B3-SpanId:-}&amp;quot; spanName=&amp;quot;%X{X-Span-Name:-}&amp;quot; parent=&amp;quot;%X{X-B3-ParentSpanId:-}&amp;quot; exportable=&amp;quot;%X{X-Span-Export:-}&amp;quot; pid=&amp;quot;${PID:-}&amp;quot; txnId=&amp;quot;%X{txnId}&amp;quot; %msg%n&quot;/&gt; --&gt;
           &lt;!--   &lt;property name=&quot;PATTERN&quot; value=&quot;%h %l %u [%date{dd/MMM/yyyy:HH:mm:ss.SSS}] &amp;quot;%r&amp;quot; %s %b &amp;quot; &amp;quot;%i{User-Agent}&amp;quot; [trace=%responseHeader{X-B3-TraceId},span=%i{X-B3-SpanId}] %D&quot;/&gt; --&gt;
      &lt;/Properties&gt;
  </code></pre>
  <p>Aspect class :</p>
  <p>In the log of my Aspect class however I can see the trace id</p>
  <pre><code>public class MyAspect {
  
      private final static Logger logger = LogManager.getLogger(MyAspect.class);
      long startTime = new Date().getTime();
      
      @Autowired
      Tracer tracer;
  
        @Around(&quot;allControllerMethods() &amp;&amp; args(..,@annotation(org.springframework...RequestBody) requestBody) &quot;)
        public Object controllerEvents(ProceedingJoinPoint jp, Object requestBody) throws Throwable {
              
           ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
               HttpServletRequest request = attributes.getRequest();
               MethodSignature signature = (MethodSignature) jp.getSignature();
                Method method = signature.getMethod();
                Object resObject = jp.proceed();
           
                logger.error(&quot;traceId&quot;, tracer.currentSpan().context().traceIdString());
  
          }
      }
  </code></pre>
  <p>pom :</p>
  <pre><code>&lt;dependency&gt;
              &lt;groupId&gt;io.zipkin.brave&lt;/groupId&gt;
              &lt;artifactId&gt;brave&lt;/artifactId&gt;
              &lt;version&gt;5.12.6&lt;/version&gt;
          &lt;/dependency&gt; 
           &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-sleuth-zipkin&lt;/artifactId&gt;
              &lt;version&gt;2.2.0.RELEASE&lt;/version&gt;
          &lt;/dependency&gt; 
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
              &lt;version&gt;1.4.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  <p>ConfigHandler :</p>
  <pre><code>    @Bean
      public RestTemplate restTemplate() {
          return new RestTemplate();
      }
  </code></pre>
  <p>Please see that in the test project where I am not using log4j it comes as expected.</p>