Interface LogEvent
-
- All Superinterfaces:
java.io.Serializable
- All Known Implementing Classes:
AbstractJacksonLayout.ReadOnlyLogEventWrapper,AbstractLogEvent,Log4jLogEvent,LogEventJsonMixIn,LogEventWithContextListMixIn,LogEventWrapper,MutableLogEvent,RingBufferLogEvent
public interface LogEvent extends java.io.SerializableProvides contextual information about a logged message. A LogEvent must beSerializableso that it may be transmitted over a network connection, output in aSerializedLayout, and many other uses. Besides containing aMessage, a LogEvent has a correspondingLevelthat the message was logged at. If aMarkerwas used, then it is included here. The contents of theThreadContextat the time of the log call are provided viagetContextMap()andgetContextStack(). If aThrowablewas included in the log call, then it is provided viagetThrown(). When this class is serialized, the attached Throwable will be wrapped into aThrowableProxyso that it may be safely serialized and deserialized properly without causing problems if the exception class is not available on the other end.Since version 2.7,
getContextMap()is deprecated in favor ofgetContextData(), which can carry bothThreadContextdata as well as other context data supplied by the configuredContextDataInjector.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description ReadOnlyStringMapgetContextData()Returns theReadOnlyStringMapobject holding context data key-value pairs.java.util.Map<java.lang.String,java.lang.String>getContextMap()Deprecated.usegetContextData()insteadThreadContext.ContextStackgetContextStack()Gets the context stack (also known as Nested Diagnostic Context or NDC).InstantgetInstant()Returns the Instant when the message was logged.LevelgetLevel()Gets the level.java.lang.StringgetLoggerFqcn()Returns the fully qualified class name of the caller of the logging API.java.lang.StringgetLoggerName()Gets the logger name.MarkergetMarker()Gets the Marker associated with the event.MessagegetMessage()Gets the message associated with the event.longgetNanoTime()Returns the value of the running Java Virtual Machine's high-resolution time source when this event was created, or a dummy value if it is known that this value will not be used downstream.java.lang.StackTraceElementgetSource()Gets the source of logging request.longgetThreadId()Gets the thread ID.java.lang.StringgetThreadName()Gets the thread name.intgetThreadPriority()Gets the thread priority.java.lang.ThrowablegetThrown()Gets throwable associated with logging request.ThrowableProxygetThrownProxy()Gets throwable proxy associated with logging request.longgetTimeMillis()Gets event time in milliseconds since midnight, January 1, 1970 UTC.booleanisEndOfBatch()Returnstrueif this event is the last one in a batch,falseotherwise.booleanisIncludeLocation()Returns whether the source of the logging request is required downstream.voidsetEndOfBatch(boolean endOfBatch)Sets whether this event is the last one in a batch.voidsetIncludeLocation(boolean locationRequired)Sets whether the source of the logging request is required downstream.LogEventtoImmutable()Returns an immutable version of this log event, which MAY BE a copy of this event.
-
-
-
Method Detail
-
toImmutable
LogEvent toImmutable()
Returns an immutable version of this log event, which MAY BE a copy of this event.- Returns:
- an immutable version of this log event
-
getContextMap
@Deprecated java.util.Map<java.lang.String,java.lang.String> getContextMap()
Deprecated.usegetContextData()insteadGets the context map (also know as Mapped Diagnostic Context or MDC).- Returns:
- The context map, never
null.
-
getContextData
ReadOnlyStringMap getContextData()
Returns theReadOnlyStringMapobject holding context data key-value pairs.Context data (also known as Mapped Diagnostic Context or MDC) is data that is set by the application to be included in all subsequent log events. The default source for context data is the
ThreadContext(and properties configured on the Logger that logged the event), but users can configure a customContextDataInjectorto inject key-value pairs from any arbitrary source.- Returns:
- the
ReadOnlyStringMapobject holding context data key-value pairs - Since:
- 2.7
- See Also:
ContextDataInjector,ThreadContext
-
getContextStack
ThreadContext.ContextStack getContextStack()
Gets the context stack (also known as Nested Diagnostic Context or NDC).- Returns:
- The context stack, never
null.
-
getLoggerFqcn
java.lang.String getLoggerFqcn()
Returns the fully qualified class name of the caller of the logging API.- Returns:
- The fully qualified class name of the caller.
-
getLevel
Level getLevel()
Gets the level.- Returns:
- level.
-
getLoggerName
java.lang.String getLoggerName()
Gets the logger name.- Returns:
- logger name, may be
null.
-
getMarker
Marker getMarker()
Gets the Marker associated with the event.- Returns:
- Marker or
nullif no Marker was defined on this LogEvent
-
getMessage
Message getMessage()
Gets the message associated with the event.- Returns:
- message.
-
getTimeMillis
long getTimeMillis()
Gets event time in milliseconds since midnight, January 1, 1970 UTC. UsegetInstant()to get higher precision timestamp information if available on this platform.- Returns:
- the milliseconds component of this log event's timestamp
- See Also:
System.currentTimeMillis()
-
getInstant
Instant getInstant()
Returns the Instant when the message was logged.Caution: if this
LogEventimplementation is mutable and reused for multiple consecutive log messages, then theInstantobject returned by this method is also mutable and reused. Client code should not keep a reference to the returned object but make a copy instead.- Returns:
- the
Instantholding Instant details for this log event - Since:
- 2.11
-
getSource
java.lang.StackTraceElement getSource()
Gets the source of logging request.- Returns:
- source of logging request, may be null.
-
getThreadName
java.lang.String getThreadName()
Gets the thread name.- Returns:
- thread name, may be null. TODO guess this could go into a thread context object too. (RG) Why?
-
getThreadId
long getThreadId()
Gets the thread ID.- Returns:
- thread ID.
- Since:
- 2.6
-
getThreadPriority
int getThreadPriority()
Gets the thread priority.- Returns:
- thread priority.
- Since:
- 2.6
-
getThrown
java.lang.Throwable getThrown()
Gets throwable associated with logging request.Convenience method for
ThrowableProxy.getThrowable();- Returns:
- throwable, may be null.
-
getThrownProxy
ThrowableProxy getThrownProxy()
Gets throwable proxy associated with logging request.- Returns:
- throwable, may be null.
-
isEndOfBatch
boolean isEndOfBatch()
Returnstrueif this event is the last one in a batch,falseotherwise. Used by asynchronous Loggers and Appenders to signal to buffered downstream components when to flush to disk, as a more efficient alternative to theimmediateFlush=trueconfiguration.- Returns:
- whether this event is the last one in a batch.
-
isIncludeLocation
boolean isIncludeLocation()
Returns whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use this flag to determine whether to take aStackTracesnapshot or not before handing off this event to another thread.- Returns:
trueif the source of the logging request is required downstream,falseotherwise.- See Also:
getSource()
-
setEndOfBatch
void setEndOfBatch(boolean endOfBatch)
Sets whether this event is the last one in a batch. Used by asynchronous Loggers and Appenders to signal to buffered downstream components when to flush to disk, as a more efficient alternative to theimmediateFlush=trueconfiguration.- Parameters:
endOfBatch-trueif this event is the last one in a batch,falseotherwise.
-
setIncludeLocation
void setIncludeLocation(boolean locationRequired)
Sets whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use this flag to determine whether to take aStackTracesnapshot or not before handing off this event to another thread.- Parameters:
locationRequired-trueif the source of the logging request is required downstream,falseotherwise.- See Also:
getSource()
-
getNanoTime
long getNanoTime()
Returns the value of the running Java Virtual Machine's high-resolution time source when this event was created, or a dummy value if it is known that this value will not be used downstream.- Returns:
- The value of the running Java Virtual Machine's high-resolution time source when this event was created.
- Since:
- Log4J 2.4
-
-