Class LoggingUtils

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

public final class LoggingUtils extends Object
Logging utility class.

This class is used to initialize or load a logging configuration.
It also provides methods to enable/disable logging.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static LoggerConfiguration
    The current configuration or null if the logging system has not been initialized.
    private static final List<String>
    A list of all loggers which are configured.
    private static boolean
    Whether the cached factory has been registered or not.
  • 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
    (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
    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.
    static @Unmodifiable @NotNull List<String>
    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
    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
    load(String @NotNull ... loggers)
    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
    load(@NotNull List<String> loggers)
    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.

    Methods inherited from class java.lang.Object

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

    • CONFIGURED_LOGGERS

      private static final List<String> CONFIGURED_LOGGERS
      A list of all loggers which are configured.
    • configuration

      private static LoggerConfiguration configuration
      The current configuration or null if the logging system has not been initialized.
    • registeredFactory

      private static boolean registeredFactory
      Whether 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

      public static void load(String @NotNull ... loggers)
      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 null
      IllegalArgumentException - If the logger list is empty or contains no valid logger
      IllegalStateException - If the logging system has already been initialized
      See Also:
    • load

      public static void load(@NotNull @NotNull List<String> loggers)
      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 null
      IllegalArgumentException - If the logger list is empty or contains no valid logger
      IllegalStateException - If the logging system has already been initialized
      See Also:
    • initialize

      public static void initialize(@NotNull @NotNull LoggerConfiguration configuration)
      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 null
      IllegalStateException - 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 use
      override - Whether to override the current configuration if the logging system has already been initialized
      Throws:
      NullPointerException - If the configuration is null
      IllegalStateException - If the logging system has already been initialized and override is false
      See Also:
    • initializeSafe

      public static boolean initializeSafe(@NotNull @NotNull LoggerConfiguration configuration)
      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

      private static void initializeInternal(@NotNull @NotNull LoggerConfiguration configuration)
      Initializes the log4j2 configurator with the given configuration.
      Parameters:
      configuration - The configuration to use
      Throws:
      NullPointerException - If the configuration is null
    • reconfigure

      public static void reconfigure(@NotNull @NotNull LoggerConfiguration configuration)
      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

      public static void initializeOrReconfigure(@NotNull @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.
      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

      @Nullable public static @Nullable LoggerConfiguration getConfiguration()
      Returns the current configuration of the logging system.
      Returns:
      The current configuration or null if the logging system has not been initialized
    • getConfiguredLoggers

      @NotNull public static @Unmodifiable @NotNull List<String> 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 for
      level - The level to enable logging for
      Throws:
      NullPointerException - If the level or logging type is null
      IllegalArgumentException - If the combination of logging type and level is invalid
      IllegalStateException - 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 null
      IllegalArgumentException - 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 null
      IllegalArgumentException - If the level is not supported by file logging
      IllegalStateException - 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 logging
      IllegalStateException - 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 for
      level - The level to disable logging for
      Throws:
      NullPointerException - If the level or logging type is null
      IllegalArgumentException - 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 null
      IllegalArgumentException - 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 logging
      IllegalStateException - 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 null
      IllegalArgumentException - 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 logging
      IllegalStateException - 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

      private static void enableAppender(@NotNull @NotNull String name)
      Enables the given appender.
      Parameters:
      name - The name of the appender to enable
      Throws:
      NullPointerException - If the name of the appender is null
      IllegalStateException - If the appender was not found (file logging only)
    • disableAppender

      private static void disableAppender(@NotNull @NotNull String name)
      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 type
      level - 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 check
      level - The level to check
      Throws:
      IllegalArgumentException - If the combination of logging type and level is invalid