001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.async;
018
019import java.util.Map;
020
021import org.apache.logging.log4j.Level;
022import org.apache.logging.log4j.Marker;
023import org.apache.logging.log4j.ThreadContext.ContextStack;
024import org.apache.logging.log4j.message.Message;
025
026import com.lmax.disruptor.EventTranslator;
027
028/**
029 * This class is responsible for writing elements that make up a log event into
030 * the ringbuffer {@code RingBufferLogEvent}. After this translator populated
031 * the ringbuffer event, the disruptor will update the sequence number so that
032 * the event can be consumed by another thread.
033 */
034public class RingBufferLogEventTranslator implements
035        EventTranslator<RingBufferLogEvent> {
036
037    private AsyncLogger asyncLogger;
038    private String loggerName;
039    private Marker marker;
040    private String fqcn;
041    private Level level;
042    private Message message;
043    private Throwable thrown;
044    private Map<String, String> contextMap;
045    private ContextStack contextStack;
046    private String threadName;
047    private StackTraceElement location;
048    private long currentTimeMillis;
049    private long nanoTime;
050
051    // @Override
052    @Override
053    public void translateTo(final RingBufferLogEvent event, final long sequence) {
054        event.setValues(asyncLogger, loggerName, marker, fqcn, level, message,
055                thrown, contextMap, contextStack, threadName, location,
056                currentTimeMillis, nanoTime);
057        clear();
058    }
059
060    /**
061     * Release references held by this object to allow objects to be
062     * garbage-collected.
063     */
064    private void clear() {
065        setValues(null, // asyncLogger
066                null, // loggerName
067                null, // marker
068                null, // fqcn
069                null, // level
070                null, // data
071                null, // t
072                null, // map
073                null, // contextStack
074                null, // threadName
075                null, // location
076                0, // currentTimeMillis
077                0 // nanoTime
078        );
079    }
080
081    public void setValues(final AsyncLogger anAsyncLogger, final String aLoggerName, final Marker aMarker,
082            final String theFqcn, final Level aLevel, final Message msg, final Throwable aThrowable,
083            final Map<String, String> aMap, final ContextStack aContextStack, final String aThreadName,
084            final StackTraceElement aLocation, final long aCurrentTimeMillis, final long aNanoTime) {
085        this.asyncLogger = anAsyncLogger;
086        this.loggerName = aLoggerName;
087        this.marker = aMarker;
088        this.fqcn = theFqcn;
089        this.level = aLevel;
090        this.message = msg;
091        this.thrown = aThrowable;
092        this.contextMap = aMap;
093        this.contextStack = aContextStack;
094        this.threadName = aThreadName;
095        this.location = aLocation;
096        this.currentTimeMillis = aCurrentTimeMillis;
097        this.nanoTime = aNanoTime;
098    }
099
100    public void setValuesPart1(final AsyncLogger anAsyncLogger, final String aLoggerName, final Marker aMarker,
101            final String theFqcn, final Level aLevel, final Message msg, final Throwable aThrowable) {
102        this.asyncLogger = anAsyncLogger;
103        this.loggerName = aLoggerName;
104        this.marker = aMarker;
105        this.fqcn = theFqcn;
106        this.level = aLevel;
107        this.message = msg;
108        this.thrown = aThrowable;
109    }
110
111    public void setValuesPart2(final Map<String, String> aMap, final ContextStack aContextStack, final String aThreadName,
112            final StackTraceElement aLocation, final long aCurrentTimeMillis, final long aNanoTime) {
113        this.contextMap = aMap;
114        this.contextStack = aContextStack;
115        this.threadName = aThreadName;
116        this.location = aLocation;
117        this.currentTimeMillis = aCurrentTimeMillis;
118        this.nanoTime = aNanoTime;
119    }
120}