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
018package org.apache.logging.log4j.core.async;
019
020import org.apache.logging.log4j.Level;
021import org.apache.logging.log4j.core.LogEvent;
022import org.apache.logging.log4j.core.impl.LogEventFactory;
023import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
024
025/**
026 * Encapsulates the mechanism used to log asynchronously. There is one delegate per configuration, which is shared by
027 * all AsyncLoggerConfig objects in the configuration.
028 */
029public interface AsyncLoggerConfigDelegate {
030
031    /**
032     * Creates and returns a new {@code RingBufferAdmin} that instruments the ringbuffer of this
033     * {@code AsyncLoggerConfig}.
034     *
035     * @param contextName name of the {@code LoggerContext}
036     * @param loggerConfigName name of the logger config
037     * @return the RingBufferAdmin that instruments the ringbuffer
038     */
039    RingBufferAdmin createRingBufferAdmin(final String contextName, final String loggerConfigName);
040
041    /**
042     * Returns the {@code EventRoute} for the event with the specified level.
043     *
044     * @param level the level of the event to log
045     * @return the {@code EventRoute}
046     */
047    EventRoute getEventRoute(final Level level);
048
049    /**
050     * Enqueues the {@link LogEvent} on the mixed configuration ringbuffer.
051     * This method must only be used after {@link #tryEnqueue(LogEvent, AsyncLoggerConfig)} returns <code>false</code>
052     * indicating that the ringbuffer is full, otherwise it may incur unnecessary synchronization.
053     */
054    void enqueueEvent(LogEvent event, AsyncLoggerConfig asyncLoggerConfig);
055
056    boolean tryEnqueue(LogEvent event, AsyncLoggerConfig asyncLoggerConfig);
057
058    /**
059     * Notifies the delegate what LogEventFactory an AsyncLoggerConfig is using, so the delegate can determine
060     * whether to populate the ring buffer with mutable log events or not. This method may be invoced multiple times
061     * for all AsyncLoggerConfigs that use this delegate.
062     *
063     * @param logEventFactory the factory used
064     */
065    void setLogEventFactory(LogEventFactory logEventFactory);
066}