This class is used to initialize or load a logging configuration.
It also provides methods to enable/disable logging.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static LoggerConfiguration
The current configuration or null if the logging system has not been initialized.A list of all loggers which are configured.private static boolean
Whether the cached factory has been registered or not. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Private constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescription(package private) static void
checkLevel
(@NotNull LoggingType type, @NotNull org.apache.logging.log4j.Level level) Checks whether the given level is valid for the given logging type.
Invalid combinations are:
The levels 'all' or 'off' with any logging type Logging type 'file' and level 'trace', 'warn' or 'fatal'static @NotNull org.apache.logging.log4j.Marker
createRefrenceMarker
(int length) Creates a new reference marker with the given length.static void
disable
(@NotNull LoggingType type, @NotNull org.apache.logging.log4j.Level level) Disables logging for the given logging type and level.private static void
disableAppender
(@NotNull String name) Disables the given appender.static void
Disables console logging for all levels.static void
disableConsole
(@NotNull org.apache.logging.log4j.Level level) Disables console logging for the given level.static void
Disables file logging for all levels.static void
disableFile
(@NotNull org.apache.logging.log4j.Level level) Disables file logging for the given level.static void
enable
(@NotNull LoggingType type, @NotNull org.apache.logging.log4j.Level level) Enables logging for the given logging type and level.private static void
enableAppender
(@NotNull String name) Enables the given appender.static void
Enables console logging for all levels.static void
enableConsole
(@NotNull org.apache.logging.log4j.Level level) Enables console logging for the given level.static void
Enables file logging for all levels.static void
enableFile
(@NotNull org.apache.logging.log4j.Level level) Enables file logging for the given level.static @Nullable LoggerConfiguration
Returns the current configuration of the logging system.Returns a list of all the configured loggers.(package private) static @NotNull String
getLogger
(@NotNull LoggingType type, @NotNull org.apache.logging.log4j.Level level) Gets the name of the logger for the given logging type and level.static boolean
Checks whether the cached factory has been automatically registered.static void
initialize
(@NotNull LoggerConfiguration configuration) Initializes the logging system with the given configuration.static void
initialize
(@NotNull LoggerConfiguration configuration, boolean override) Initializes the logging system with the given configuration.private static void
initializeInternal
(@NotNull LoggerConfiguration configuration) Initializes the log4j2 configurator with the given configuration.static void
initializeOrReconfigure
(@NotNull LoggerConfiguration configuration) Tries to initialize the logging system with the given configuration,
if the logging system has already been initialized, the configuration will be reconfigured.static boolean
initializeSafe
(@NotNull LoggerConfiguration configuration) Initializes the logging system with the given configuration if it has not already been initialized.static boolean
Checks whether the logging system has been initialized.static boolean
Checks whether the root logger has been configured.
If this returns true, other loggers were ignored or not configured.static void
Loads the logging configuration for the given loggers from the system properties and initializes the logging system.
Loading the logger should be done as early as possible in the application lifecycle.
Ideally, in the static-initializer of the main class as first call.static void
Loads the logging configuration for the given loggers from the system properties and initializes the logging system.
Loading the logger should be done as early as possible in the application lifecycle.
Ideally, in the static-initializer of the main class as first call.static void
reconfigure
(@NotNull LoggerConfiguration configuration) Reconfigures the logging system with the given configuration.
Allows to change the logging configuration after it was initialized.static void
Registers the spring logging configuration factory.
The factory will only be registered if the spring boot framework is found.
-
Field Details
-
CONFIGURED_LOGGERS
A list of all loggers which are configured. -
configuration
The current configuration or null if the logging system has not been initialized. -
registeredFactory
private static boolean registeredFactoryWhether the cached factory has been registered or not.
-
-
Constructor Details
-
LoggingUtils
private LoggingUtils()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
load
Loads the logging configuration for the given loggers from the system properties and initializes the logging system.
Loading the logger should be done as early as possible in the application lifecycle.
Ideally, in the static-initializer of the main class as first call.- Parameters:
loggers
- The loggers to load the configuration for- Throws:
NullPointerException
- If the logger list is nullIllegalArgumentException
- If the logger list is empty or contains no valid loggerIllegalStateException
- If the logging system has already been initialized- See Also:
-
load
Loads the logging configuration for the given loggers from the system properties and initializes the logging system.
Loading the logger should be done as early as possible in the application lifecycle.
Ideally, in the static-initializer of the main class as first call.- Parameters:
loggers
- The loggers to load the configuration for- Throws:
NullPointerException
- If the logger list is nullIllegalArgumentException
- If the logger list is empty or contains no valid loggerIllegalStateException
- If the logging system has already been initialized- See Also:
-
initialize
Initializes the logging system with the given configuration.
The logger should be initialized as early as possible in the application lifecycle.
Ideally, in the static-initializer of the main class before any calls to the logger.
- Parameters:
configuration
- The configuration to use- Throws:
NullPointerException
- If the configuration is nullIllegalStateException
- If the logging system has already been initialized- See Also:
-
initialize
public static void initialize(@NotNull @NotNull LoggerConfiguration configuration, boolean override) Initializes the logging system with the given configuration.
The logger should be initialized as early as possible in the application lifecycle.
Ideally, in the static-initializer of the main class before any calls to the logger.
Example:
public class Main { private static final Logger LOGGER; public static void main(String[] args) { // ... } static { // Initialize the logging system here LOGGER = LogManager.getLogger(Main.class); } }
- Parameters:
configuration
- The configuration to useoverride
- Whether to override the current configuration if the logging system has already been initialized- Throws:
NullPointerException
- If the configuration is nullIllegalStateException
- If the logging system has already been initialized and override is false- See Also:
-
initializeSafe
Initializes the logging system with the given configuration if it has not already been initialized.- Parameters:
configuration
- The configuration to use- Returns:
- Whether the logging system has been initialized
- Throws:
NullPointerException
- If the configuration is null- See Also:
-
initializeInternal
Initializes the log4j2 configurator with the given configuration.- Parameters:
configuration
- The configuration to use- Throws:
NullPointerException
- If the configuration is null
-
reconfigure
Reconfigures the logging system with the given configuration.
Allows to change the logging configuration after it was initialized.- Parameters:
configuration
- The configuration to use- Throws:
NullPointerException
- If the configuration is null
-
initializeOrReconfigure
Tries to initialize the logging system with the given configuration,
if the logging system has already been initialized, the configuration will be reconfigured.- Parameters:
configuration
- The configuration to use- Throws:
NullPointerException
- If the configuration is null
-
isInitialized
public static boolean isInitialized()Checks whether the logging system has been initialized.- Returns:
- True if the logging system has been initialized, false otherwise
-
hasFactoryBeenRegistered
public static boolean hasFactoryBeenRegistered()Checks whether the cached factory has been automatically registered.- Returns:
- True if the cached factory has been registered, false otherwise
-
getConfiguration
Returns the current configuration of the logging system.- Returns:
- The current configuration or null if the logging system has not been initialized
-
getConfiguredLoggers
Returns a list of all the configured loggers.- Returns:
- The names of all the configured loggers as an unmodifiable list
-
isRootLoggerConfigured
public static boolean isRootLoggerConfigured()Checks whether the root logger has been configured.
If this returns true, other loggers were ignored or not configured.- Returns:
- True if the root logger has been configured, false otherwise
-
registerSpringFactory
public static void registerSpringFactory()Registers the spring logging configuration factory.
The factory will only be registered if the spring boot framework is found.
This method should be called as early as possible in the application lifecycle.
Ideally, in the static-initializer of the main class as first call.
Example:
public class Main { private static final Logger LOGGER; public static void main(String[] args) { // ... } static { // Register the spring logging factory here // Initialize the logging system here LOGGER = LogManager.getLogger(Main.class); } }
- See Also:
-
enable
public static void enable(@NotNull @NotNull LoggingType type, @NotNull @NotNull org.apache.logging.log4j.Level level) Enables logging for the given logging type and level.- Parameters:
type
- The logging type to enable logging forlevel
- The level to enable logging for- Throws:
NullPointerException
- If the level or logging type is nullIllegalArgumentException
- If the combination of logging type and level is invalidIllegalStateException
- If the appender for the given logging type and level was not found (file logging only)
-
enableConsole
public static void enableConsole(@NotNull @NotNull org.apache.logging.log4j.Level level) Enables console logging for the given level.- Parameters:
level
- The level to enable console logging for- Throws:
NullPointerException
- If the level is nullIllegalArgumentException
- If the level is not supported by console logging
-
enableConsole
public static void enableConsole()Enables console logging for all levels.- Throws:
IllegalArgumentException
- If a level is not supported by console logging
-
enableFile
public static void enableFile(@NotNull @NotNull org.apache.logging.log4j.Level level) Enables file logging for the given level.- Parameters:
level
- The level to enable file logging for- Throws:
NullPointerException
- If the level is nullIllegalArgumentException
- If the level is not supported by file loggingIllegalStateException
- If the appender for the given file logging level was not found
-
enableFile
public static void enableFile()Enables file logging for all levels.- Throws:
IllegalArgumentException
- If a level is not supported by file loggingIllegalStateException
- If a file logging appender was not found
-
disable
public static void disable(@NotNull @NotNull LoggingType type, @NotNull @NotNull org.apache.logging.log4j.Level level) Disables logging for the given logging type and level.- Parameters:
type
- The logging type to disable logging forlevel
- The level to disable logging for- Throws:
NullPointerException
- If the level or logging type is nullIllegalArgumentException
- If the combination of logging type and level is invalid
-
disableConsole
public static void disableConsole(@NotNull @NotNull org.apache.logging.log4j.Level level) Disables console logging for the given level.- Parameters:
level
- The level to disable console logging for- Throws:
NullPointerException
- If the level is nullIllegalArgumentException
- If the level is not supported by console logging
-
disableConsole
public static void disableConsole()Disables console logging for all levels.- Throws:
IllegalArgumentException
- If a level is not supported by console loggingIllegalStateException
- If a console logging appender was not found
-
disableFile
public static void disableFile(@NotNull @NotNull org.apache.logging.log4j.Level level) Disables file logging for the given level.- Parameters:
level
- The level to disable file logging for- Throws:
NullPointerException
- If the level is nullIllegalArgumentException
- If the level is not supported by file logging
-
disableFile
public static void disableFile()Disables file logging for all levels.- Throws:
IllegalArgumentException
- If a level is not supported by file loggingIllegalStateException
- If a file logging appender was not found
-
createRefrenceMarker
@NotNull public static @NotNull org.apache.logging.log4j.Marker createRefrenceMarker(int length) Creates a new reference marker with the given length.
The marker will be generated with a random UUID which is shortened to the given length.
The dashes of the UUID will be removed and the marker will be created with the first characters of the UUID.
The marker should be use to identify related log messages.
An example use case would be to mark log messages which are part of the same process or operation.
The process or operation may be handled by a different thread with delayed log messages,
which may causes the log messages to be out of order or being separated by other log messages.
- Parameters:
length
- The length of the reference marker- Returns:
- A new reference marker
- Throws:
IllegalArgumentException
- If the length is less than 1 or greater than 32
-
enableAppender
Enables the given appender.- Parameters:
name
- The name of the appender to enable- Throws:
NullPointerException
- If the name of the appender is nullIllegalStateException
- If the appender was not found (file logging only)
-
disableAppender
Disables the given appender.- Parameters:
name
- The name of the appender to disable
-
getLogger
@NotNull static @NotNull String getLogger(@NotNull @NotNull LoggingType type, @NotNull @NotNull org.apache.logging.log4j.Level level) Gets the name of the logger for the given logging type and level.- Parameters:
type
- The logging typelevel
- The level- Returns:
- The name of the logger in upper camel case
-
checkLevel
static void checkLevel(@NotNull @NotNull LoggingType type, @NotNull @NotNull org.apache.logging.log4j.Level level) Checks whether the given level is valid for the given logging type.
Invalid combinations are:
- The levels 'all' or 'off' with any logging type
- Logging type 'file' and level 'trace', 'warn' or 'fatal'
- Parameters:
type
- The logging type to checklevel
- The level to check- Throws:
IllegalArgumentException
- If the combination of logging type and level is invalid
-