Class TestLogger
- java.lang.Object
-
- com.github.valfirst.slf4jtest.TestLogger
-
- All Implemented Interfaces:
Logger
public class TestLogger extends Object implements Logger
Implementation ofLoggerwhich storesLoggingEvents in memory and provides methods to access and remove them in order to facilitate writing tests that assert particular logging calls were made.LoggingEvents are stored in both anThreadLocaland a normalList. ThegetLoggingEvents()andclear()methods reference theThreadLocalevents. ThegetAllLoggingEvents()andclearAll()methods reference all events logged on this Logger. This is in order to facilitate parallelising tests - tests that use the thread local methods can be parallelised.By default all Levels are enabled. It is important to note that the conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so the
setEnabledLevels(ImmutableSet),setEnabledLevels(Level...),setEnabledLevelsForAllThreads(ImmutableSet),setEnabledLevelsForAllThreads(Level...)and the various isXxxxxEnabled() methods make no assumptions about this hierarchy. If you wish to use traditional hierarchical setups you may do so by passing the constants inConventionalLevelHierarchytosetEnabledLevels(ImmutableSet)orsetEnabledLevelsForAllThreads(ImmutableSet).
-
-
Field Summary
-
Fields inherited from interface org.slf4j.Logger
ROOT_LOGGER_NAME
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes allLoggingEvents logged by this thread and resets the enabled levels of the logger toLevel.enablableValueSet()for this thread.voidclearAll()Removes ALLLoggingEvents logged on this logger, regardless of thread, and resets the enabled levels of the logger toLevel.enablableValueSet()for ALL threads.voiddebug(String message)voiddebug(String format, Object arg)voiddebug(String format, Object... args)voiddebug(String format, Object arg1, Object arg2)voiddebug(String msg, Throwable throwable)voiddebug(Marker marker, String msg)voiddebug(Marker marker, String format, Object arg)voiddebug(Marker marker, String format, Object... args)voiddebug(Marker marker, String format, Object arg1, Object arg2)voiddebug(Marker marker, String msg, Throwable throwable)voiderror(String message)voiderror(String format, Object arg)voiderror(String format, Object... args)voiderror(String format, Object arg1, Object arg2)voiderror(String msg, Throwable throwable)voiderror(Marker marker, String msg)voiderror(Marker marker, String format, Object arg)voiderror(Marker marker, String format, Object... args)voiderror(Marker marker, String format, Object arg1, Object arg2)voiderror(Marker marker, String msg, Throwable throwable)com.google.common.collect.ImmutableList<LoggingEvent>getAllLoggingEvents()com.google.common.collect.ImmutableSet<Level>getEnabledLevels()com.google.common.collect.ImmutableList<LoggingEvent>getLoggingEvents()StringgetName()voidinfo(String message)voidinfo(String format, Object arg)voidinfo(String format, Object... args)voidinfo(String format, Object arg1, Object arg2)voidinfo(String msg, Throwable throwable)voidinfo(Marker marker, String msg)voidinfo(Marker marker, String format, Object arg)voidinfo(Marker marker, String format, Object... args)voidinfo(Marker marker, String format, Object arg1, Object arg2)voidinfo(Marker marker, String msg, Throwable throwable)booleanisDebugEnabled()booleanisDebugEnabled(Marker marker)booleanisErrorEnabled()booleanisErrorEnabled(Marker marker)booleanisInfoEnabled()booleanisInfoEnabled(Marker marker)booleanisTraceEnabled()booleanisTraceEnabled(Marker marker)booleanisWarnEnabled()booleanisWarnEnabled(Marker marker)voidsetEnabledLevels(com.google.common.collect.ImmutableSet<Level> enabledLevels)The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method.voidsetEnabledLevels(Level... enabledLevels)The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method.voidsetEnabledLevelsForAllThreads(com.google.common.collect.ImmutableSet<Level> enabledLevelsForAllThreads)The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method.voidsetEnabledLevelsForAllThreads(Level... enabledLevelsForAllThreads)The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method.voidtrace(String message)voidtrace(String format, Object arg)voidtrace(String format, Object... args)voidtrace(String format, Object arg1, Object arg2)voidtrace(String msg, Throwable throwable)voidtrace(Marker marker, String msg)voidtrace(Marker marker, String format, Object arg)voidtrace(Marker marker, String format, Object... args)voidtrace(Marker marker, String format, Object arg1, Object arg2)voidtrace(Marker marker, String msg, Throwable throwable)voidwarn(String message)voidwarn(String format, Object arg)voidwarn(String format, Object... args)voidwarn(String format, Object arg1, Object arg2)voidwarn(String msg, Throwable throwable)voidwarn(Marker marker, String msg)voidwarn(Marker marker, String format, Object arg)voidwarn(Marker marker, String format, Object... args)voidwarn(Marker marker, String format, Object arg1, Object arg2)voidwarn(Marker marker, String msg, Throwable throwable)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.slf4j.Logger
atDebug, atError, atInfo, atLevel, atTrace, atWarn, isEnabledForLevel, makeLoggingEventBuilder
-
-
-
-
Method Detail
-
clear
public void clear()
Removes allLoggingEvents logged by this thread and resets the enabled levels of the logger toLevel.enablableValueSet()for this thread.
-
clearAll
public void clearAll()
Removes ALLLoggingEvents logged on this logger, regardless of thread, and resets the enabled levels of the logger toLevel.enablableValueSet()for ALL threads.
-
getLoggingEvents
public com.google.common.collect.ImmutableList<LoggingEvent> getLoggingEvents()
- Returns:
- all
LoggingEvents logged on this logger by this thread
-
getAllLoggingEvents
public com.google.common.collect.ImmutableList<LoggingEvent> getAllLoggingEvents()
- Returns:
- all
LoggingEvents logged on this logger by ANY thread
-
isTraceEnabled
public boolean isTraceEnabled()
- Specified by:
isTraceEnabledin interfaceLogger- Returns:
- whether this logger is trace enabled in this thread
-
isTraceEnabled
public boolean isTraceEnabled(Marker marker)
- Specified by:
isTraceEnabledin interfaceLogger
-
isDebugEnabled
public boolean isDebugEnabled()
- Specified by:
isDebugEnabledin interfaceLogger- Returns:
- whether this logger is debug enabled in this thread
-
isDebugEnabled
public boolean isDebugEnabled(Marker marker)
- Specified by:
isDebugEnabledin interfaceLogger
-
isInfoEnabled
public boolean isInfoEnabled()
- Specified by:
isInfoEnabledin interfaceLogger- Returns:
- whether this logger is info enabled in this thread
-
isInfoEnabled
public boolean isInfoEnabled(Marker marker)
- Specified by:
isInfoEnabledin interfaceLogger
-
isWarnEnabled
public boolean isWarnEnabled()
- Specified by:
isWarnEnabledin interfaceLogger- Returns:
- whether this logger is warn enabled in this thread
-
isWarnEnabled
public boolean isWarnEnabled(Marker marker)
- Specified by:
isWarnEnabledin interfaceLogger
-
isErrorEnabled
public boolean isErrorEnabled()
- Specified by:
isErrorEnabledin interfaceLogger- Returns:
- whether this logger is error enabled in this thread
-
isErrorEnabled
public boolean isErrorEnabled(Marker marker)
- Specified by:
isErrorEnabledin interfaceLogger
-
getEnabledLevels
public com.google.common.collect.ImmutableSet<Level> getEnabledLevels()
- Returns:
- the set of levels enabled for this logger on this thread
-
setEnabledLevels
public void setEnabledLevels(com.google.common.collect.ImmutableSet<Level> enabledLevels)
The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method. If you wish to use traditional hierarchical setups you may conveniently do so by using the constants inConventionalLevelHierarchy- Parameters:
enabledLevels- levels which will be considered enabled for this logger IN THIS THREAD; does not affect enabled levels for this logger in other threads
-
setEnabledLevels
public void setEnabledLevels(Level... enabledLevels)
The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method. If you wish to use traditional hierarchical setups you may conveniently do so by passing the constants inConventionalLevelHierarchytosetEnabledLevels(ImmutableSet)- Parameters:
enabledLevels- levels which will be considered enabled for this logger IN THIS THREAD; does not affect enabled levels for this logger in other threads
-
setEnabledLevelsForAllThreads
public void setEnabledLevelsForAllThreads(com.google.common.collect.ImmutableSet<Level> enabledLevelsForAllThreads)
The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method. If you wish to use traditional hierarchical setups you may conveniently do so by using the constants inConventionalLevelHierarchy- Parameters:
enabledLevelsForAllThreads- levels which will be considered enabled for this logger IN ALL THREADS
-
setEnabledLevelsForAllThreads
public void setEnabledLevelsForAllThreads(Level... enabledLevelsForAllThreads)
The conventional hierarchical notion of Levels, where info being enabled implies warn and error being enabled, is not a requirement of the SLF4J API, so all levels you wish to enable must be passed explicitly to this method. If you wish to use traditional hierarchical setups you may conveniently do so by passing the constants inConventionalLevelHierarchytosetEnabledLevelsForAllThreads(ImmutableSet)- Parameters:
enabledLevelsForAllThreads- levels which will be considered enabled for this logger IN ALL THREADS
-
-