Class MutableLogEvent
- java.lang.Object
-
- org.apache.logging.log4j.core.impl.MutableLogEvent
-
- All Implemented Interfaces:
Serializable
,LogEvent
,Message
,ParameterVisitable
,ReusableMessage
,StringBuilderFormattable
public class MutableLogEvent extends Object implements LogEvent, ReusableMessage, ParameterVisitable
Mutable implementation of theLogEvent
interface.- Since:
- 2.6
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MutableLogEvent()
MutableLogEvent(StringBuilder msgText, Object[] replacementParameters)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears all references this event has to other objects.Log4jLogEvent
createMemento()
Creates and returns a new immutable copy of thisMutableLogEvent
.<S> void
forEachParameter(ParameterConsumer<S> action, S state)
Performs the given action for each parameter until all values have been processed or the action throws an exception.void
formatTo(StringBuilder buffer)
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.ReadOnlyStringMap
getContextData()
Returns theReadOnlyStringMap
object holding context data key-value pairs.Map<String,String>
getContextMap()
Gets the context map (also know as Mapped Diagnostic Context or MDC).ThreadContext.ContextStack
getContextStack()
Gets the context stack (also known as Nested Diagnostic Context or NDC).String
getFormat()
Gets the format portion of the Message.String
getFormattedMessage()
Gets the Message formatted as a String.Instant
getInstant()
Returns the Instant when the message was logged.Level
getLevel()
Gets the level.String
getLoggerFqcn()
Returns the fully qualified class name of the caller of the logging API.String
getLoggerName()
Gets the logger name.Marker
getMarker()
Gets the Marker associated with the event.Message
getMessage()
Gets the message associated with the event.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.short
getParameterCount()
Returns the number of parameters that was used to initialize this reusable message for the current content.Object[]
getParameters()
Gets parameter values, if any.StackTraceElement
getSource()
Returns the StackTraceElement for the caller.long
getThreadId()
Gets the thread ID.String
getThreadName()
Gets the thread name.int
getThreadPriority()
Gets the thread priority.Throwable
getThrowable()
Gets the throwable, if any.Throwable
getThrown()
Gets throwable associated with logging request.ThrowableProxy
getThrownProxy()
Returns the ThrowableProxy associated with the event, or null.long
getTimeMillis()
Gets event time in milliseconds since midnight, January 1, 1970 UTC.void
initFrom(LogEvent event)
Initialize the fields of thisMutableLogEvent
from another event.void
initializeBuilder(Log4jLogEvent.Builder builder)
Initializes the specifiedLog4jLogEvent.Builder
from thisMutableLogEvent
.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.Message
memento()
Returns an immutable snapshot of the current internal state of this reusable message.void
setContextData(StringMap mutableContextData)
void
setContextStack(ThreadContext.ContextStack contextStack)
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.void
setLevel(Level level)
void
setLoggerFqcn(String loggerFqcn)
void
setLoggerName(String loggerName)
void
setMarker(Marker marker)
void
setMessage(Message msg)
void
setNanoTime(long nanoTime)
void
setSource(StackTraceElement source)
void
setThreadId(long threadId)
void
setThreadName(String threadName)
void
setThreadPriority(int threadPriority)
void
setThrown(Throwable thrown)
void
setTimeMillis(long timeMillis)
Object[]
swapParameters(Object[] emptyReplacement)
Replaces this ReusableMessage's parameter array with the specified value and return the original arrayLog4jLogEvent
toImmutable()
Returns an immutable version of this log event, which MAY BE a copy of this event.protected Object
writeReplace()
Creates a LogEventProxy that can be serialized.
-
-
-
Constructor Detail
-
MutableLogEvent
public MutableLogEvent()
-
MutableLogEvent
public MutableLogEvent(StringBuilder msgText, Object[] replacementParameters)
-
-
Method Detail
-
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
-
initFrom
public void initFrom(LogEvent event)
Initialize the fields of thisMutableLogEvent
from another event. Similar in purpose and usage asLog4jLogEvent.LogEventProxy
, but a mutable version.This method is used on async logger ringbuffer slots holding MutableLogEvent objects in each slot.
- Parameters:
event
- the event to copy data from
-
clear
public void clear()
Clears all references this event has to other objects.
-
getLoggerFqcn
public String getLoggerFqcn()
Description copied from interface:LogEvent
Returns the fully qualified class name of the caller of the logging API.- Specified by:
getLoggerFqcn
in interfaceLogEvent
- Returns:
- The fully qualified class name of the caller.
-
setLoggerFqcn
public void setLoggerFqcn(String loggerFqcn)
-
getMarker
public Marker getMarker()
Description copied from interface:LogEvent
Gets the Marker associated with the event.
-
setMarker
public void setMarker(Marker marker)
-
setLevel
public void setLevel(Level level)
-
getLoggerName
public String getLoggerName()
Description copied from interface:LogEvent
Gets the logger name.- Specified by:
getLoggerName
in interfaceLogEvent
- Returns:
- logger name, may be
null
.
-
setLoggerName
public void setLoggerName(String loggerName)
-
getMessage
public Message getMessage()
Description copied from interface:LogEvent
Gets the message associated with the event.- Specified by:
getMessage
in interfaceLogEvent
- Returns:
- message.
-
setMessage
public void setMessage(Message msg)
-
getFormattedMessage
public String getFormattedMessage()
Description copied from interface:Message
Gets the Message formatted as a String. Each Message implementation determines the appropriate way to format the data encapsulated in the Message. Messages that provide more than one way of formatting the Message will implement MultiformatMessage.When configured to log asynchronously, this method is called before the Message is queued, unless this message implements
ReusableMessage
or is annotated withAsynchronouslyFormattable
. This gives the Message implementation class a chance to create a formatted message String with the current value of any mutable objects. The intention is that the Message implementation caches this formatted message and returns it on subsequent calls. (See LOG4J2-763.)When logging synchronously, this method will not be called for Messages that implement the
StringBuilderFormattable
interface: instead, theformatTo(StringBuilder)
method will be called so the Message can format its contents without creating intermediate String objects.- Specified by:
getFormattedMessage
in interfaceMessage
- Returns:
- The message String.
- See Also:
Message.getFormattedMessage()
-
getFormat
public String getFormat()
Description copied from interface:Message
Gets the format portion of the Message.- Specified by:
getFormat
in interfaceMessage
- Returns:
- The message format. Some implementations, such as ParameterizedMessage, will use this as the message "pattern". Other Messages may simply return an empty String. TODO Do all messages have a format? What syntax? Using a Formatter object could be cleaner. (RG) In SimpleMessage the format is identical to the formatted message. In ParameterizedMessage and StructuredDataMessage it is not. It is up to the Message implementer to determine what this method will return. A Formatter is inappropriate as this is very specific to the Message implementation so it isn't clear to me how having a Formatter separate from the Message would be cleaner.
- See Also:
Message.getFormat()
-
getParameters
public Object[] getParameters()
Description copied from interface:Message
Gets parameter values, if any.- Specified by:
getParameters
in interfaceMessage
- Returns:
- An array of parameter values or null.
- See Also:
Message.getParameters()
-
forEachParameter
public <S> void forEachParameter(ParameterConsumer<S> action, S state)
Description copied from interface:ParameterVisitable
Performs the given action for each parameter until all values have been processed or the action throws an exception.The second parameter lets callers pass in a stateful object to be modified with the key-value pairs, so the TriConsumer implementation itself can be stateless and potentially reusable.
- Specified by:
forEachParameter
in interfaceParameterVisitable
- Type Parameters:
S
- type of the third parameter- Parameters:
action
- The action to be performed for each key-value pair in this collectionstate
- the object to be passed as the third parameter to each invocation on the specified ParameterConsumer.
-
getThrowable
public Throwable getThrowable()
Description copied from interface:Message
Gets the throwable, if any.- Specified by:
getThrowable
in interfaceMessage
- Returns:
- the throwable or null.
- See Also:
Message.getThrowable()
-
formatTo
public void formatTo(StringBuilder buffer)
Description copied from interface:StringBuilderFormattable
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.- Specified by:
formatTo
in interfaceStringBuilderFormattable
- Parameters:
buffer
- the StringBuilder to write into- See Also:
StringBuilderFormattable.formatTo(StringBuilder)
-
swapParameters
public Object[] swapParameters(Object[] emptyReplacement)
Replaces this ReusableMessage's parameter array with the specified value and return the original array- Specified by:
swapParameters
in interfaceReusableMessage
- Parameters:
emptyReplacement
- the parameter array that can be used for subsequent uses of this reusable message- Returns:
- the original parameter array
- See Also:
ReusableMessage.swapParameters(Object[])
-
getParameterCount
public short getParameterCount()
Description copied from interface:ReusableMessage
Returns the number of parameters that was used to initialize this reusable message for the current content.The parameter array returned by
ReusableMessage.swapParameters(Object[])
may be larger than the actual number of parameters. Callers should use this method to determine how many elements the array contains.- Specified by:
getParameterCount
in interfaceReusableMessage
- Returns:
- the current number of parameters
-
memento
public Message memento()
Description copied from interface:ReusableMessage
Returns an immutable snapshot of the current internal state of this reusable message. The returned snapshot will not be affected by subsequent modifications of this reusable message.- Specified by:
memento
in interfaceReusableMessage
- Returns:
- an immutable snapshot of this message
-
getThrown
public Throwable getThrown()
Description copied from interface:LogEvent
Gets throwable associated with logging request.Convenience method for
ThrowableProxy.getThrowable();
-
setThrown
public void setThrown(Throwable thrown)
-
getTimeMillis
public long getTimeMillis()
Description copied from interface:LogEvent
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()
-
setTimeMillis
public void setTimeMillis(long timeMillis)
-
getInstant
public Instant getInstant()
Description copied from interface:LogEvent
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
-
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.
-
setSource
public void setSource(StackTraceElement source)
-
getSource
public 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.
-
getContextData
public ReadOnlyStringMap getContextData()
Description copied from interface:LogEvent
Returns theReadOnlyStringMap
object 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 customContextDataInjector
to inject key-value pairs from any arbitrary source.- Specified by:
getContextData
in interfaceLogEvent
- Returns:
- the
ReadOnlyStringMap
object holding context data key-value pairs - See Also:
ContextDataInjector
,ThreadContext
-
getContextMap
public Map<String,String> getContextMap()
Description copied from interface:LogEvent
Gets the context map (also know as Mapped Diagnostic Context or MDC).- Specified by:
getContextMap
in interfaceLogEvent
- Returns:
- The context map, never
null
.
-
setContextData
public void setContextData(StringMap mutableContextData)
-
getContextStack
public ThreadContext.ContextStack getContextStack()
Description copied from interface:LogEvent
Gets the context stack (also known as Nested Diagnostic Context or NDC).- Specified by:
getContextStack
in interfaceLogEvent
- Returns:
- The context stack, never
null
.
-
setContextStack
public void setContextStack(ThreadContext.ContextStack contextStack)
-
getThreadId
public long getThreadId()
Description copied from interface:LogEvent
Gets the thread ID.- Specified by:
getThreadId
in interfaceLogEvent
- Returns:
- thread ID.
-
setThreadId
public void setThreadId(long threadId)
-
getThreadName
public String getThreadName()
Description copied from interface:LogEvent
Gets the thread name.- Specified by:
getThreadName
in interfaceLogEvent
- Returns:
- thread name, may be null. TODO guess this could go into a thread context object too. (RG) Why?
-
setThreadName
public void setThreadName(String threadName)
-
getThreadPriority
public int getThreadPriority()
Description copied from interface:LogEvent
Gets the thread priority.- Specified by:
getThreadPriority
in interfaceLogEvent
- Returns:
- thread priority.
-
setThreadPriority
public void setThreadPriority(int threadPriority)
-
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.
-
setNanoTime
public void setNanoTime(long nanoTime)
-
writeReplace
protected Object writeReplace()
Creates a LogEventProxy that can be serialized.- Returns:
- a LogEventProxy.
-
createMemento
public Log4jLogEvent createMemento()
Creates and returns a new immutable copy of thisMutableLogEvent
. IfisIncludeLocation()
is true, this will obtain caller location information.- Returns:
- a new immutable copy of the data in this
MutableLogEvent
-
initializeBuilder
public void initializeBuilder(Log4jLogEvent.Builder builder)
Initializes the specifiedLog4jLogEvent.Builder
from thisMutableLogEvent
.- Parameters:
builder
- the builder whose fields to populate
-
-