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 java.util.Map;
20  
21  import org.apache.logging.log4j.Level;
22  import org.apache.logging.log4j.Marker;
23  import org.apache.logging.log4j.ThreadContext.ContextStack;
24  import org.apache.logging.log4j.message.Message;
25  
26  import com.lmax.disruptor.EventTranslator;
27  
28  /**
29   * This class is responsible for writing elements that make up a log event into
30   * the ringbuffer {@code RingBufferLogEvent}. After this translator populated
31   * the ringbuffer event, the disruptor will update the sequence number so that
32   * the event can be consumed by another thread.
33   */
34  public class RingBufferLogEventTranslator implements
35          EventTranslator<RingBufferLogEvent> {
36  
37      private AsyncLogger asyncLogger;
38      private String loggerName;
39      protected Marker marker;
40      protected String fqcn;
41      protected Level level;
42      protected Message message;
43      protected Throwable thrown;
44      private Map<String, String> contextMap;
45      private ContextStack contextStack;
46      private long threadId = Thread.currentThread().getId();
47      private String threadName = Thread.currentThread().getName();
48      private int threadPriority = Thread.currentThread().getPriority();
49      private StackTraceElement location;
50      private long currentTimeMillis;
51      private long nanoTime;
52  
53      // @Override
54      @Override
55      public void translateTo(final RingBufferLogEvent event, final long sequence) {
56          event.setValues(asyncLogger, loggerName, marker, fqcn, level, message, thrown, contextMap, contextStack,
57                  threadId, threadName, threadPriority, location, currentTimeMillis, nanoTime);
58          clear();
59      }
60  
61      /**
62       * Release references held by this object to allow objects to be garbage-collected.
63       */
64      private void clear() {
65          setBasicValues(null, // asyncLogger
66                  null, // loggerName
67                  null, // marker
68                  null, // fqcn
69                  null, // level
70                  null, // data
71                  null, // t
72                  null, // map
73                  null, // contextStack
74                  null, // location
75                  0, // currentTimeMillis
76                  0 // nanoTime
77          );
78      }
79  
80      public void setBasicValues(final AsyncLogger anAsyncLogger, final String aLoggerName, final Marker aMarker,
81              final String theFqcn, final Level aLevel, final Message msg, final Throwable aThrowable,
82              final Map<String, String> aMap, final ContextStack aContextStack, final StackTraceElement aLocation,
83              final long aCurrentTimeMillis, final long aNanoTime) {
84          this.asyncLogger = anAsyncLogger;
85          this.loggerName = aLoggerName;
86          this.marker = aMarker;
87          this.fqcn = theFqcn;
88          this.level = aLevel;
89          this.message = msg;
90          this.thrown = aThrowable;
91          this.contextMap = aMap;
92          this.contextStack = aContextStack;
93          this.location = aLocation;
94          this.currentTimeMillis = aCurrentTimeMillis;
95          this.nanoTime = aNanoTime;
96      }
97  
98      public void updateThreadValues() {
99          final Thread currentThread = Thread.currentThread();
100         this.threadId = currentThread.getId();
101         this.threadName = currentThread.getName();
102         this.threadPriority = currentThread.getPriority();
103     }
104 }