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.Arrays;
020import java.util.List;
021import java.util.concurrent.TimeUnit;
022
023import org.apache.logging.log4j.Level;
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.core.Core;
026import org.apache.logging.log4j.core.Filter;
027import org.apache.logging.log4j.core.LogEvent;
028import org.apache.logging.log4j.core.config.AppenderRef;
029import org.apache.logging.log4j.core.config.Configuration;
030import org.apache.logging.log4j.core.config.LoggerConfig;
031import org.apache.logging.log4j.core.config.Node;
032import org.apache.logging.log4j.core.config.Property;
033import org.apache.logging.log4j.core.config.plugins.Plugin;
034import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
035import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
036import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
037import org.apache.logging.log4j.core.config.plugins.PluginElement;
038import org.apache.logging.log4j.core.config.plugins.PluginFactory;
039import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
040import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
041import org.apache.logging.log4j.core.util.Booleans;
042import org.apache.logging.log4j.spi.AbstractLogger;
043import org.apache.logging.log4j.util.Strings;
044
045/**
046 * Asynchronous Logger object that is created via configuration and can be
047 * combined with synchronous loggers.
048 * <p>
049 * AsyncLoggerConfig is a logger designed for high throughput and low latency
050 * logging. It does not perform any I/O in the calling (application) thread, but
051 * instead hands off the work to another thread as soon as possible. The actual
052 * logging is performed in the background thread. It uses
053 * <a href="https://lmax-exchange.github.io/disruptor/">LMAX Disruptor</a> for
054 * inter-thread communication.
055 * <p>
056 * To use AsyncLoggerConfig, specify {@code <asyncLogger>} or
057 * {@code <asyncRoot>} in configuration.
058 * <p>
059 * Note that for performance reasons, this logger does not include source
060 * location by default. You need to specify {@code includeLocation="true"} in
061 * the configuration or any %class, %location or %line conversion patterns in
062 * your log4j.xml configuration will produce either a "?" character or no output
063 * at all.
064 * <p>
065 * For best performance, use AsyncLoggerConfig with the RandomAccessFileAppender or
066 * RollingRandomAccessFileAppender, with immediateFlush=false. These appenders have
067 * built-in support for the batching mechanism used by the Disruptor library,
068 * and they will flush to disk at the end of each batch. This means that even
069 * with immediateFlush=false, there will never be any items left in the buffer;
070 * all log events will all be written to disk in a very efficient manner.
071 */
072@Plugin(name = "asyncLogger", category = Node.CATEGORY, printObject = true)
073public class AsyncLoggerConfig extends LoggerConfig {
074
075    @PluginBuilderFactory
076    public static <B extends Builder<B>> B newAsyncBuilder() {
077        return new Builder<B>().asBuilder();
078    }
079
080    public static class Builder<B extends Builder<B>> extends LoggerConfig.Builder<B> {
081
082        @Override
083        public LoggerConfig build() {
084            final String name = getLoggerName().equals(ROOT) ? Strings.EMPTY : getLoggerName();
085            LevelAndRefs container = LoggerConfig.getLevelAndRefs(getLevel(), getRefs(), getLevelAndRefs(),
086                    getConfig());
087            return new AsyncLoggerConfig(name, container.refs,getFilter(), container.level, isAdditivity(),
088                    getProperties(), getConfig(), includeLocation(getIncludeLocation(), getConfig()));
089        }
090    }
091
092    private static final ThreadLocal<Boolean> ASYNC_LOGGER_ENTERED = new ThreadLocal<Boolean>() {
093        @Override
094        protected Boolean initialValue() {
095            return Boolean.FALSE;
096        }
097    };
098
099    private final AsyncLoggerConfigDelegate delegate;
100
101    protected AsyncLoggerConfig(final String name,
102            final List<AppenderRef> appenders, final Filter filter,
103            final Level level, final boolean additive,
104            final Property[] properties, final Configuration config,
105            final boolean includeLocation) {
106        super(name, appenders, filter, level, additive, properties, config,
107                includeLocation);
108        delegate = config.getAsyncLoggerConfigDelegate();
109        delegate.setLogEventFactory(getLogEventFactory());
110    }
111
112    @Override
113    protected void log(final LogEvent event, final LoggerConfigPredicate predicate) {
114        // See LOG4J2-2301
115        if (predicate == LoggerConfigPredicate.ALL &&
116                ASYNC_LOGGER_ENTERED.get() == Boolean.FALSE &&
117                // Optimization: AsyncLoggerConfig is identical to LoggerConfig
118                // when no appenders are present. Avoid splitting for synchronous
119                // and asynchronous execution paths until encountering an
120                // AsyncLoggerConfig with appenders.
121                hasAppenders()) {
122            // This is the first AsnycLoggerConfig encountered by this LogEvent
123            ASYNC_LOGGER_ENTERED.set(Boolean.TRUE);
124            try {
125                // Detect the first time we encounter an AsyncLoggerConfig. We must log
126                // to all non-async loggers first.
127                super.log(event, LoggerConfigPredicate.SYNCHRONOUS_ONLY);
128                // Then pass the event to the background thread where
129                // all async logging is executed. It is important this
130                // happens at most once and after all synchronous loggers
131                // have been invoked, because we lose parameter references
132                // from reusable messages.
133                logToAsyncDelegate(event);
134            } finally {
135                ASYNC_LOGGER_ENTERED.set(Boolean.FALSE);
136            }
137        } else {
138            super.log(event, predicate);
139        }
140    }
141
142    @Override
143    protected void callAppenders(final LogEvent event) {
144        super.callAppenders(event);
145    }
146
147    private void logToAsyncDelegate(final LogEvent event) {
148        if (!isFiltered(event)) {
149            // Passes on the event to a separate thread that will call
150            // asyncCallAppenders(LogEvent).
151            populateLazilyInitializedFields(event);
152            if (!delegate.tryEnqueue(event, this)) {
153                handleQueueFull(event);
154            }
155        }
156    }
157
158    private void handleQueueFull(final LogEvent event) {
159        if (AbstractLogger.getRecursionDepth() > 1) { // LOG4J2-1518, LOG4J2-2031
160            // If queue is full AND we are in a recursive call, call appender directly to prevent deadlock
161            AsyncQueueFullMessageUtil.logWarningToStatusLogger();
162            logToAsyncLoggerConfigsOnCurrentThread(event);
163        } else {
164            // otherwise, we leave it to the user preference
165            final EventRoute eventRoute = delegate.getEventRoute(event.getLevel());
166            eventRoute.logMessage(this, event);
167        }
168    }
169
170    private void populateLazilyInitializedFields(final LogEvent event) {
171        event.getSource();
172        event.getThreadName();
173    }
174
175    void logInBackgroundThread(final LogEvent event) {
176        delegate.enqueueEvent(event, this);
177    }
178
179    /**
180     * Called by AsyncLoggerConfigHelper.RingBufferLog4jEventHandler.
181     *
182     * This method will log the provided event to only configs of type {@link AsyncLoggerConfig} (not
183     * default {@link LoggerConfig} definitions), which will be invoked on the <b>calling thread</b>.
184     */
185    void logToAsyncLoggerConfigsOnCurrentThread(final LogEvent event) {
186        log(event, LoggerConfigPredicate.ASYNCHRONOUS_ONLY);
187    }
188
189    private String displayName() {
190        return LogManager.ROOT_LOGGER_NAME.equals(getName()) ? LoggerConfig.ROOT : getName();
191    }
192
193    @Override
194    public void start() {
195        LOGGER.trace("AsyncLoggerConfig[{}] starting...", displayName());
196        super.start();
197    }
198
199    @Override
200    public boolean stop(final long timeout, final TimeUnit timeUnit) {
201        setStopping();
202        super.stop(timeout, timeUnit, false);
203        LOGGER.trace("AsyncLoggerConfig[{}] stopping...", displayName());
204        setStopped();
205        return true;
206    }
207
208    /**
209     * Creates and returns a new {@code RingBufferAdmin} that instruments the
210     * ringbuffer of this {@code AsyncLoggerConfig}.
211     *
212     * @param contextName name of the {@code LoggerContext}
213     * @return a new {@code RingBufferAdmin} that instruments the ringbuffer
214     */
215    public RingBufferAdmin createRingBufferAdmin(final String contextName) {
216        return delegate.createRingBufferAdmin(contextName, getName());
217    }
218
219    /**
220     * Factory method to create a LoggerConfig.
221     *
222     * @param additivity True if additive, false otherwise.
223     * @param levelName The Level to be associated with the Logger.
224     * @param loggerName The name of the Logger.
225     * @param includeLocation "true" if location should be passed downstream
226     * @param refs An array of Appender names.
227     * @param properties Properties to pass to the Logger.
228     * @param config The Configuration.
229     * @param filter A Filter.
230     * @return A new LoggerConfig.
231     * @deprecated use {@link #createLogger(boolean, Level, String, String, AppenderRef[], Property[], Configuration, Filter)}
232     */
233    @Deprecated
234    public static LoggerConfig createLogger(
235            final String additivity,
236            final String levelName,
237            final String loggerName,
238            final String includeLocation,
239            final AppenderRef[] refs,
240            final Property[] properties,
241            final Configuration config,
242            final Filter filter) {
243        if (loggerName == null) {
244            LOGGER.error("Loggers cannot be configured without a name");
245            return null;
246        }
247
248        final List<AppenderRef> appenderRefs = Arrays.asList(refs);
249        Level level;
250        try {
251            level = Level.toLevel(levelName, Level.ERROR);
252        } catch (final Exception ex) {
253            LOGGER.error(
254                    "Invalid Log level specified: {}. Defaulting to Error",
255                    levelName);
256            level = Level.ERROR;
257        }
258        final String name = loggerName.equals(LoggerConfig.ROOT) ? Strings.EMPTY : loggerName;
259        final boolean additive = Booleans.parseBoolean(additivity, true);
260
261        return new AsyncLoggerConfig(name, appenderRefs, filter, level,
262                additive, properties, config, includeLocation(includeLocation));
263    }
264
265    /**
266     * Factory method to create a LoggerConfig.
267     *
268     * @param additivity True if additive, false otherwise.
269     * @param level The Level to be associated with the Logger.
270     * @param loggerName The name of the Logger.
271     * @param includeLocation "true" if location should be passed downstream
272     * @param refs An array of Appender names.
273     * @param properties Properties to pass to the Logger.
274     * @param config The Configuration.
275     * @param filter A Filter.
276     * @return A new LoggerConfig.
277     * @since 3.0
278     */
279    @Deprecated
280    public static LoggerConfig createLogger(
281            @PluginAttribute(value = "additivity", defaultBoolean = true) final boolean additivity,
282            @PluginAttribute("level") final Level level,
283            @Required(message = "Loggers cannot be configured without a name") @PluginAttribute("name") final String loggerName,
284            @PluginAttribute("includeLocation") final String includeLocation,
285            @PluginElement("AppenderRef") final AppenderRef[] refs,
286            @PluginElement("Properties") final Property[] properties,
287            @PluginConfiguration final Configuration config,
288            @PluginElement("Filter") final Filter filter) {
289        final String name = loggerName.equals(ROOT) ? Strings.EMPTY : loggerName;
290        return new AsyncLoggerConfig(name, Arrays.asList(refs), filter, level, additivity, properties, config,
291                includeLocation(includeLocation));
292    }
293
294    // Note: for asynchronous loggers, includeLocation default is FALSE
295    protected static boolean includeLocation(final String includeLocationConfigValue) {
296        return Boolean.parseBoolean(includeLocationConfigValue);
297    }
298
299    /**
300     * An asynchronous root Logger.
301     */
302    @Plugin(name = "asyncRoot", category = Core.CATEGORY_NAME, printObject = true)
303    public static class RootLogger extends LoggerConfig {
304
305        @PluginBuilderFactory
306        public static <B extends Builder<B>> B newAsyncRootBuilder() {
307            return new Builder<B>().asBuilder();
308        }
309
310        public static class Builder<B extends Builder<B>> extends RootLogger.Builder<B> {
311
312            @Override
313            public LoggerConfig build() {
314                LevelAndRefs container = LoggerConfig.getLevelAndRefs(getLevel(), getRefs(), getLevelAndRefs(),
315                        getConfig());
316                return new AsyncLoggerConfig(LogManager.ROOT_LOGGER_NAME, container.refs, getFilter(), container.level,
317                        isAdditivity(), getProperties(), getConfig(), includeLocation(getIncludeLocation(),
318                        getConfig()));
319            }
320        }
321
322        /**
323         * @deprecated use {@link #createLogger(String, Level, String, AppenderRef[], Property[], Configuration, Filter)}
324         */
325        @Deprecated
326        public static LoggerConfig createLogger(
327                final String additivity,
328                final String levelName,
329                final String includeLocation,
330                final AppenderRef[] refs,
331                final Property[] properties,
332                final Configuration config,
333                final Filter filter) {
334            final List<AppenderRef> appenderRefs = Arrays.asList(refs);
335            Level level = null;
336            try {
337                level = Level.toLevel(levelName, Level.ERROR);
338            } catch (final Exception ex) {
339                LOGGER.error("Invalid Log level specified: {}. Defaulting to Error", levelName);
340                level = Level.ERROR;
341            }
342            final boolean additive = Booleans.parseBoolean(additivity, true);
343            return new AsyncLoggerConfig(LogManager.ROOT_LOGGER_NAME,
344                    appenderRefs, filter, level, additive, properties, config,
345                    AsyncLoggerConfig.includeLocation(includeLocation));
346        }
347
348        /**
349         *
350         */
351        @Deprecated
352        public static LoggerConfig createLogger(
353                @PluginAttribute("additivity") final String additivity,
354                @PluginAttribute("level") final Level level,
355                @PluginAttribute("includeLocation") final String includeLocation,
356                @PluginElement("AppenderRef") final AppenderRef[] refs,
357                @PluginElement("Properties") final Property[] properties,
358                @PluginConfiguration final Configuration config,
359                @PluginElement("Filter") final Filter filter) {
360            final List<AppenderRef> appenderRefs = Arrays.asList(refs);
361            final Level actualLevel = level == null ? Level.ERROR : level;
362            final boolean additive = Booleans.parseBoolean(additivity, true);
363            return new AsyncLoggerConfig(LogManager.ROOT_LOGGER_NAME, appenderRefs, filter, actualLevel, additive,
364                    properties, config, AsyncLoggerConfig.includeLocation(includeLocation));
365        }
366    }
367}