Class JdkLoggerContext
- java.lang.Object
-
- com.github.robtimus.junit.support.extension.testlogger.JdkLoggerContext
-
public final class JdkLoggerContext extends Object
JdkLoggerContext
represents a JDKLogger
. It can be injected usingTestLogger
,TestLogger.ForClass
orTestLogger.Root
, and can be used to configure the logger for test purposes.- Author:
- Rob Spoor
- Since:
- 2.1
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JdkLoggerContext
addHandler(Handler handler)
Adds a handler to the logger.LogCaptor<LogRecord>
capture()
Returns an object that captures logged records.JdkLoggerContext
removeHandler(Handler handler)
Removes a handler from the logger.JdkLoggerContext
removeHandlers()
Removes all handlers from the logger.JdkLoggerContext
removeHandlers(Predicate<? super Handler> filter)
Removes all handlers from the logger that match a filter.void
restore()
Restores the original settings of the logger.JdkLoggerContext
setHandler(Handler handler)
Sets the single handler for the logger.JdkLoggerContext
setLevel(Level level)
Sets the new level for the logger.JdkLoggerContext
useParentHandlers(boolean useParentHandlers)
Sets whether or not to use parent handlers for the logger.
-
-
-
Method Detail
-
setLevel
public JdkLoggerContext 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
.
-
addHandler
public JdkLoggerContext addHandler(Handler handler)
Adds a handler to the logger.- Parameters:
handler
- The handler to add.- Returns:
- This object.
- Throws:
NullPointerException
- If the given handler isnull
.
-
setHandler
public JdkLoggerContext setHandler(Handler handler)
Sets the single handler for the logger. All existing handlers will first be removed.- Parameters:
handler
- The handler to set.- Returns:
- This object.
- Throws:
NullPointerException
- If the given handler isnull
.
-
removeHandler
public JdkLoggerContext removeHandler(Handler handler)
Removes a handler from the logger.- Parameters:
handler
- The handler to remove.- Returns:
- This object.
- Throws:
NullPointerException
- If the given handler isnull
.
-
removeHandlers
public JdkLoggerContext removeHandlers()
Removes all handlers from the logger.- Returns:
- This object.
-
removeHandlers
public JdkLoggerContext removeHandlers(Predicate<? super Handler> filter)
Removes all handlers from the logger that match a filter.- Parameters:
filter
- The filter to use.- Returns:
- This object.
- Throws:
NullPointerException
- If the given filter isnull
.
-
useParentHandlers
public JdkLoggerContext useParentHandlers(boolean useParentHandlers)
Sets whether or not to use parent handlers for the logger.- Parameters:
useParentHandlers
-true
to use parent handlers,false
otherwise.- Returns:
- This object.
-
capture
public LogCaptor<LogRecord> capture()
Returns an object that captures logged records. This can be used instead of having to append a capturing handler manually.- Returns:
- An object that captures logged records.
- 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.
-
-