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;
019
020import java.io.Serializable;
021import java.util.Map;
022
023import org.apache.logging.log4j.Level;
024import org.apache.logging.log4j.Marker;
025import org.apache.logging.log4j.ThreadContext;
026import org.apache.logging.log4j.core.impl.ThrowableProxy;
027import org.apache.logging.log4j.message.Message;
028import org.apache.logging.log4j.util.ReadOnlyStringMap;
029
030/**
031 * Provides contextual information about a logged message. A LogEvent must be {@link java.io.Serializable} so that it
032 * may be transmitted over a network connection, output in a
033 * {@link org.apache.logging.log4j.core.layout.SerializedLayout}, and many other uses. Besides containing a
034 * {@link org.apache.logging.log4j.message.Message}, a LogEvent has a corresponding
035 * {@link org.apache.logging.log4j.Level} that the message was logged at. If a
036 * {@link org.apache.logging.log4j.Marker} was used, then it is included here. The contents of the
037 * {@link org.apache.logging.log4j.ThreadContext} at the time of the log call are provided via
038 * {@link #getContextMap()} and {@link #getContextStack()}. If a {@link java.lang.Throwable} was included in the log
039 * call, then it is provided via {@link #getThrown()}. When this class is serialized, the attached Throwable will
040 * be wrapped into a {@link org.apache.logging.log4j.core.impl.ThrowableProxy} so that it may be safely serialized
041 * and deserialized properly without causing problems if the exception class is not available on the other end.
042 * <p>
043 * Since version 2.7, {@link #getContextMap()} is deprecated in favor of {@link #getContextData()}, which
044 * can carry both {@code ThreadContext} data as well as other context data supplied by the
045 * {@linkplain org.apache.logging.log4j.core.impl.ContextDataInjectorFactory configured}
046 * {@link ContextDataInjector}.
047 * </p>
048 */
049public interface LogEvent extends Serializable {
050
051    /**
052     * Gets the context map (also know as Mapped Diagnostic Context or MDC).
053     *
054     * @return The context map, never {@code null}.
055     * @deprecated use {@link #getContextData()} instead
056     */
057    @Deprecated
058    Map<String, String> getContextMap();
059
060    /**
061     * Returns the {@code ReadOnlyStringMap} object holding context data key-value pairs.
062     * <p>
063     * Context data (also known as Mapped Diagnostic Context or MDC) is data that is set by the application to be
064     * included in all subsequent log events. The default source for context data is the {@link ThreadContext} (and
065     * <a href="https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution">properties</a>
066     * configured on the Logger that logged the event), but users can configure a custom {@link ContextDataInjector}
067     * to inject key-value pairs from any arbitrary source.
068     *
069     * @return the {@code ReadOnlyStringMap} object holding context data key-value pairs
070     * @see ContextDataInjector
071     * @see ThreadContext
072     * @since 2.7
073     */
074    ReadOnlyStringMap getContextData();
075
076    /**
077     * Gets the context stack (also known as Nested Diagnostic Context or NDC).
078     *
079     * @return The context stack, never {@code null}.
080     */
081    ThreadContext.ContextStack getContextStack();
082
083    /**
084     * Returns the fully qualified class name of the caller of the logging API.
085     *
086     * @return The fully qualified class name of the caller.
087     */
088    String getLoggerFqcn();
089
090    /**
091     * Gets the level.
092     *
093     * @return level.
094     */
095    Level getLevel();
096
097    /**
098     * Gets the logger name.
099     *
100     * @return logger name, may be {@code null}.
101     */
102    String getLoggerName();
103
104    /**
105     * Gets the Marker associated with the event.
106     *
107     * @return Marker or {@code null} if no Marker was defined on this LogEvent
108     */
109    Marker getMarker();
110
111    /**
112     * Gets the message associated with the event.
113     *
114     * @return message.
115     */
116    Message getMessage();
117
118    /**
119     * Gets event time in milliseconds since midnight, January 1, 1970 UTC.
120     *
121     * @return milliseconds since midnight, January 1, 1970 UTC.
122     * @see java.lang.System#currentTimeMillis()
123     */
124    long getTimeMillis();
125
126    /**
127     * Gets the source of logging request.
128     *
129     * @return source of logging request, may be null.
130     */
131    StackTraceElement getSource();
132
133    /**
134     * Gets the thread name.
135     *
136     * @return thread name, may be null.
137     * TODO guess this could go into a thread context object too. (RG) Why?
138     */
139    String getThreadName();
140
141    /**
142     * Gets the thread ID.
143     *
144     * @return thread ID.
145     * @since 2.6
146     */
147    long getThreadId();
148
149    /**
150     * Gets the thread priority.
151     *
152     * @return thread priority.
153     * @since 2.6
154     */
155    int getThreadPriority();
156
157    /**
158     * Gets throwable associated with logging request.
159     *
160     * <p>Convenience method for {@code ThrowableProxy.getThrowable();}</p>
161     *
162     * @return throwable, may be null.
163     */
164    Throwable getThrown();
165
166    /**
167     * Gets throwable proxy associated with logging request.
168     *
169     * @return throwable, may be null.
170     */
171    ThrowableProxy getThrownProxy();
172
173    /**
174     * Returns {@code true} if this event is the last one in a batch, {@code false} otherwise. Used by asynchronous
175     * Loggers and Appenders to signal to buffered downstream components when to flush to disk, as a more efficient
176     * alternative to the {@code immediateFlush=true} configuration.
177     *
178     * @return whether this event is the last one in a batch.
179     */
180    // see also LOG4J2-164
181    boolean isEndOfBatch();
182
183    /**
184     * Returns whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use
185     * this flag to determine whether to take a {@code StackTrace} snapshot or not before handing off this event to
186     * another thread.
187     *
188     * @return {@code true} if the source of the logging request is required downstream, {@code false} otherwise.
189     * @see #getSource()
190     */
191    // see also LOG4J2-153
192    boolean isIncludeLocation();
193
194    /**
195     * Sets whether this event is the last one in a batch. Used by asynchronous Loggers and Appenders to signal to
196     * buffered downstream components when to flush to disk, as a more efficient alternative to the
197     * {@code immediateFlush=true} configuration.
198     *
199     * @param endOfBatch {@code true} if this event is the last one in a batch, {@code false} otherwise.
200     */
201    void setEndOfBatch(boolean endOfBatch);
202
203    /**
204     * Sets whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use
205     * this flag to determine whether to take a {@code StackTrace} snapshot or not before handing off this event to
206     * another thread.
207     *
208     * @param locationRequired {@code true} if the source of the logging request is required downstream, {@code false}
209     *                         otherwise.
210     * @see #getSource()
211     */
212    void setIncludeLocation(boolean locationRequired);
213
214    /**
215     * Returns the value of the running Java Virtual Machine's high-resolution time source when this event was created,
216     * or a dummy value if it is known that this value will not be used downstream.
217     * @return The value of the running Java Virtual Machine's high-resolution time source when this event was created.
218     * @since Log4J 2.4
219     */
220    long getNanoTime();
221}