Class JdkLoggerContext
- java.lang.Object
-
- com.github.robtimus.junit.support.extension.testlogger.JdkLoggerContext
-
public final class JdkLoggerContext extends Object
JdkLoggerContextrepresents a JDKLogger. It can be injected usingTestLogger,TestLogger.ForClassorTestLogger.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 JdkLoggerContextaddHandler(Handler handler)Adds a handler to the logger.LogCaptor<LogRecord>capture()Returns an object that captures logged records.JdkLoggerContextremoveHandler(Handler handler)Removes a handler from the logger.JdkLoggerContextremoveHandlers()Removes all handlers from the logger.JdkLoggerContextremoveHandlers(Predicate<? super Handler> filter)Removes all handlers from the logger that match a filter.voidrestore()Restores the original settings of the logger.JdkLoggerContextsetHandler(Handler handler)Sets the single handler for the logger.JdkLoggerContextsetLevel(Level level)Sets the new level for the logger.JdkLoggerContextuseParentHandlers(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-trueto use parent handlers,falseotherwise.- 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.
-
-