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     */
017    package org.apache.logging.log4j.core.async;
018    
019    import java.util.Map;
020    
021    import org.apache.logging.log4j.Level;
022    import org.apache.logging.log4j.Marker;
023    import org.apache.logging.log4j.ThreadContext.ContextStack;
024    import org.apache.logging.log4j.message.Message;
025    
026    import 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     */
034    public 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    
050        // @Override
051        @Override
052        public void translateTo(final RingBufferLogEvent event, final long sequence) {
053            event.setValues(asyncLogger, loggerName, marker, fqcn, level, message,
054                    thrown, contextMap, contextStack, threadName, location,
055                    currentTimeMillis);
056            clear();
057        }
058    
059        /**
060         * Release references held by this object to allow objects to be
061         * garbage-collected.
062         */
063        private void clear() {
064            setValues(null, // asyncLogger
065                    null, // loggerName
066                    null, // marker
067                    null, // fqcn
068                    null, // level
069                    null, // data
070                    null, // t
071                    null, // map
072                    null, // contextStack
073                    null, // threadName
074                    null, // location
075                    0 // currentTimeMillis
076            );
077        }
078    
079        public void setValues(final AsyncLogger asyncLogger, final String loggerName,
080                final Marker marker, final String fqcn, final Level level, final Message message,
081                final Throwable thrown, final Map<String, String> contextMap,
082                final ContextStack contextStack, final String threadName,
083                final StackTraceElement location, final long currentTimeMillis) {
084            this.asyncLogger = asyncLogger;
085            this.loggerName = loggerName;
086            this.marker = marker;
087            this.fqcn = fqcn;
088            this.level = level;
089            this.message = message;
090            this.thrown = thrown;
091            this.contextMap = contextMap;
092            this.contextStack = contextStack;
093            this.threadName = threadName;
094            this.location = location;
095            this.currentTimeMillis = currentTimeMillis;
096        }
097    
098    }