Class CustomConfigurator
- All Implemented Interfaces:
ch.qos.logback.classic.spi.Configurator
,ch.qos.logback.core.spi.ContextAware
Configures Logback programmatically.
Logging works differently in different environments. For normal use in development and production, we want it to work normally, as specified in the application configuration. But in testing, we don't want any logging. As for how that is accomplished:
Development & production
Logback supports a variety of configuration methods, but regardless, it
really wants to take the reins of its own initialization and not have to
wait for the application main method. This is a problem because the logging
configuration is part of the application configuration, which doesn't get
read until the -config
command-line argument has been handled, long
after Logback would want to have initialized itself.
The Logback docs suggest using a custom Configurator
(like this
one) and telling Logback about it using the ServiceLoader
mechanism, so that it can be loaded at startup. But since we don't want it
to load at startup, we don't use that mechanism. Instead we reset the logger
context manually when ready using resetLoggerContext()
.
Testing
None of the above applies in testing. In order to disable logging in testing, we put a skeletal logback.xml file in src/main/resources. This serves as a "poison pill" that effectively silences Logback.
Why src/main/resources and not src/test/resources? Because the former will cause Logback to not fall back to its own default configuration in development/production.
-
Nested Class Summary
Nested classes/interfaces inherited from interface ch.qos.logback.classic.spi.Configurator
ch.qos.logback.classic.spi.Configurator.ExecutionStatus
-
Field Summary
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionch.qos.logback.classic.spi.Configurator.ExecutionStatus
configure
(ch.qos.logback.classic.LoggerContext loggerContext) static void
To be invoked when theapplication configuration
becomes available at startup.Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContext
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
-
Constructor Details
-
CustomConfigurator
public CustomConfigurator()
-
-
Method Details
-
resetLoggerContext
public static void resetLoggerContext()To be invoked when theapplication configuration
becomes available at startup. (See class documentation.) -
configure
public ch.qos.logback.classic.spi.Configurator.ExecutionStatus configure(ch.qos.logback.classic.LoggerContext loggerContext) - Specified by:
configure
in interfacech.qos.logback.classic.spi.Configurator
-