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.impl;
018
019import java.util.List;
020
021import org.apache.logging.log4j.Level;
022import org.apache.logging.log4j.Marker;
023import org.apache.logging.log4j.ThreadContext;
024import org.apache.logging.log4j.core.LogEvent;
025import org.apache.logging.log4j.core.async.ThreadNameCachingStrategy;
026import org.apache.logging.log4j.core.config.Property;
027import org.apache.logging.log4j.core.util.Clock;
028import org.apache.logging.log4j.core.util.ClockFactory;
029import org.apache.logging.log4j.message.Message;
030import org.apache.logging.log4j.message.TimestampMessage;
031
032/**
033 * Garbage-free LogEventFactory that reuses a single mutable log event.
034 * @since 2.6
035 */
036public class ReusableLogEventFactory implements LogEventFactory {
037    private static final ThreadNameCachingStrategy THREAD_NAME_CACHING_STRATEGY = ThreadNameCachingStrategy.create();
038    private static final Clock CLOCK = ClockFactory.getClock();
039
040    private static ThreadLocal<MutableLogEvent> mutableLogEventThreadLocal = new ThreadLocal<>();
041    /**
042     * Creates a log event.
043     *
044     * @param loggerName The name of the Logger.
045     * @param marker An optional Marker.
046     * @param fqcn The fully qualified class name of the caller.
047     * @param level The event Level.
048     * @param message The Message.
049     * @param properties Properties to be added to the log event.
050     * @param t An optional Throwable.
051     * @return The LogEvent.
052     */
053    @Override
054    public LogEvent createEvent(final String loggerName, final Marker marker,
055                                final String fqcn, final Level level, final Message message,
056                                final List<Property> properties, final Throwable t) {
057        MutableLogEvent result = mutableLogEventThreadLocal.get();
058        if (result == null) {
059            result = new MutableLogEvent();
060
061            // usually no need to re-initialize thread-specific fields since the event is stored in a ThreadLocal
062            result.setThreadId(Thread.currentThread().getId());
063            result.setThreadName(Thread.currentThread().getName()); // Thread.getName() allocates Objects on each call
064            result.setThreadPriority(Thread.currentThread().getPriority());
065            mutableLogEventThreadLocal.set(result);
066        }
067        result.clear(); // ensure any previously cached values (thrownProxy, source, etc.) are cleared
068
069        result.setLoggerName(loggerName);
070        result.setMarker(marker);
071        result.setLoggerFqcn(fqcn);
072        result.setLevel(level == null ? Level.OFF : level);
073        result.setMessage(message);
074        result.setThrown(t);
075        result.setContextMap(Log4jLogEvent.createMap(properties));
076        result.setContextStack(ThreadContext.getDepth() == 0 ? null : ThreadContext.cloneStack());// mutable copy
077        result.setTimeMillis(message instanceof TimestampMessage
078                ? ((TimestampMessage) message).getTimestamp()
079                : CLOCK.currentTimeMillis());
080        result.setNanoTime(Log4jLogEvent.getNanoClock().nanoTime());
081
082        if (THREAD_NAME_CACHING_STRATEGY == ThreadNameCachingStrategy.UNCACHED) {
083            result.setThreadName(Thread.currentThread().getName()); // Thread.getName() allocates Objects on each call
084            result.setThreadPriority(Thread.currentThread().getPriority());
085        }
086        return result;
087    }
088}