Package org.apache.logging.log4j.core
Class Logger
java.lang.Object
org.apache.logging.log4j.spi.AbstractLogger
org.apache.logging.log4j.core.Logger
- All Implemented Interfaces:
Serializable
,Supplier<LoggerConfig>
,Logger
,ExtendedLogger
,LocationAwareLogger
,Supplier<LoggerConfig>
- Direct Known Subclasses:
AsyncLogger
The core implementation of the
Logger
interface. Besides providing an implementation
of all the Logger methods, this class also provides some convenience methods for Log4j 1.x compatibility as well as
access to the Filters
and Appenders
associated with this Logger. Note that access to these underlying objects is provided primarily for use in
unit tests or bridging legacy Log4j 1.x code. Future versions of this class may or may not include the various
methods that are noted as not being part of the public API.
TODO All the isEnabled methods could be pushed into a filter interface. Not sure of the utility of having isEnabled
be able to examine the message pattern and parameters. (RG) Moving the isEnabled methods out of Logger noticeably
impacts performance. The message pattern and parameters are required so that they can be used in global filters.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Serialization proxy class for Logger.protected class
The binding between a Logger and its configuration. -
Field Summary
Modifier and TypeFieldDescriptionprotected Logger.PrivateConfig
Config should be consistent across threads.Fields inherited from class org.apache.logging.log4j.spi.AbstractLogger
CATCHING_MARKER, DEFAULT_FLOW_MESSAGE_FACTORY_CLASS, DEFAULT_MESSAGE_FACTORY_CLASS, ENTRY_MARKER, EXCEPTION_MARKER, EXIT_MARKER, FLOW_MARKER, name, THROWING_MARKER
-
Constructor Summary
ModifierConstructorDescriptionprotected
Logger
(LoggerContext context, String name, MessageFactory messageFactory) Constructs an instance using the givenLoggerContext
, logger name, andMessageFactory
.protected
Logger
(LoggerContext context, String name, MessageFactory messageFactory, FlowMessageFactory flowMessageFactory) The canonical constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAppender
(Appender appender) This method is not exposed through the public API and is used primarily for unit testing.void
This method is not exposed through the public API and is used primarily for unit testing.boolean
int
This method is not exposed through the public API and is used primarily for unit testing.get()
This method is not exposed through the public API and is used primarily for unit testing.Returns the LoggerContext this Logger is associated with.This method is not exposed through the public API and is used primarily for unit testing.getLevel()
Gets the Level associated with the Logger.This method is only used for 1.x compatibility.int
hashCode()
boolean
This method is not exposed through the public API and is present only to support the Log4j 1.2 compatibility bridge.boolean
isEnabled
(Level level, Marker marker, CharSequence message, Throwable t) boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
isEnabled
(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4) boolean
isEnabled
(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5) boolean
isEnabled
(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) boolean
isEnabled
(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7) boolean
isEnabled
(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8) boolean
isEnabled
(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9) boolean
boolean
protected void
log
(Level level, Marker marker, String fqcn, StackTraceElement location, Message message, Throwable throwable) void
void
removeAppender
(Appender appender) This method is not exposed through the public API and is used primarily for unit testing.protected boolean
void
setAdditive
(boolean additive) This method is not exposed through the public API and is present only to support the Log4j 1.2 compatibility bridge.void
This method is not exposed through the public API and is provided primarily for unit testing.toString()
Returns a String representation of this instance in the form"name:level[ in context_name]"
.protected void
updateConfiguration
(Configuration newConfig) Associates this Logger with a new Configuration.protected Object
Methods inherited from class org.apache.logging.log4j.spi.AbstractLogger
always, atDebug, atError, atFatal, atInfo, atTrace, atWarn, catching, catching, catching, catchingMsg, checkMessageFactory, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, debug, enter, enter, enter, enter, enter, entry, entry, entry, entryMsg, entryMsg, entryMsg, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, error, exit, exit, exit, exit, exitMsg, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, fatal, getFlowMessageFactory, getLogBuilder, getMessageFactory, getName, getRecursionDepth, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, info, isDebugEnabled, isDebugEnabled, isEnabled, isEnabled, isErrorEnabled, isErrorEnabled, isFatalEnabled, isFatalEnabled, isInfoEnabled, isInfoEnabled, isTraceEnabled, isTraceEnabled, isWarnEnabled, isWarnEnabled, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, log, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logIfEnabled, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, logMessage, printf, printf, throwing, throwing, throwing, throwingMsg, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, trace, traceEntry, traceEntry, traceEntry, traceEntry, traceEntry, traceExit, traceExit, traceExit, traceExit, traceExit, traceExit, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn, warn
-
Field Details
-
privateConfig
Config should be consistent across threads.
-
-
Constructor Details
-
Logger
Constructs an instance using the givenLoggerContext
, logger name, andMessageFactory
.- Parameters:
context
- theLoggerContext
this logger is associated withmessageFactory
- The message factory to be used. If null, first the "log4j2.messageFactory" property will be used to instantiate the message factory. If the property is missing and thelog4j2.enableThreadLocals
property is notfalse
,ReusableMessageFactory
will be used. Otherwise, we will fall back toParameterizedMessageFactory
.name
- the logger name
-
Logger
protected Logger(LoggerContext context, String name, MessageFactory messageFactory, FlowMessageFactory flowMessageFactory) The canonical constructor.- Parameters:
context
- theLoggerContext
this logger is associated withmessageFactory
- The message factory to be used. If null, first the "log4j2.messageFactory" property will be used to instantiate the message factory. If the property is missing and thelog4j2.enableThreadLocals
property is notfalse
,ReusableMessageFactory
will be used. Otherwise, we will fall back toParameterizedMessageFactory
.flowMessageFactory
- The flow message factory to be used. If null, first the "log4j2.flowMessageFactory" property will be used to instantiate the flow message factory. If the property is missing,DefaultFlowMessageFactory
will be used.name
- the logger name
-
-
Method Details
-
writeReplace
- Throws:
ObjectStreamException
-
getParent
This method is only used for 1.x compatibility. Returns the parent of this Logger. If it doesn't already exist return a temporary Logger.- Returns:
- The parent Logger.
-
getContext
Returns the LoggerContext this Logger is associated with.- Returns:
- the LoggerContext.
-
setLevel
This method is not exposed through the public API and is provided primarily for unit testing.If the new level is null, this logger inherits the level from its parent.
- Parameters:
level
- The Level to use on this Logger, may be null.
-
get
- Specified by:
get
in interfaceSupplier<LoggerConfig>
- Specified by:
get
in interfaceSupplier<LoggerConfig>
-
requiresLocation
protected boolean requiresLocation()- Overrides:
requiresLocation
in classAbstractLogger
-
logMessage
- Specified by:
logMessage
in interfaceExtendedLogger
-
log
protected void log(Level level, Marker marker, String fqcn, StackTraceElement location, Message message, Throwable throwable) - Overrides:
log
in classAbstractLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
public boolean isEnabled(Level level, Marker marker, String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9) - Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
isEnabled
- Specified by:
isEnabled
in interfaceExtendedLogger
-
addAppender
This method is not exposed through the public API and is used primarily for unit testing.- Parameters:
appender
- The Appender to add to the Logger.
-
removeAppender
This method is not exposed through the public API and is used primarily for unit testing.- Parameters:
appender
- The Appender to remove from the Logger.
-
getAppenders
This method is not exposed through the public API and is used primarily for unit testing.- Returns:
- A Map containing the Appender's name as the key and the Appender as the value.
-
getFilters
This method is not exposed through the public API and is used primarily for unit testing.- Returns:
- An Iterator over all the Filters associated with the Logger.
-
getLevel
Gets the Level associated with the Logger. -
filterCount
public int filterCount()This method is not exposed through the public API and is used primarily for unit testing.- Returns:
- The number of Filters associated with the Logger.
-
addFilter
This method is not exposed through the public API and is used primarily for unit testing.- Parameters:
filter
- The Filter to add.
-
isAdditive
public boolean isAdditive()This method is not exposed through the public API and is present only to support the Log4j 1.2 compatibility bridge.- Returns:
- true if the associated LoggerConfig is additive, false otherwise.
-
setAdditive
public void setAdditive(boolean additive) This method is not exposed through the public API and is present only to support the Log4j 1.2 compatibility bridge.- Parameters:
additive
- Boolean value to indicate whether the Logger is additive or not.
-
atLevel
- Specified by:
atLevel
in interfaceLogger
- Overrides:
atLevel
in classAbstractLogger
-
updateConfiguration
Associates this Logger with a new Configuration. This method is not exposed through the public API.There are two ways this could be used to guarantee all threads are aware of changes to config.
- Synchronize this method. Accessors don't need to be synchronized as Java will treat all variables within a synchronized block as volatile.
- Declare the variable volatile. Option 2 is used here as the performance cost is very low and it does a better job at documenting how it is used.
- Parameters:
newConfig
- The new Configuration.
-
toString
Returns a String representation of this instance in the form"name:level[ in context_name]"
. -
equals
-
hashCode
public int hashCode()
-