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     * Returns an immutable version of this log event, which MAY BE a copy of this event.
053     *  
054     * @return an immutable version of this log event
055     */
056    LogEvent toImmutable();
057
058    /**
059     * Gets the context map (also know as Mapped Diagnostic Context or MDC).
060     *
061     * @return The context map, never {@code null}.
062     * @deprecated use {@link #getContextData()} instead
063     */
064    @Deprecated
065    Map<String, String> getContextMap();
066
067    /**
068     * Returns the {@code ReadOnlyStringMap} object holding context data key-value pairs.
069     * <p>
070     * Context data (also known as Mapped Diagnostic Context or MDC) is data that is set by the application to be
071     * included in all subsequent log events. The default source for context data is the {@link ThreadContext} (and
072     * <a href="https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution">properties</a>
073     * configured on the Logger that logged the event), but users can configure a custom {@link ContextDataInjector}
074     * to inject key-value pairs from any arbitrary source.
075     *
076     * @return the {@code ReadOnlyStringMap} object holding context data key-value pairs
077     * @see ContextDataInjector
078     * @see ThreadContext
079     * @since 2.7
080     */
081    ReadOnlyStringMap getContextData();
082
083    /**
084     * Gets the context stack (also known as Nested Diagnostic Context or NDC).
085     *
086     * @return The context stack, never {@code null}.
087     */
088    ThreadContext.ContextStack getContextStack();
089
090    /**
091     * Returns the fully qualified class name of the caller of the logging API.
092     *
093     * @return The fully qualified class name of the caller.
094     */
095    String getLoggerFqcn();
096
097    /**
098     * Gets the level.
099     *
100     * @return level.
101     */
102    Level getLevel();
103
104    /**
105     * Gets the logger name.
106     *
107     * @return logger name, may be {@code null}.
108     */
109    String getLoggerName();
110
111    /**
112     * Gets the Marker associated with the event.
113     *
114     * @return Marker or {@code null} if no Marker was defined on this LogEvent
115     */
116    Marker getMarker();
117
118    /**
119     * Gets the message associated with the event.
120     *
121     * @return message.
122     */
123    Message getMessage();
124
125    /**
126     * Gets event time in milliseconds since midnight, January 1, 1970 UTC.
127     *
128     * @return milliseconds since midnight, January 1, 1970 UTC.
129     * @see java.lang.System#currentTimeMillis()
130     */
131    long getTimeMillis();
132
133    /**
134     * Gets the source of logging request.
135     *
136     * @return source of logging request, may be null.
137     */
138    StackTraceElement getSource();
139
140    /**
141     * Gets the thread name.
142     *
143     * @return thread name, may be null.
144     * TODO guess this could go into a thread context object too. (RG) Why?
145     */
146    String getThreadName();
147
148    /**
149     * Gets the thread ID.
150     *
151     * @return thread ID.
152     * @since 2.6
153     */
154    long getThreadId();
155
156    /**
157     * Gets the thread priority.
158     *
159     * @return thread priority.
160     * @since 2.6
161     */
162    int getThreadPriority();
163
164    /**
165     * Gets throwable associated with logging request.
166     *
167     * <p>Convenience method for {@code ThrowableProxy.getThrowable();}</p>
168     *
169     * @return throwable, may be null.
170     */
171    Throwable getThrown();
172
173    /**
174     * Gets throwable proxy associated with logging request.
175     *
176     * @return throwable, may be null.
177     */
178    ThrowableProxy getThrownProxy();
179
180    /**
181     * Returns {@code true} if this event is the last one in a batch, {@code false} otherwise. Used by asynchronous
182     * Loggers and Appenders to signal to buffered downstream components when to flush to disk, as a more efficient
183     * alternative to the {@code immediateFlush=true} configuration.
184     *
185     * @return whether this event is the last one in a batch.
186     */
187    // see also LOG4J2-164
188    boolean isEndOfBatch();
189
190    /**
191     * Returns whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use
192     * this flag to determine whether to take a {@code StackTrace} snapshot or not before handing off this event to
193     * another thread.
194     *
195     * @return {@code true} if the source of the logging request is required downstream, {@code false} otherwise.
196     * @see #getSource()
197     */
198    // see also LOG4J2-153
199    boolean isIncludeLocation();
200
201    /**
202     * Sets whether this event is the last one in a batch. Used by asynchronous Loggers and Appenders to signal to
203     * buffered downstream components when to flush to disk, as a more efficient alternative to the
204     * {@code immediateFlush=true} configuration.
205     *
206     * @param endOfBatch {@code true} if this event is the last one in a batch, {@code false} otherwise.
207     */
208    void setEndOfBatch(boolean endOfBatch);
209
210    /**
211     * Sets whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use
212     * this flag to determine whether to take a {@code StackTrace} snapshot or not before handing off this event to
213     * another thread.
214     *
215     * @param locationRequired {@code true} if the source of the logging request is required downstream, {@code false}
216     *                         otherwise.
217     * @see #getSource()
218     */
219    void setIncludeLocation(boolean locationRequired);
220
221    /**
222     * Returns the value of the running Java Virtual Machine's high-resolution time source when this event was created,
223     * or a dummy value if it is known that this value will not be used downstream.
224     * @return The value of the running Java Virtual Machine's high-resolution time source when this event was created.
225     * @since Log4J 2.4
226     */
227    long getNanoTime();
228}