Class LoggingHelper

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

final class LoggingHelper extends Object
Helper class to configure logging from system properties.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final String
    Constant for the system property 'logging.archive.auto_deletion.age'.
    private static final String
    Constant for the system property 'logging.archive.auto_deletion.depth'.
    private static final String
    Constant for the system property 'logging.archive.compression_level'.
    private static final String
    Constant for the system property 'logging.archive.max_files'.
    private static final String
    Constant for the system property 'logging.archive.type'.
    private static final String
    Constant for the system property 'logging.config'.
    private static final String
    Constant for the system property 'logging.config.override'.
    private static final String
    Constant for the system property 'logging.console'.
    private static final String
    Constant for the system property 'logging.file'.
    private static final String
    Constant for the system property 'logging.file.folder.root'.
    private static final String
    Constant for the system property 'logging.file.size'.
    private static final String
    Constant for the system property 'logging.status_level'.
    private static final Pattern
    The pattern to match a property in the format 'key=value'.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
    This is a static helper class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Configures the current logging configuration according to the system properties.
    Only console logging is configured, due file logging must be configured at startup.
    System properties: logging.console.{level}
    Enables/disables console logging for the given level, expect 'true' or 'false'
    {level} must be replaced with a valid Level name in lower case

    Default: Enabled levels: info, warn, error, fatal Disabled levels: trace, debug Examples: logging.console.debug=true
    Enables console logging for the debug level logging.console.info=false
    Disables console logging for the info level
    private static boolean
    isDisabled(@Nullable String property)
    Checks if the given property is disabled.
    A property is disabled if it is equal to 'false', 'disable' or 'disabled', '0', 'off' or 'no' (case-insensitive).
    private static boolean
    isEnabled(@Nullable String property)
    Checks if the given property is enabled.
    A property is enabled if it is equal to 'true', 'enable' or 'enabled', '1', 'on' or 'yes' (case-insensitive).
    static @NotNull LoggerConfiguration
    load(@NotNull List<String> loggers)
    Creates a new logger configuration for the given loggers.
    The configuration is created with the default values and modifies it according to the system properties.
    General system properties:
    logging.status_level
    The status level of the internal apache logger, expect a valid Level name in lower case.
    Default: 'error'
    logging.config
    The location of the logging configuration file, expect a relative or absolute path of a file.
    If specified, the system properties will be loaded from the configuration file.
    Default: None
    logging.config.override
    Enables/disables the override of system properties by the configuration file, expect 'true' or 'false'.
    Default: 'false'
    logging.console
    Enables/disables console logging, expect 'true' or 'false'.
    Default: 'true'
    logging.file
    Enables/disables file logging, expect 'true' or 'false'.
    Default: 'true'

    Log and archive system properties:
    logging.file.size
    The maximum size of a log file, expect a number followed by a unit.
    Default: '20MB'
    logging.archive.type
    The type of the log archives, expect a file extension ('.zip', '.gz', '.bz2', '.xz').
    Default: '.gz'
    logging.archive.compression_level
    The compression level of the log archives, expect a number between 0 and 9.
    Default: '4'
    logging.archive.max_files
    The maximum number of log archives which should be kept, expect a number greater than 0.
    Default: '10'
    logging.archive.auto_deletion.depth
    The maximum depth of the log archive auto deletion, expect a number greater than 0.
    Default: '1'
    logging.archive.auto_deletion.age
    The maximum age of the log archive auto deletion, expect a number greater than 0.
    Default: '30'

    File logging system properties:
    logging.file.{level}
    Enables/disables file logging for the given level, expect 'true' or 'false'.
    {level} must be replaced with a valid Level name in lower case.

    Default: Enabled levels: None Disabled levels: Trace, Debug, Info, Warn, Error, Fatal Examples: logging.file.debug=true
    Enables file logging for the debug level
    logging.file.info=false
    Disables file logging for the info level
    logging.file.folder.root
    The root folder to log to, expect a relative or absolute path of a folder.
    Default: './'
    logging.file.folder.{level}
    The folder to a log file of a specific level, expect a relative path of a folder.
    {level} must be replaced with a valid Level name in lower case.
    Default: 'logs/'
    logging.file.folder.{level}.file
    The file name of the current log file of a specific level, expect a file name.
    {level} must be replaced with a valid Level name in lower case.
    Default: '{level}.log'
    logging.file.folder.{level}.archive
    The file pattern of the archived log files of a specific level, expect a file pattern.
    {level} must be replaced with a valid Level name in lower case.
    Default: '{level}-%d{dd-MM-yyyy}-%i.log'
    private static @NotNull LoggerConfiguration
    loadInternal(@NotNull List<String> loggers)
    Creates a new logger configuration for the given loggers.
    The configuration is created with the default values and modifies it according to the system properties.
    The system properties are specified in the documentation of the method load(List).
    private static void
    loadProperties(@NotNull ResourceLocation location)
    Loads the system properties from the given location.
    The location must be a valid file, and the properties must be in the format 'key=value'.
    If a property is already set, the override is allowed if the system property 'logging.config.override' is set to 'true'.

    Methods inherited from class java.lang.Object

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

    • PROPERTY_PATTERN

      private static final Pattern PROPERTY_PATTERN
      The pattern to match a property in the format 'key=value'.

      The key must not contain any whitespace characters.
      The value must not contain any line breaks.

    • LOGGING_LEVEL_STATUS

      private static final String LOGGING_LEVEL_STATUS
      Constant for the system property 'logging.status_level'.

      The property is used to set the status level of the internal apache logger.
      The value must be a valid Level name in lower case.

      The default value is 'error'.

      See Also:
    • LOGGING_CONFIG

      private static final String LOGGING_CONFIG
      Constant for the system property 'logging.config'.

      The property is used to set the location of the logging configuration file.
      The value must be a relative or absolute path of a file.
      If specified, the system properties will be loaded from the configuration file.

      By default, the property is not set.

      See Also:
    • LOGGING_CONFIG_OVERRIDE

      private static final String LOGGING_CONFIG_OVERRIDE
      Constant for the system property 'logging.config.override'.

      The property is used to enable/disable the override of system properties by the configuration file.
      The value must be 'true' or 'false'.
      If enabled, the system properties set with -D flag will be overridden by the configuration file.

      The default value is 'false'.

      See Also:
    • LOGGING_CONSOLE

      private static final String LOGGING_CONSOLE
      Constant for the system property 'logging.console'.

      The property is used to enable/disable console logging.
      The value must be 'true' or 'false'.

      The default value is 'true'.

      See Also:
    • LOGGING_FILE

      private static final String LOGGING_FILE
      Constant for the system property 'logging.file'.

      The property is used to enable/disable file logging.
      The value must be 'true' or 'false'.

      The default value is 'true'.

      See Also:
    • LOGGING_FILE_SIZE

      private static final String LOGGING_FILE_SIZE
      Constant for the system property 'logging.file.size'.

      The property is used to set the maximum size of a log file.
      The value must be a number followed by a unit, the unit must be one of the following:

      • 'B': bytes
      • 'KB': kilobytes
      • 'MB': megabytes
      • 'GB': gigabytes
      • 'TB': terabytes

      The default value is '20MB'.

      See Also:
    • LOGGING_FILE_FOLDER_ROOT

      private static final String LOGGING_FILE_FOLDER_ROOT
      Constant for the system property 'logging.file.folder.root'.

      The property is used to set the root folder of the log files.
      The value must be a relative or absolute path of a folder.

      The default value is './'.

      See Also:
    • LOGGING_ARCHIVE_TYPE

      private static final String LOGGING_ARCHIVE_TYPE
      Constant for the system property 'logging.archive.type'.

      The property is used to set the type of the log archives.
      The value must be one of the following:

      • '.zip': compressed with the zip algorithm
      • '.gz': compressed with the gzip algorithm
      • '.bz2': compressed with the bzip2 algorithm
      • '.xz': compressed with the xz algorithm

      The default value is '.gz'.

      See Also:
    • LOGGING_ARCHIVE_COMPRESSION_LEVEL

      private static final String LOGGING_ARCHIVE_COMPRESSION_LEVEL
      Constant for the system property 'logging.archive.compression_level'.

      The property is used to set the compression level of the log archives.
      The value must be a number between 0 and 9, a higher value means a higher compression level.
      If the value is out of range, the value will be clamped to the nearest valid value.

      The default value is 4.

      See Also:
    • LOGGING_ARCHIVE_MAX_FILES

      private static final String LOGGING_ARCHIVE_MAX_FILES
      Constant for the system property 'logging.archive.max_files'.

      The property is used to set the maximum number of log archives which should be kept.
      The value must be a number greater than 0.
      If the value is less than 1, the value will be clamped to 1.

      The default value is 10.

      See Also:
    • LOGGING_ARCHIVE_AUTO_DELETION_DEPTH

      private static final String LOGGING_ARCHIVE_AUTO_DELETION_DEPTH
      Constant for the system property 'logging.archive.auto_deletion.depth'.

      The property is used to set the maximum depth of the log archive auto deletion.
      The value must be a number greater than 0.
      If the value is less than 1, the value will be clamped to 1.

      The default value is 1.

      See Also:
    • LOGGING_ARCHIVE_AUTO_DELETION_AGE

      private static final String LOGGING_ARCHIVE_AUTO_DELETION_AGE
      Constant for the system property 'logging.archive.auto_deletion.age'.

      The property is used to set the maximum age of the log archive auto deletion.
      The value must be a number greater than 0.
      If the value is less than 1, the value will be clamped to 1.

      The default value is 30.

      See Also:
  • Constructor Details

    • LoggingHelper

      private LoggingHelper()
      Private constructor to prevent instantiation.
      This is a static helper class.
  • Method Details

    • load

      @NotNull public static @NotNull LoggerConfiguration load(@NotNull @NotNull List<String> loggers)
      Creates a new logger configuration for the given loggers.
      The configuration is created with the default values and modifies it according to the system properties.
      General system properties:
      • logging.status_level
        The status level of the internal apache logger, expect a valid Level name in lower case.
        Default: 'error'
      • logging.config
        The location of the logging configuration file, expect a relative or absolute path of a file.
        If specified, the system properties will be loaded from the configuration file.
        Default: None
      • logging.config.override
        Enables/disables the override of system properties by the configuration file, expect 'true' or 'false'.
        Default: 'false'
      • logging.console
        Enables/disables console logging, expect 'true' or 'false'.
        Default: 'true'
      • logging.file
        Enables/disables file logging, expect 'true' or 'false'.
        Default: 'true'

      Log and archive system properties:
      • logging.file.size
        The maximum size of a log file, expect a number followed by a unit.
        Default: '20MB'
      • logging.archive.type
        The type of the log archives, expect a file extension ('.zip', '.gz', '.bz2', '.xz').
        Default: '.gz'
      • logging.archive.compression_level
        The compression level of the log archives, expect a number between 0 and 9.
        Default: '4'
      • logging.archive.max_files
        The maximum number of log archives which should be kept, expect a number greater than 0.
        Default: '10'
      • logging.archive.auto_deletion.depth
        The maximum depth of the log archive auto deletion, expect a number greater than 0.
        Default: '1'
      • logging.archive.auto_deletion.age
        The maximum age of the log archive auto deletion, expect a number greater than 0.
        Default: '30'

      File logging system properties:
      • logging.file.{level}
        Enables/disables file logging for the given level, expect 'true' or 'false'.
        {level} must be replaced with a valid Level name in lower case.

        Default:
        • Enabled levels: None
        • Disabled levels: Trace, Debug, Info, Warn, Error, Fatal
        Examples:
        • logging.file.debug=true
          Enables file logging for the debug level
        • logging.file.info=false
          Disables file logging for the info level
      • logging.file.folder.root
        The root folder to log to, expect a relative or absolute path of a folder.
        Default: './'
      • logging.file.folder.{level}
        The folder to a log file of a specific level, expect a relative path of a folder.
        {level} must be replaced with a valid Level name in lower case.
        Default: 'logs/'
      • logging.file.folder.{level}.file
        The file name of the current log file of a specific level, expect a file name.
        {level} must be replaced with a valid Level name in lower case.
        Default: '{level}.log'
      • logging.file.folder.{level}.archive
        The file pattern of the archived log files of a specific level, expect a file pattern.
        {level} must be replaced with a valid Level name in lower case.
        Default: '{level}-%d{dd-MM-yyyy}-%i.log'
      Parameters:
      loggers - The names of the loggers which should be configured
      Returns:
      The logger configuration
      Throws:
      NullPointerException - If the given list of loggers is null
      InvalidValueException - If the value of a system property is invalid
      IllegalStateException - If a system property is already set and the configuration tries to override it
      See Also:
    • loadProperties

      private static void loadProperties(@NotNull @NotNull ResourceLocation location)
      Loads the system properties from the given location.
      The location must be a valid file, and the properties must be in the format 'key=value'.
      If a property is already set, the override is allowed if the system property 'logging.config.override' is set to 'true'.
      Parameters:
      location - The location of the properties file
      Throws:
      NullPointerException - If the given location is null
      IllegalStateException - If a system property is already set and the configuration tries to override it
      RuntimeException - If the properties file could not be loaded
      See Also:
    • loadInternal

      @NotNull private static @NotNull LoggerConfiguration loadInternal(@NotNull @NotNull List<String> loggers)
      Creates a new logger configuration for the given loggers.
      The configuration is created with the default values and modifies it according to the system properties.
      The system properties are specified in the documentation of the method load(List).
      Parameters:
      loggers - The names of the loggers which should be configured
      Returns:
      The logger configuration
      Throws:
      NullPointerException - If the given list of loggers is null
      InvalidValueException - If the value of a system property is invalid
      See Also:
    • configure

      public static void configure()
      Configures the current logging configuration according to the system properties.
      Only console logging is configured, due file logging must be configured at startup.
      System properties:
      • logging.console.{level}
        Enables/disables console logging for the given level, expect 'true' or 'false'
        {level} must be replaced with a valid Level name in lower case

        Default:
        • Enabled levels: info, warn, error, fatal
        • Disabled levels: trace, debug
        Examples:
        • logging.console.debug=true
          Enables console logging for the debug level
        • logging.console.info=false
          Disables console logging for the info level
      Throws:
      IllegalArgumentException - If the value of a system property is invalid
    • isEnabled

      private static boolean isEnabled(@Nullable @Nullable String property)
      Checks if the given property is enabled.
      A property is enabled if it is equal to 'true', 'enable' or 'enabled', '1', 'on' or 'yes' (case-insensitive).
      Parameters:
      property - The property to check
      Returns:
      True if the property is enabled, false otherwise
    • isDisabled

      private static boolean isDisabled(@Nullable @Nullable String property)
      Checks if the given property is disabled.
      A property is disabled if it is equal to 'false', 'disable' or 'disabled', '0', 'off' or 'no' (case-insensitive).
      Parameters:
      property - The property to check
      Returns:
      True if the property is disabled, false otherwise