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