Class Log4jLoggerContext
- java.lang.Object
-
- com.github.robtimus.junit.support.extension.testlogger.Log4jLoggerContext
-
public final class Log4jLoggerContext extends Object
Log4jLoggerContext
represents a Log4jLogger
(2.x only). It can be injected usingTestLogger
,TestLogger.ForClass
orTestLogger.Root
, and can be used to configure the logger for test purposes.Note: using mocks or spies usually don't work out-of-the-box. One of the reasons is that the
LogEvent
passed toAppender.append(LogEvent)
may be mutable, and may be reused and altered by Log4j. To overcome this issue, spy on aLog4jNullAppender
and verify calls to itsLog4jNullAppender.ignore(LogEvent)
method.- Author:
- Rob Spoor
- Since:
- 2.1
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Log4jLoggerContext
addAppender(Appender appender)
Adds an appender to the logger.LogCaptor<LogEvent>
capture()
Returns an object that captures logged events.Log4jLoggerContext
removeAppender(Appender appender)
Removes an appender from the logger.Log4jLoggerContext
removeAppenders()
Removes all appenders from the logger.Log4jLoggerContext
removeAppenders(Predicate<? super Appender> filter)
Removes all appenders from the logger that match a filter.void
restore()
Restores the original settings of the logger.Log4jLoggerContext
setAppender(Appender appender)
Sets the single appender for the logger.Log4jLoggerContext
setLevel(Level level)
Sets the new level for the logger.Log4jLoggerContext
useParentAppenders(boolean useParentAppenders)
Sets whether or not to use parent appenders for the logger.
-
-
-
Method Detail
-
setLevel
public Log4jLoggerContext setLevel(Level level)
Sets the new level for the logger.- Parameters:
level
- The new level.- Returns:
- This object.
- Throws:
NullPointerException
- If the given level isnull
.
-
addAppender
public Log4jLoggerContext addAppender(Appender appender)
Adds an appender to the logger.- Parameters:
appender
- The appender to add.- Returns:
- This object.
- Throws:
NullPointerException
- If the given appender isnull
.
-
setAppender
public Log4jLoggerContext setAppender(Appender appender)
Sets the single appender for the logger. All existing appenders will first be removed.- Parameters:
appender
- The appender to set.- Returns:
- This object.
- Throws:
NullPointerException
- If the given appender isnull
.
-
removeAppender
public Log4jLoggerContext removeAppender(Appender appender)
Removes an appender from the logger.- Parameters:
appender
- The appender to remove.- Returns:
- This object.
- Throws:
NullPointerException
- If the given appender isnull
.
-
removeAppenders
public Log4jLoggerContext removeAppenders()
Removes all appenders from the logger.- Returns:
- This object.
-
removeAppenders
public Log4jLoggerContext removeAppenders(Predicate<? super Appender> filter)
Removes all appenders from the logger that match a filter.- Parameters:
filter
- The filter to use.- Returns:
- This object.
- Throws:
NullPointerException
- If the given filter isnull
.
-
useParentAppenders
public Log4jLoggerContext useParentAppenders(boolean useParentAppenders)
Sets whether or not to use parent appenders for the logger.- Parameters:
useParentAppenders
-true
to use parent appenders,false
otherwise.- Returns:
- This object.
-
capture
public LogCaptor<LogEvent> capture()
Returns an object that captures logged events. This can be used instead of having to append a capturing appender manually.- Returns:
- An object that captures logged events.
- Since:
- 3.0
-
restore
public void restore()
Restores the original settings of the logger.It should usually not be necessary to call this method, as it will be called automatically once this context goes out of scope.
-
-