The default configuration is:
- Console logging enabled
- File logging enabled
The default loggers are:
- Console: INFO, WARN, ERROR, FATAL
- File: none
LoggingUtils
.- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Set
<LoggingType> The allowed logging types.(package private) static final String
Regex to check if the archive type is valid.private int
The age in days after which archived log files should be deleted.private int
The maximum depth in which archived log files should be deleted.private String
The archive type for the log files.private int
The compression level for the archived log files.static final LoggerConfiguration
The default logger configuration.The default patterns for the log levels.private final Map
<LoggingType, List<org.apache.logging.log4j.Level>> The default loggers for the logging types and levels.(package private) static final String
Regex to check if the file size is valid.private String
The maximum file size for the log files.The names of the loggers which should be configured.The log file and archive patterns for the log levels.private int
The maximum number of archived log files which should be kept.private static final String
The names of the log levels used in the default patterns.(package private) static final String
Regex to check if the file is absolute or relative.private final Map
<LoggingType, Map<org.apache.logging.log4j.Level, String>> The pattern overrides for the logging types and levels.private String
The root directory for all log files.private org.apache.logging.log4j.Level
The status level for the internal Log4j2 logger. -
Constructor Summary
ConstructorsConstructorDescriptionLoggerConfiguration
(String @NotNull ... loggers) Constructs a new logger configuration with the specified logger name.
The logger name is used to identify the logger in the configuration.
The logger name must be the package name, the full class name, or a '*' to include all loggers.
If the list contains a '*', all other logger names will be ignored.LoggerConfiguration
(@NotNull List<String> loggers) Constructs a new logger configuration with the specified logger name.
The logger name is used to identify the logger in the configuration.
The logger name must be the package name, the full class name or a '*' to include all loggers.
If the list contains a '*', all other logger names will be ignored. -
Method Summary
Modifier and TypeMethodDescription@NotNull LoggerConfiguration
addDefaultLogger
(@NotNull LoggingType type, @NotNull org.apache.logging.log4j.Level level) Adds the default logger for the given type and level.
All default loggers will be automatically enabled at startup.@NotNull org.apache.logging.log4j.core.config.Configuration
build()
Builds the configuration.@NotNull LoggerConfiguration
disableLogging
(@Nullable LoggingType type) Disables the given logging type.@NotNull LoggerConfiguration
enableLogging
(@NotNull LoggingType type) Enables the given logging type.private @NotNull String
getPattern
(@NotNull LoggingType type, @NotNull org.apache.logging.log4j.Level level) Gets the pattern for the given type and level.@NotNull LoggerConfiguration
overrideConsolePattern
(@NotNull org.apache.logging.log4j.Level level, @NotNull String pattern) Overrides the console pattern for the given level.@NotNull LoggerConfiguration
overrideDebugLog
(@NotNull String file, @NotNull String archive) Overrides the pattern for the log file and the archived log file for log levelLevel.DEBUG
.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.@NotNull LoggerConfiguration
overrideErrorLog
(@NotNull String file, @NotNull String archive) Overrides the pattern for the log file and the archived log file for log levelLevel.ERROR
.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.@NotNull LoggerConfiguration
overrideFilePattern
(@NotNull org.apache.logging.log4j.Level level, @NotNull String pattern) Overrides the file pattern for the given level.@NotNull LoggerConfiguration
overrideInfoLog
(@NotNull String file, @NotNull String archive) Overrides the pattern for the log file and the archived log file for log levelLevel.INFO
.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.@NotNull LoggerConfiguration
overrideLog
(@NotNull org.apache.logging.log4j.Level level, @NotNull String file, @NotNull String archive) Overrides the pattern for the log file and the archived log file for the given log level.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.@NotNull LoggerConfiguration
overridePattern
(@NotNull LoggingType type, @NotNull org.apache.logging.log4j.Level level, @NotNull String pattern) Overrides the pattern for the given logging type and level.
Default patterns can be found inDEFAULT_PATTERNS
.@NotNull LoggerConfiguration
removeDefaultLogger
(@Nullable LoggingType type, @Nullable org.apache.logging.log4j.Level level) Removes the default logger for the given type and level.
Removed default loggers will not be automatically enabled at startup.@NotNull LoggerConfiguration
setArchiveAutoDeletionAge
(int archiveAutoDeletionAge) Sets the age in days after which archived log files should be deleted.
The maximum depth must be greater than 0.
If the value is less than 1, it will be set to 1.@NotNull LoggerConfiguration
setArchiveAutoDeletionDepth
(int archiveAutoDeletionDepth) Sets the maximum depth in which archived log files should be deleted.
The maximum depth must be greater than 0.
If the value is less than 1, it will be set to 1.@NotNull LoggerConfiguration
setArchiveType
(@NotNull String archiveType) Sets the archive type for the archived log files.
The archive type must be '.gz', '.zip', '.bz2' or '.xy' (dot is optional, case-insensitive).@NotNull LoggerConfiguration
setCompressionLevel
(int compressionLevel) Sets the compression level for the archived log files.
The compression level must be between 0 and 9.
If the value is out of bounds, it will be clamped to the nearest bound.@NotNull LoggerConfiguration
setFileSize
(@NotNull String fileSize) Sets the maximum file size for the log files.
The file size must be a number followed by an optional unit (KB, MB, GB, TB).@NotNull LoggerConfiguration
setMaxArchiveFiles
(int maxArchiveFiles) Sets the maximum number of archived log files which should be kept.
The maximum number of archived log files must be greater than 0.
If the value is less than 1, it will be set to 1.@NotNull LoggerConfiguration
setRootDirectory
(@NotNull String rootDirectory) Sets the root directory for all log files.
The root directory is the directory where the log files will be stored.
The default root directory is './'.@NotNull LoggerConfiguration
setStatusLevel
(@NotNull org.apache.logging.log4j.Level level) Sets the status level for the internal Log4j2 logger.private void
validateLevel
(@NotNull org.apache.logging.log4j.Level level) Validates the given level.
-
Field Details
-
NAMES
The names of the log levels used in the default patterns.- See Also:
-
DEFAULT_PATTERNS
The default patterns for the log levels. -
PATH_PATTERN
Regex to check if the file is absolute or relative.- See Also:
-
FILE_SIZE
Regex to check if the file size is valid.- See Also:
-
ARCHIVE_TYPE
Regex to check if the archive type is valid.- See Also:
-
DEFAULT
The default logger configuration. -
loggers
The names of the loggers which should be configured. -
allowedTypes
The allowed logging types. -
patternOverrides
The pattern overrides for the logging types and levels. -
defaultLoggers
The default loggers for the logging types and levels. -
logs
The log file and archive patterns for the log levels. -
statusLevel
private org.apache.logging.log4j.Level statusLevelThe status level for the internal Log4j2 logger. -
rootDirectory
The root directory for all log files. -
fileSize
The maximum file size for the log files. -
archiveType
The archive type for the log files. -
compressionLevel
private int compressionLevelThe compression level for the archived log files. -
maxArchiveFiles
private int maxArchiveFilesThe maximum number of archived log files which should be kept. -
archiveAutoDeletionDepth
private int archiveAutoDeletionDepthThe maximum depth in which archived log files should be deleted. -
archiveAutoDeletionAge
private int archiveAutoDeletionAgeThe age in days after which archived log files should be deleted.
-
-
Constructor Details
-
LoggerConfiguration
Constructs a new logger configuration with the specified logger name.
The logger name is used to identify the logger in the configuration.
The logger name must be the package name, the full class name, or a '*' to include all loggers.
If the list contains a '*', all other logger names will be ignored.- Parameters:
loggers
- The names of the logger which should be configured- Throws:
NullPointerException
- If the given array is nullIllegalArgumentException
- If the given list is empty or does not contain any valid logger name- See Also:
-
LoggerConfiguration
Constructs a new logger configuration with the specified logger name.
The logger name is used to identify the logger in the configuration.
The logger name must be the package name, the full class name or a '*' to include all loggers.
If the list contains a '*', all other logger names will be ignored.- Parameters:
loggers
- The names of the logger which should be configured- Throws:
NullPointerException
- If the given list is nullIllegalArgumentException
- If the given list is empty or does not contain any valid logger name
-
-
Method Details
-
setStatusLevel
@NotNull public @NotNull LoggerConfiguration setStatusLevel(@NotNull @NotNull org.apache.logging.log4j.Level level) Sets the status level for the internal Log4j2 logger.- Parameters:
level
- The level to set- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given level is null
-
enableLogging
Enables the given logging type.- Parameters:
type
- The type to enable- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given type is null
-
disableLogging
Disables the given logging type.- Parameters:
type
- The type to disable- Returns:
- The current configuration builder
-
overridePattern
@NotNull public @NotNull LoggerConfiguration overridePattern(@NotNull @NotNull LoggingType type, @NotNull @NotNull org.apache.logging.log4j.Level level, @NotNull @NotNull String pattern) Overrides the pattern for the given logging type and level.
Default patterns can be found inDEFAULT_PATTERNS
.- Parameters:
type
- The logging type to override the pattern for (LoggingType.CONSOLE
orLoggingType.FILE
)level
- The level to override the pattern for (Level.ALL
overrides all levels,Level.OFF
clears all overrides)pattern
- The pattern to use instead of the default one- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given type, level or pattern is nullIllegalArgumentException
- If the given pattern is empty or does not contain any of the following placeholders: %m, %msg, %message, %throwable, %ex, %exception
-
overrideConsolePattern
@NotNull public @NotNull LoggerConfiguration overrideConsolePattern(@NotNull @NotNull org.apache.logging.log4j.Level level, @NotNull @NotNull String pattern) Overrides the console pattern for the given level.- Parameters:
level
- The level to override the pattern for (Level.ALL
overrides all levels,Level.OFF
clears all overrides)pattern
- The pattern to use instead of the default one- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given level or pattern is nullIllegalArgumentException
- If the given pattern is empty or does not contain any of the following placeholders: %m, %msg, %message, %throwable, %ex, %exception- See Also:
-
overrideFilePattern
@NotNull public @NotNull LoggerConfiguration overrideFilePattern(@NotNull @NotNull org.apache.logging.log4j.Level level, @NotNull @NotNull String pattern) Overrides the file pattern for the given level.- Parameters:
level
- The level to override the pattern for (Level.ALL
overrides all levels,Level.OFF
clears all overrides)pattern
- The pattern to use instead of the default one- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given level or pattern is nullIllegalArgumentException
- If the given pattern is empty or does not contain any of the following placeholders: %m, %msg, %message, %throwable, %ex, %exception- See Also:
-
setRootDirectory
@NotNull public @NotNull LoggerConfiguration setRootDirectory(@NotNull @NotNull String rootDirectory) Sets the root directory for all log files.
The root directory is the directory where the log files will be stored.
The default root directory is './'.- Parameters:
rootDirectory
- The root directory to set, the path must be relative or absolute- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given root folder is nullIllegalArgumentException
- If the given root folder is empty or not relative or absolute
-
overrideLog
@NotNull public @NotNull LoggerConfiguration overrideLog(@NotNull @NotNull org.apache.logging.log4j.Level level, @NotNull @NotNull String file, @NotNull @NotNull String archive) Overrides the pattern for the log file and the archived log file for the given log level.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.- Parameters:
level
- The level to override the log file forfile
- The pattern for the current log filearchive
- The pattern for all archived log files- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given level, file name or file pattern is nullIllegalArgumentException
- If the given level is not valid, or if the given files are empty, absolute or relative
-
overrideDebugLog
@NotNull public @NotNull LoggerConfiguration overrideDebugLog(@NotNull @NotNull String file, @NotNull @NotNull String archive) Overrides the pattern for the log file and the archived log file for log levelLevel.DEBUG
.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.- Parameters:
file
- The pattern for the current log filearchive
- The pattern for all archived log files- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given file name or file pattern is nullIllegalArgumentException
- If the given level is not valid, or if the given files are empty, absolute or relative- See Also:
-
overrideInfoLog
@NotNull public @NotNull LoggerConfiguration overrideInfoLog(@NotNull @NotNull String file, @NotNull @NotNull String archive) Overrides the pattern for the log file and the archived log file for log levelLevel.INFO
.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.- Parameters:
file
- The pattern for the current log filearchive
- The pattern for all archived log files- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given file name or file pattern is nullIllegalArgumentException
- If the given level is not valid, or if the given files are empty or absolute- See Also:
-
overrideErrorLog
@NotNull public @NotNull LoggerConfiguration overrideErrorLog(@NotNull @NotNull String file, @NotNull @NotNull String archive) Overrides the pattern for the log file and the archived log file for log levelLevel.ERROR
.
If the root directory is not set, the default root directory ('./') is used.
If the root directory has been set, the given file and archive will be appended to the root directory.
The file and archive must not be absolute or relative.
The log file will be built as follows:rootDirectory + (file|archive + "." + archive_type)
Default patterns can be found inlogs
.- Parameters:
file
- The pattern for the current log filearchive
- The pattern for all archived log files- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given file name or file pattern is nullIllegalArgumentException
- If the given level is not valid, or if the given files are empty, absolute or relative- See Also:
-
validateLevel
private void validateLevel(@NotNull @NotNull org.apache.logging.log4j.Level level) Validates the given level.- Parameters:
level
- The level to validate- Throws:
NullPointerException
- If the given level is nullIllegalArgumentException
- If the given level is notLevel.DEBUG
,Level.INFO
orLevel.ERROR
-
addDefaultLogger
@NotNull public @NotNull LoggerConfiguration addDefaultLogger(@NotNull @NotNull LoggingType type, @NotNull @NotNull org.apache.logging.log4j.Level level) Adds the default logger for the given type and level.
All default loggers will be automatically enabled at startup.
Console loggers which are not configured at startup can be configured later using
the enable and disable methods inLoggingUtils
.
File loggers which are not configured at startup are not available and can not be configured later.
If a file logger is configured at startup, it can be configured later using
the enable and disable methods inLoggingUtils
.
- Parameters:
type
- The type to add the default logger forlevel
- The level to add the default logger for- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given type or level is nullIllegalArgumentException
- If the given type is not allowed
-
removeDefaultLogger
@NotNull public @NotNull LoggerConfiguration removeDefaultLogger(@Nullable @Nullable LoggingType type, @Nullable @Nullable org.apache.logging.log4j.Level level) Removes the default logger for the given type and level.
Removed default loggers will not be automatically enabled at startup.
Console loggers which are not configured at startup can be configured later using
the enable and disable methods inLoggingUtils
.
File loggers which are not configured at startup are not available and can not be configured later.
If a file logger is configured at startup, it can be configured later using
the enable and disable methods inLoggingUtils
.
- Parameters:
type
- The type to remove the default logger forlevel
- The level to remove the default logger for- Returns:
- The current configuration builder
-
setFileSize
Sets the maximum file size for the log files.
The file size must be a number followed by an optional unit (KB, MB, GB, TB).- Parameters:
fileSize
- The file size to set- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given file size is nullIllegalArgumentException
- If the given file size is not valid
-
setArchiveType
Sets the archive type for the archived log files.
The archive type must be '.gz', '.zip', '.bz2' or '.xy' (dot is optional, case-insensitive).- Parameters:
archiveType
- The archive type to set- Returns:
- The current configuration builder
- Throws:
NullPointerException
- If the given archive type is nullIllegalArgumentException
- If the given archive type is not valid
-
setCompressionLevel
Sets the compression level for the archived log files.
The compression level must be between 0 and 9.
If the value is out of bounds, it will be clamped to the nearest bound.- Parameters:
compressionLevel
- The compression level to set- Returns:
- The current configuration builder
-
setMaxArchiveFiles
Sets the maximum number of archived log files which should be kept.
The maximum number of archived log files must be greater than 0.
If the value is less than 1, it will be set to 1.- Parameters:
maxArchiveFiles
- The maximum number of archived log files to keep- Returns:
- The current configuration builder
-
setArchiveAutoDeletionDepth
@NotNull public @NotNull LoggerConfiguration setArchiveAutoDeletionDepth(int archiveAutoDeletionDepth) Sets the maximum depth in which archived log files should be deleted.
The maximum depth must be greater than 0.
If the value is less than 1, it will be set to 1.- Parameters:
archiveAutoDeletionDepth
- The depth in which archived log files should be deleted- Returns:
- The current configuration builder
-
setArchiveAutoDeletionAge
Sets the age in days after which archived log files should be deleted.
The maximum depth must be greater than 0.
If the value is less than 1, it will be set to 1.- Parameters:
archiveAutoDeletionAge
- The age in days after which archived log files should be deleted- Returns:
- The current configuration builder
-
build
@NotNull public @NotNull org.apache.logging.log4j.core.config.Configuration build()Builds the configuration.- Returns:
- The built configuration
-
getPattern
@NotNull private @NotNull String getPattern(@NotNull @NotNull LoggingType type, @NotNull @NotNull org.apache.logging.log4j.Level level) Gets the pattern for the given type and level.- Parameters:
type
- The type to get the pattern forlevel
- The level to get the pattern for- Returns:
- The pattern for the given type and level, or the default pattern if there is no override
-