View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.async;
18  
19  import org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Marker;
21  import org.apache.logging.log4j.ThreadContext.ContextStack;
22  import org.apache.logging.log4j.core.ContextDataInjector;
23  import org.apache.logging.log4j.core.impl.ContextDataInjectorFactory;
24  import org.apache.logging.log4j.util.StringMap;
25  import org.apache.logging.log4j.message.Message;
26  
27  import com.lmax.disruptor.EventTranslator;
28  
29  /**
30   * This class is responsible for writing elements that make up a log event into
31   * the ringbuffer {@code RingBufferLogEvent}. After this translator populated
32   * the ringbuffer event, the disruptor will update the sequence number so that
33   * the event can be consumed by another thread.
34   */
35  public class RingBufferLogEventTranslator implements
36          EventTranslator<RingBufferLogEvent> {
37  
38      private final ContextDataInjector injector = ContextDataInjectorFactory.createInjector();
39      private AsyncLogger asyncLogger;
40      String loggerName;
41      protected Marker marker;
42      protected String fqcn;
43      protected Level level;
44      protected Message message;
45      protected Throwable thrown;
46      private ContextStack contextStack;
47      private long threadId = Thread.currentThread().getId();
48      private String threadName = Thread.currentThread().getName();
49      private int threadPriority = Thread.currentThread().getPriority();
50      private StackTraceElement location;
51      private long currentTimeMillis;
52      private long nanoTime;
53  
54      // @Override
55      @Override
56      public void translateTo(final RingBufferLogEvent event, final long sequence) {
57  
58          event.setValues(asyncLogger, loggerName, marker, fqcn, level, message, thrown,
59                  // config properties are taken care of in the EventHandler thread
60                  // in the AsyncLogger#actualAsyncLog method
61                  injector.injectContextData(null, (StringMap) event.getContextData()), contextStack,
62                  threadId, threadName, threadPriority, location, currentTimeMillis, nanoTime);
63  
64          clear(); // clear the translator
65      }
66  
67      /**
68       * Release references held by this object to allow objects to be garbage-collected.
69       */
70      private void clear() {
71          setBasicValues(null, // asyncLogger
72                  null, // loggerName
73                  null, // marker
74                  null, // fqcn
75                  null, // level
76                  null, // data
77                  null, // t
78                  null, // contextStack
79                  null, // location
80                  0, // currentTimeMillis
81                  0 // nanoTime
82          );
83      }
84  
85      public void setBasicValues(final AsyncLogger anAsyncLogger, final String aLoggerName, final Marker aMarker,
86              final String theFqcn, final Level aLevel, final Message msg, final Throwable aThrowable,
87              final ContextStack aContextStack, final StackTraceElement aLocation,
88              final long aCurrentTimeMillis, final long aNanoTime) {
89          this.asyncLogger = anAsyncLogger;
90          this.loggerName = aLoggerName;
91          this.marker = aMarker;
92          this.fqcn = theFqcn;
93          this.level = aLevel;
94          this.message = msg;
95          this.thrown = aThrowable;
96          this.contextStack = aContextStack;
97          this.location = aLocation;
98          this.currentTimeMillis = aCurrentTimeMillis;
99          this.nanoTime = aNanoTime;
100     }
101 
102     public void updateThreadValues() {
103         final Thread currentThread = Thread.currentThread();
104         this.threadId = currentThread.getId();
105         this.threadName = currentThread.getName();
106         this.threadPriority = currentThread.getPriority();
107     }
108 }