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      private Marker marker;
40      private String fqcn;
41      private Level level;
42      private Message message;
43      private Throwable thrown;
44      private Map<String, String> contextMap;
45      private ContextStack contextStack;
46      private String threadName;
47      private StackTraceElement location;
48      private long currentTimeMillis;
49      private long nanoTime;
50  
51      // @Override
52      @Override
53      public void translateTo(final RingBufferLogEvent event, final long sequence) {
54          event.setValues(asyncLogger, loggerName, marker, fqcn, level, message,
55                  thrown, contextMap, contextStack, threadName, location,
56                  currentTimeMillis, nanoTime);
57          clear();
58      }
59  
60      /**
61       * Release references held by this object to allow objects to be
62       * garbage-collected.
63       */
64      private void clear() {
65          setValues(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, // threadName
75                  null, // location
76                  0, // currentTimeMillis
77                  0 // nanoTime
78          );
79      }
80  
81      public void setValues(final AsyncLogger asyncLogger, final String loggerName,
82              final Marker marker, final String fqcn, final Level level, final Message message,
83              final Throwable thrown, final Map<String, String> contextMap,
84              final ContextStack contextStack, final String threadName,
85              final StackTraceElement location, final long currentTimeMillis, final long nanoTime) {
86          this.asyncLogger = asyncLogger;
87          this.loggerName = loggerName;
88          this.marker = marker;
89          this.fqcn = fqcn;
90          this.level = level;
91          this.message = message;
92          this.thrown = thrown;
93          this.contextMap = contextMap;
94          this.contextStack = contextStack;
95          this.threadName = threadName;
96          this.location = location;
97          this.currentTimeMillis = currentTimeMillis;
98          this.nanoTime = nanoTime;
99      }
100 }