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    protected Marker marker;
040    protected String fqcn;
041    protected Level level;
042    protected Message message;
043    protected Throwable thrown;
044    private Map<String, String> contextMap;
045    private ContextStack contextStack;
046    private long threadId = Thread.currentThread().getId();
047    private String threadName = Thread.currentThread().getName();
048    private int threadPriority = Thread.currentThread().getPriority();
049    private StackTraceElement location;
050    private long currentTimeMillis;
051    private long nanoTime;
052
053    // @Override
054    @Override
055    public void translateTo(final RingBufferLogEvent event, final long sequence) {
056        event.setValues(asyncLogger, loggerName, marker, fqcn, level, message, thrown, contextMap, contextStack,
057                threadId, threadName, threadPriority, location, currentTimeMillis, nanoTime);
058        clear();
059    }
060
061    /**
062     * Release references held by this object to allow objects to be garbage-collected.
063     */
064    private void clear() {
065        setBasicValues(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, // location
075                0, // currentTimeMillis
076                0 // nanoTime
077        );
078    }
079
080    public void setBasicValues(final AsyncLogger anAsyncLogger, final String aLoggerName, final Marker aMarker,
081            final String theFqcn, final Level aLevel, final Message msg, final Throwable aThrowable,
082            final Map<String, String> aMap, final ContextStack aContextStack, final StackTraceElement aLocation,
083            final long aCurrentTimeMillis, final long aNanoTime) {
084        this.asyncLogger = anAsyncLogger;
085        this.loggerName = aLoggerName;
086        this.marker = aMarker;
087        this.fqcn = theFqcn;
088        this.level = aLevel;
089        this.message = msg;
090        this.thrown = aThrowable;
091        this.contextMap = aMap;
092        this.contextStack = aContextStack;
093        this.location = aLocation;
094        this.currentTimeMillis = aCurrentTimeMillis;
095        this.nanoTime = aNanoTime;
096    }
097
098    public void updateThreadValues() {
099        final Thread currentThread = Thread.currentThread();
100        this.threadId = currentThread.getId();
101        this.threadName = currentThread.getName();
102        this.threadPriority = currentThread.getPriority();
103    }
104}