Class Log4jLogEvent
- java.lang.Object
-
- org.apache.logging.log4j.core.impl.Log4jLogEvent
-
- All Implemented Interfaces:
java.io.Serializable
,LogEvent
public class Log4jLogEvent extends java.lang.Object implements LogEvent
Implementation of a LogEvent.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Log4jLogEvent.Builder
LogEvent Builder helper class.(package private) static class
Log4jLogEvent.LogEventProxy
Proxy pattern used to serialize the LogEvent.
-
Field Summary
Fields Modifier and Type Field Description private static Clock
CLOCK
private static ContextDataInjector
CONTEXT_DATA_INJECTOR
private StringMap
contextData
private ThreadContext.ContextStack
contextStack
private boolean
endOfBatch
private boolean
includeLocation
private MutableInstant
instant
private Level
level
private java.lang.String
loggerFqcn
private java.lang.String
loggerName
private Marker
marker
private Message
message
private static NanoClock
nanoClock
private long
nanoTime
private static long
serialVersionUID
private java.lang.StackTraceElement
source
private long
threadId
private java.lang.String
threadName
private int
threadPriority
private java.lang.Throwable
thrown
private ThrowableProxy
thrownProxy
-
Constructor Summary
Constructors Modifier Constructor Description Log4jLogEvent()
Log4jLogEvent(long timestamp)
Deprecated.useLog4jLogEvent.Builder
instead.Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, java.lang.StackTraceElement source, Level level, Message message, java.util.List<Property> properties, java.lang.Throwable t)
Constructor.Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable t)
Deprecated.useLog4jLogEvent.Builder
instead.Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable t, java.util.Map<java.lang.String,java.lang.String> mdc, ThreadContext.ContextStack ndc, java.lang.String threadName, java.lang.StackTraceElement location, long timestampMillis)
Deprecated.useLog4jLogEvent.Builder
instead.private
Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, StringMap contextData, ThreadContext.ContextStack contextStack, long threadId, java.lang.String threadName, int threadPriority, java.lang.StackTraceElement source, long nanoTime)
private
Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, StringMap contextData, ThreadContext.ContextStack contextStack, long threadId, java.lang.String threadName, int threadPriority, java.lang.StackTraceElement source, long timestampMillis, int nanoOfMillisecond, long nanoTime)
Constructor.private
Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, StringMap contextData, ThreadContext.ContextStack contextStack, long threadId, java.lang.String threadName, int threadPriority, java.lang.StackTraceElement source, Clock clock, long nanoTime)
Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.util.List<Property> properties, java.lang.Throwable t)
Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Log4jLogEvent.Builder
asBuilder()
Returns a new fully initializedLog4jLogEvent.Builder
containing a copy of all fields of this event.static boolean
canDeserialize(java.io.Serializable event)
private static StringMap
createContextData(java.util.List<Property> properties)
private static StringMap
createContextData(java.util.Map<java.lang.String,java.lang.String> contextMap)
static Log4jLogEvent
createEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, java.util.Map<java.lang.String,java.lang.String> mdc, ThreadContext.ContextStack ndc, java.lang.String threadName, java.lang.StackTraceElement location, long timestamp)
Deprecated.useLog4jLogEvent.Builder
instead.static LogEvent
createMemento(LogEvent logEvent)
static Log4jLogEvent
createMemento(LogEvent event, boolean includeLocation)
Creates and returns a new immutable copy of thisLog4jLogEvent
.static Log4jLogEvent
deserialize(java.io.Serializable event)
boolean
equals(java.lang.Object o)
ReadOnlyStringMap
getContextData()
Returns theReadOnlyStringMap
containing context data key-value pairs.java.util.Map<java.lang.String,java.lang.String>
getContextMap()
Returns the immutable copy of the ThreadContext Map.ThreadContext.ContextStack
getContextStack()
Returns an immutable copy of the ThreadContext stack.Instant
getInstant()
Returns the Instant when the message was logged.Level
getLevel()
Returns the logging Level.java.lang.String
getLoggerFqcn()
The fully qualified class name of the class that was called by the caller.java.lang.String
getLoggerName()
Returns the name of the Logger used to generate the event.Marker
getMarker()
Returns the Marker associated with the event, or null.Message
getMessage()
Returns the Message associated with the event.static NanoClock
getNanoClock()
Returns theNanoClock
to use for creating the nanoTime timestamp of log events.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.java.lang.StackTraceElement
getSource()
Returns the StackTraceElement for the caller.long
getThreadId()
Gets the thread ID.java.lang.String
getThreadName()
Returns the name of the Thread on which the event was generated.int
getThreadPriority()
Gets the thread priority.java.lang.Throwable
getThrown()
Returns the Throwable associated with the event, or null.ThrowableProxy
getThrownProxy()
Returns the ThrowableProxy associated with the event, or null.long
getTimeMillis()
Gets event time in milliseconds since midnight, January 1, 1970 UTC.int
hashCode()
boolean
isEndOfBatch()
Returnstrue
if this event is the last one in a batch,false
otherwise.boolean
isIncludeLocation()
Returns whether the source of the logging request is required downstream.void
makeMessageImmutable()
static Log4jLogEvent.Builder
newBuilder()
Returns a new emptyLog4jLogEvent.Builder
with all fields empty.private void
readObject(java.io.ObjectInputStream stream)
static java.io.Serializable
serialize(Log4jLogEvent event, boolean includeLocation)
Take a snapshot of the specifiedLog4jLogEvent
.static java.io.Serializable
serialize(LogEvent event, boolean includeLocation)
Take a snapshot of the specifiedLogEvent
.void
setEndOfBatch(boolean endOfBatch)
Sets whether this event is the last one in a batch.void
setIncludeLocation(boolean includeLocation)
Sets whether the source of the logging request is required downstream.static void
setNanoClock(NanoClock nanoClock)
Sets theNanoClock
to use for creating the nanoTime timestamp of log events.Log4jLogEvent
toImmutable()
Returns an immutable version of this log event, which MAY BE a copy of this event.java.lang.String
toString()
protected java.lang.Object
writeReplace()
Creates a LogEventProxy that can be serialized.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
CLOCK
private static final Clock CLOCK
-
nanoClock
private static volatile NanoClock nanoClock
-
CONTEXT_DATA_INJECTOR
private static final ContextDataInjector CONTEXT_DATA_INJECTOR
-
loggerFqcn
private final java.lang.String loggerFqcn
-
marker
private final Marker marker
-
level
private final Level level
-
loggerName
private final java.lang.String loggerName
-
message
private Message message
-
instant
private final MutableInstant instant
-
thrown
private final transient java.lang.Throwable thrown
-
thrownProxy
private ThrowableProxy thrownProxy
-
contextData
private final StringMap contextData
-
contextStack
private final ThreadContext.ContextStack contextStack
-
threadId
private long threadId
-
threadName
private java.lang.String threadName
-
threadPriority
private int threadPriority
-
source
private java.lang.StackTraceElement source
-
includeLocation
private boolean includeLocation
-
endOfBatch
private boolean endOfBatch
-
nanoTime
private final transient long nanoTime
- Since:
- Log4J 2.4
-
-
Constructor Detail
-
Log4jLogEvent
public Log4jLogEvent()
-
Log4jLogEvent
@Deprecated public Log4jLogEvent(long timestamp)
Deprecated.useLog4jLogEvent.Builder
instead. This constructor will be removed in an upcoming release.
-
Log4jLogEvent
@Deprecated public Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable t)
Deprecated.useLog4jLogEvent.Builder
instead. This constructor will be removed in an upcoming release.Constructor.- Parameters:
loggerName
- The name of the Logger.marker
- The Marker or null.loggerFQCN
- The fully qualified class name of the caller.level
- The logging Level.message
- The Message.t
- A Throwable or null.
-
Log4jLogEvent
public Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.util.List<Property> properties, java.lang.Throwable t)
Constructor.- Parameters:
loggerName
- The name of the Logger.marker
- The Marker or null.loggerFQCN
- The fully qualified class name of the caller.level
- The logging Level.message
- The Message.properties
- the properties to be merged with ThreadContext key-value pairs into the event's ReadOnlyStringMap.t
- A Throwable or null.
-
Log4jLogEvent
public Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, java.lang.StackTraceElement source, Level level, Message message, java.util.List<Property> properties, java.lang.Throwable t)
Constructor.- Parameters:
loggerName
- The name of the Logger.marker
- The Marker or null.loggerFQCN
- The fully qualified class name of the caller.level
- The logging Level.message
- The Message.properties
- the properties to be merged with ThreadContext key-value pairs into the event's ReadOnlyStringMap.t
- A Throwable or null.
-
Log4jLogEvent
@Deprecated public Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable t, java.util.Map<java.lang.String,java.lang.String> mdc, ThreadContext.ContextStack ndc, java.lang.String threadName, java.lang.StackTraceElement location, long timestampMillis)
Deprecated.useLog4jLogEvent.Builder
instead. This constructor will be removed in an upcoming release.Constructor.- Parameters:
loggerName
- The name of the Logger.marker
- The Marker or null.loggerFQCN
- The fully qualified class name of the caller.level
- The logging Level.message
- The Message.t
- A Throwable or null.mdc
- The mapped diagnostic context.ndc
- the nested diagnostic context.threadName
- The name of the thread.location
- The locations of the caller.timestampMillis
- The timestamp of the event.
-
Log4jLogEvent
private Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, StringMap contextData, ThreadContext.ContextStack contextStack, long threadId, java.lang.String threadName, int threadPriority, java.lang.StackTraceElement source, long timestampMillis, int nanoOfMillisecond, long nanoTime)
Constructor.- Parameters:
loggerName
- The name of the Logger.marker
- The Marker or null.loggerFQCN
- The fully qualified class name of the caller.level
- The logging Level.message
- The Message.thrown
- A Throwable or null.thrownProxy
- A ThrowableProxy or null.contextData
- The key-value pairs from the context.contextStack
- the nested diagnostic context.threadId
- the thread IDthreadName
- The name of the thread.threadPriority
- the thread prioritysource
- The locations of the caller.timestampMillis
- The timestamp of the event.nanoOfMillisecond
- the nanoseconds within the millisecond, always positive, never exceeds999,999
nanoTime
- The value of the running Java Virtual Machine's high-resolution time source when the event was created.
-
Log4jLogEvent
private Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, StringMap contextData, ThreadContext.ContextStack contextStack, long threadId, java.lang.String threadName, int threadPriority, java.lang.StackTraceElement source, Clock clock, long nanoTime)
-
Log4jLogEvent
private Log4jLogEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, StringMap contextData, ThreadContext.ContextStack contextStack, long threadId, java.lang.String threadName, int threadPriority, java.lang.StackTraceElement source, long nanoTime)
-
-
Method Detail
-
newBuilder
public static Log4jLogEvent.Builder newBuilder()
Returns a new emptyLog4jLogEvent.Builder
with all fields empty.- Returns:
- a new empty builder.
-
createEvent
@Deprecated public static Log4jLogEvent createEvent(java.lang.String loggerName, Marker marker, java.lang.String loggerFQCN, Level level, Message message, java.lang.Throwable thrown, ThrowableProxy thrownProxy, java.util.Map<java.lang.String,java.lang.String> mdc, ThreadContext.ContextStack ndc, java.lang.String threadName, java.lang.StackTraceElement location, long timestamp)
Deprecated.useLog4jLogEvent.Builder
instead. This method will be removed in an upcoming release.Create a new LogEvent.- Parameters:
loggerName
- The name of the Logger.marker
- The Marker or null.loggerFQCN
- The fully qualified class name of the caller.level
- The logging Level.message
- The Message.thrown
- A Throwable or null.thrownProxy
- A ThrowableProxy or null.mdc
- The mapped diagnostic context.ndc
- the nested diagnostic context.threadName
- The name of the thread.location
- The locations of the caller.timestamp
- The timestamp of the event.- Returns:
- a new LogEvent
-
createContextData
private static StringMap createContextData(java.util.Map<java.lang.String,java.lang.String> contextMap)
-
getNanoClock
public static NanoClock getNanoClock()
Returns theNanoClock
to use for creating the nanoTime timestamp of log events.- Returns:
- the
NanoClock
to use for creating the nanoTime timestamp of log events
-
setNanoClock
public static void setNanoClock(NanoClock nanoClock)
Sets theNanoClock
to use for creating the nanoTime timestamp of log events.FOR INTERNAL USE. This method may be called with a different
NanoClock
implementation when the configuration changes.- Parameters:
nanoClock
- theNanoClock
to use for creating the nanoTime timestamp of log events
-
asBuilder
public Log4jLogEvent.Builder asBuilder()
Returns a new fully initializedLog4jLogEvent.Builder
containing a copy of all fields of this event.- Returns:
- a new fully initialized builder.
-
toImmutable
public Log4jLogEvent toImmutable()
Description copied from interface:LogEvent
Returns an immutable version of this log event, which MAY BE a copy of this event.- Specified by:
toImmutable
in interfaceLogEvent
- Returns:
- an immutable version of this log event
-
getLevel
public Level getLevel()
Returns the logging Level.
-
getLoggerName
public java.lang.String getLoggerName()
Returns the name of the Logger used to generate the event.- Specified by:
getLoggerName
in interfaceLogEvent
- Returns:
- The Logger name.
-
getMessage
public Message getMessage()
Returns the Message associated with the event.- Specified by:
getMessage
in interfaceLogEvent
- Returns:
- The Message.
-
makeMessageImmutable
public void makeMessageImmutable()
-
getThreadId
public long getThreadId()
Description copied from interface:LogEvent
Gets the thread ID.- Specified by:
getThreadId
in interfaceLogEvent
- Returns:
- thread ID.
-
getThreadName
public java.lang.String getThreadName()
Returns the name of the Thread on which the event was generated.- Specified by:
getThreadName
in interfaceLogEvent
- Returns:
- The name of the Thread.
-
getThreadPriority
public int getThreadPriority()
Description copied from interface:LogEvent
Gets the thread priority.- Specified by:
getThreadPriority
in interfaceLogEvent
- Returns:
- thread priority.
-
getTimeMillis
public long getTimeMillis()
Gets event time in milliseconds since midnight, January 1, 1970 UTC. UseLogEvent.getInstant()
to get higher precision timestamp information if available on this platform.- Specified by:
getTimeMillis
in interfaceLogEvent
- Returns:
- the milliseconds component of this log event's timestamp
- See Also:
System.currentTimeMillis()
-
getInstant
public Instant getInstant()
Returns the Instant when the message was logged.Caution: if this
LogEvent
implementation is mutable and reused for multiple consecutive log messages, then theInstant
object 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.- Specified by:
getInstant
in interfaceLogEvent
- Returns:
- the
Instant
holding Instant details for this log event - Since:
- 2.11
-
getThrown
public java.lang.Throwable getThrown()
Returns the Throwable associated with the event, or null.
-
getThrownProxy
public ThrowableProxy getThrownProxy()
Returns the ThrowableProxy associated with the event, or null.- Specified by:
getThrownProxy
in interfaceLogEvent
- Returns:
- The ThrowableProxy associated with the event.
-
getMarker
public Marker getMarker()
Returns the Marker associated with the event, or null.
-
getLoggerFqcn
public java.lang.String getLoggerFqcn()
The fully qualified class name of the class that was called by the caller.- Specified by:
getLoggerFqcn
in interfaceLogEvent
- Returns:
- the fully qualified class name of the class that is performing logging.
-
getContextData
public ReadOnlyStringMap getContextData()
Returns theReadOnlyStringMap
containing context data key-value pairs.- Specified by:
getContextData
in interfaceLogEvent
- Returns:
- the
ReadOnlyStringMap
containing context data key-value pairs - Since:
- 2.7
- See Also:
ContextDataInjector
,ThreadContext
-
getContextMap
public java.util.Map<java.lang.String,java.lang.String> getContextMap()
Returns the immutable copy of the ThreadContext Map.- Specified by:
getContextMap
in interfaceLogEvent
- Returns:
- The context Map.
-
getContextStack
public ThreadContext.ContextStack getContextStack()
Returns an immutable copy of the ThreadContext stack.- Specified by:
getContextStack
in interfaceLogEvent
- Returns:
- The context Stack.
-
getSource
public java.lang.StackTraceElement getSource()
Returns the StackTraceElement for the caller. This will be the entry that occurs right before the first occurrence of FQCN as a class name.
-
isIncludeLocation
public boolean isIncludeLocation()
Description copied from interface:LogEvent
Returns whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use this flag to determine whether to take aStackTrace
snapshot or not before handing off this event to another thread.- Specified by:
isIncludeLocation
in interfaceLogEvent
- Returns:
true
if the source of the logging request is required downstream,false
otherwise.- See Also:
LogEvent.getSource()
-
setIncludeLocation
public void setIncludeLocation(boolean includeLocation)
Description copied from interface:LogEvent
Sets whether the source of the logging request is required downstream. Asynchronous Loggers and Appenders use this flag to determine whether to take aStackTrace
snapshot or not before handing off this event to another thread.- Specified by:
setIncludeLocation
in interfaceLogEvent
- Parameters:
includeLocation
-true
if the source of the logging request is required downstream,false
otherwise.- See Also:
LogEvent.getSource()
-
isEndOfBatch
public boolean isEndOfBatch()
Description copied from interface:LogEvent
Returnstrue
if this event is the last one in a batch,false
otherwise. Used by asynchronous Loggers and Appenders to signal to buffered downstream components when to flush to disk, as a more efficient alternative to theimmediateFlush=true
configuration.- Specified by:
isEndOfBatch
in interfaceLogEvent
- Returns:
- whether this event is the last one in a batch.
-
setEndOfBatch
public void setEndOfBatch(boolean endOfBatch)
Description copied from interface:LogEvent
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=true
configuration.- Specified by:
setEndOfBatch
in interfaceLogEvent
- Parameters:
endOfBatch
-true
if this event is the last one in a batch,false
otherwise.
-
getNanoTime
public long getNanoTime()
Description copied from interface:LogEvent
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.- Specified by:
getNanoTime
in interfaceLogEvent
- Returns:
- The value of the running Java Virtual Machine's high-resolution time source when this event was created.
-
writeReplace
protected java.lang.Object writeReplace()
Creates a LogEventProxy that can be serialized.- Returns:
- a LogEventProxy.
-
serialize
public static java.io.Serializable serialize(LogEvent event, boolean includeLocation)
Take a snapshot of the specifiedLogEvent
.- Parameters:
event
- the event to take a snapshot ofincludeLocation
- if true, this method will obtain caller location information- Returns:
- snapshot of the event as a
Serializable
object - See Also:
deserialize(Serializable)
,serialize(Log4jLogEvent, boolean)
-
serialize
public static java.io.Serializable serialize(Log4jLogEvent event, boolean includeLocation)
Take a snapshot of the specifiedLog4jLogEvent
.- Parameters:
event
- the event to take a snapshot ofincludeLocation
- if true, this method will obtain caller location information- Returns:
- snapshot of the event as a
Serializable
object - See Also:
deserialize(Serializable)
,serialize(LogEvent, boolean)
-
canDeserialize
public static boolean canDeserialize(java.io.Serializable event)
-
deserialize
public static Log4jLogEvent deserialize(java.io.Serializable event)
-
readObject
private void readObject(java.io.ObjectInputStream stream) throws java.io.InvalidObjectException
- Throws:
java.io.InvalidObjectException
-
createMemento
public static Log4jLogEvent createMemento(LogEvent event, boolean includeLocation)
Creates and returns a new immutable copy of thisLog4jLogEvent
.- Returns:
- a new immutable copy of the data in this
Log4jLogEvent
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-