Class LoggerConfiguration

java.lang.Object
net.luis.utils.logging.LoggerConfiguration

public class LoggerConfiguration extends Object
Runtime logger configuration for Log4j2.

The default configuration is:
  • Console logging enabled
  • File logging enabled
A disabled logging type will not be initialized and therefore not be available and cannot be enabled later.
The default loggers are:
  • Console: INFO, WARN, ERROR, FATAL
  • File: none
Additional loggers can be added directly to the configuration or after initialization using LoggingUtils.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private 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.
    private static final Map<org.apache.logging.log4j.Level,String>
    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.
    private final List<String>
    The names of the loggers which should be configured.
    private final Map<org.apache.logging.log4j.Level,Map.Entry<String,String>>
    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

    Constructors
    Constructor
    Description
    LoggerConfiguration(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 Type
    Method
    Description
    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
    Builds the configuration.
    disableLogging(@Nullable LoggingType type)
    Disables the given logging type.
    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.
    overrideConsolePattern(@NotNull org.apache.logging.log4j.Level level, @NotNull String pattern)
    Overrides the console pattern for the given level.
    overrideDebugLog(@NotNull String file, @NotNull String archive)
    Overrides the pattern for the log file and the archived log file for log level Level.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 in logs.
    overrideErrorLog(@NotNull String file, @NotNull String archive)
    Overrides the pattern for the log file and the archived log file for log level Level.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 in logs.
    overrideFilePattern(@NotNull org.apache.logging.log4j.Level level, @NotNull String pattern)
    Overrides the file pattern for the given level.
    overrideInfoLog(@NotNull String file, @NotNull String archive)
    Overrides the pattern for the log file and the archived log file for log level Level.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 in logs.
    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 in logs.
    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 in DEFAULT_PATTERNS.
    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.
    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.
    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.
    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).
    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.
    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).
    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.
    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 './'.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NAMES

      private static final String NAMES
      The names of the log levels used in the default patterns.
      See Also:
    • DEFAULT_PATTERNS

      private static final Map<org.apache.logging.log4j.Level,String> DEFAULT_PATTERNS
      The default patterns for the log levels.
    • PATH_PATTERN

      static final String PATH_PATTERN
      Regex to check if the file is absolute or relative.
      See Also:
    • FILE_SIZE

      static final String FILE_SIZE
      Regex to check if the file size is valid.
      See Also:
    • ARCHIVE_TYPE

      static final String ARCHIVE_TYPE
      Regex to check if the archive type is valid.
      See Also:
    • DEFAULT

      public static final LoggerConfiguration DEFAULT
      The default logger configuration.
    • loggers

      private final List<String> loggers
      The names of the loggers which should be configured.
    • allowedTypes

      private final Set<LoggingType> allowedTypes
      The allowed logging types.
    • patternOverrides

      private final Map<LoggingType,Map<org.apache.logging.log4j.Level,String>> patternOverrides
      The pattern overrides for the logging types and levels.
    • defaultLoggers

      private final Map<LoggingType,List<org.apache.logging.log4j.Level>> defaultLoggers
      The default loggers for the logging types and levels.
    • logs

      private final Map<org.apache.logging.log4j.Level,Map.Entry<String,String>> logs
      The log file and archive patterns for the log levels.
    • statusLevel

      private org.apache.logging.log4j.Level statusLevel
      The status level for the internal Log4j2 logger.
    • rootDirectory

      private String rootDirectory
      The root directory for all log files.
    • fileSize

      private String fileSize
      The maximum file size for the log files.
    • archiveType

      private String archiveType
      The archive type for the log files.
    • compressionLevel

      private int compressionLevel
      The compression level for the archived log files.
    • maxArchiveFiles

      private int maxArchiveFiles
      The maximum number of archived log files which should be kept.
    • archiveAutoDeletionDepth

      private int archiveAutoDeletionDepth
      The maximum depth in which archived log files should be deleted.
    • archiveAutoDeletionAge

      private int archiveAutoDeletionAge
      The age in days after which archived log files should be deleted.
  • Constructor Details

    • LoggerConfiguration

      public LoggerConfiguration(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.
      Parameters:
      loggers - The names of the logger which should be configured
      Throws:
      NullPointerException - If the given array is null
      IllegalArgumentException - If the given list is empty or does not contain any valid logger name
      See Also:
    • LoggerConfiguration

      public LoggerConfiguration(@NotNull @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.
      Parameters:
      loggers - The names of the logger which should be configured
      Throws:
      NullPointerException - If the given list is null
      IllegalArgumentException - 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

      @NotNull public @NotNull LoggerConfiguration enableLogging(@NotNull @NotNull LoggingType type)
      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

      @NotNull public @NotNull LoggerConfiguration disableLogging(@Nullable @Nullable LoggingType type)
      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 in DEFAULT_PATTERNS.
      Parameters:
      type - The logging type to override the pattern for (LoggingType.CONSOLE or LoggingType.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 null
      IllegalArgumentException - 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 null
      IllegalArgumentException - 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 null
      IllegalArgumentException - 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 null
      IllegalArgumentException - 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 in logs.
      Parameters:
      level - The level to override the log file for
      file - The pattern for the current log file
      archive - The pattern for all archived log files
      Returns:
      The current configuration builder
      Throws:
      NullPointerException - If the given level, file name or file pattern is null
      IllegalArgumentException - 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 level Level.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 in logs.
      Parameters:
      file - The pattern for the current log file
      archive - The pattern for all archived log files
      Returns:
      The current configuration builder
      Throws:
      NullPointerException - If the given file name or file pattern is null
      IllegalArgumentException - 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 level Level.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 in logs.
      Parameters:
      file - The pattern for the current log file
      archive - The pattern for all archived log files
      Returns:
      The current configuration builder
      Throws:
      NullPointerException - If the given file name or file pattern is null
      IllegalArgumentException - 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 level Level.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 in logs.
      Parameters:
      file - The pattern for the current log file
      archive - The pattern for all archived log files
      Returns:
      The current configuration builder
      Throws:
      NullPointerException - If the given file name or file pattern is null
      IllegalArgumentException - 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 null
      IllegalArgumentException - If the given level is not Level.DEBUG, Level.INFO or Level.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 in LoggingUtils.

      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 in LoggingUtils.

      Parameters:
      type - The type to add the default logger for
      level - The level to add the default logger for
      Returns:
      The current configuration builder
      Throws:
      NullPointerException - If the given type or level is null
      IllegalArgumentException - 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 in LoggingUtils.

      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 in LoggingUtils.

      Parameters:
      type - The type to remove the default logger for
      level - The level to remove the default logger for
      Returns:
      The current configuration builder
    • setFileSize

      @NotNull public @NotNull LoggerConfiguration setFileSize(@NotNull @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).
      Parameters:
      fileSize - The file size to set
      Returns:
      The current configuration builder
      Throws:
      NullPointerException - If the given file size is null
      IllegalArgumentException - If the given file size is not valid
    • setArchiveType

      @NotNull public @NotNull LoggerConfiguration setArchiveType(@NotNull @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).
      Parameters:
      archiveType - The archive type to set
      Returns:
      The current configuration builder
      Throws:
      NullPointerException - If the given archive type is null
      IllegalArgumentException - If the given archive type is not valid
    • setCompressionLevel

      @NotNull public @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.
      Parameters:
      compressionLevel - The compression level to set
      Returns:
      The current configuration builder
    • setMaxArchiveFiles

      @NotNull public @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.
      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

      @NotNull public @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.
      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 for
      level - 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