Package utility

Class Logging

java.lang.Object
utility.Logging

public final class Logging extends Object
Logging utility class for printing messages to the console with different severity levels.

This class provides methods to log messages with different severity levels (INFO, DONE, ERROR, WARNING) along with timestamps. It also includes a method to print software information and a set to manage warning keys to prevent excessive logging.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Set<String>
    Set to keep track of logged warnings to avoid duplicate messages.
    static Logger
    Logger instance for the application.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Returns the current date formatted as a string.
    static String
    Returns the current timestamp formatted as a string.
    static void
    init(Level level)
    Initializes the logging system with a specified logging level.
    static void
    Logs a configuration message to the console with a timestamp.
    static void
    Logs a message indicating completion with a timestamp.
    static void
    Logs a critical exit message to the console with a timestamp.
    static void
    Logs an informational message to the console with a timestamp.
    static void
    Logs an error message to the console with a timestamp.
    static void
    Logs a warning message to the console with a timestamp.
    static void
    Logs a warning message to the console with a timestamp, but only once for each unique key.
    static void
    Prints the software information to the console.

    Methods inherited from class java.lang.Object

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

    • logDump

      public static final Set<String> logDump
      Set to keep track of logged warnings to avoid duplicate messages.
    • logger

      public static Logger logger
      Logger instance for the application.

      This logger is used to log messages for the application. It is configured to use the name of the Musial class as its identifier, which helps in categorizing and filtering log messages.

  • Constructor Details

    • Logging

      public Logging()
  • Method Details

    • init

      public static void init(Level level)
      Initializes the logging system with a specified logging level.

      This method configures the logger to use a custom `ConsoleHandler` with a formatter that styles log messages using ANSI escape codes for different log levels (INFO, CONFIG, WARNING, SEVERE). It also disables the default parent handlers to prevent duplicate logging.

      The logging level can be adjusted by passing a `Level` parameter, which determines the minimum severity of messages that will be logged.

      Parameters:
      level - The logging level to set for the logger (e.g., Level.INFO, Level.WARNING).
    • printSoftwareInfo

      public static void printSoftwareInfo()
      Prints the software information to the console.

      This method displays the software name, version, and license in a formatted manner. The output is styled using ANSI escape codes for background and text color.

      Note: The software information is retrieved from the Musial class.

    • logInfo

      public static void logInfo(String msg)
      Logs an informational message to the console with a timestamp.

      This method logs messages at the INFO level, which is typically used for general informational messages that highlight the progress of the application at a coarse-grained level. The message is automatically formatted and logged using the application's logger.

      Parameters:
      msg - The informational message to be logged.
    • logDone

      public static void logDone(String msg)
      Logs a message indicating completion with a timestamp.

      This method logs messages at the INFO level, indicating that a specific task or operation has been successfully completed. The message is formatted with a "DONE" label styled using ANSI escape codes for visual emphasis.

      Parameters:
      msg - The message indicating completion to be logged.
    • logConfig

      public static void logConfig(String msg)
      Logs a configuration message to the console with a timestamp.

      This method logs messages at the CONFIG level, which is typically used for static configuration information or messages that help in understanding the application's setup. The message is automatically formatted with a timestamp and logged using the application's logger.

      Parameters:
      msg - The configuration message to be logged.
    • logSevere

      public static void logSevere(String msg)
      Logs an error message to the console with a timestamp.

      This method logs messages at the SEVERE level, which is typically used for critical error messages that indicate a failure in the application. The message is automatically formatted and logged using the application's logger.

      Parameters:
      msg - The error message to be logged.
    • logExit

      public static void logExit(String msg)
      Logs a critical exit message to the console with a timestamp.

      This method is used to log messages indicating a critical application exit. The message is formatted with a timestamp and an "EXIT" label styled using ANSI escape codes for visual emphasis. The log level is set to SEVERE, which is the highest level of logging severity.

      Parameters:
      msg - The exit message to be logged.
    • logWarning

      public static void logWarning(String msg)
      Logs a warning message to the console with a timestamp.

      This method logs messages at the WARNING level, which is typically used to indicate potential issues or situations that require attention but are not critical errors. The message is automatically formatted and logged using the application's logger.

      Parameters:
      msg - The warning message to be logged.
    • logWarningOnce

      public static void logWarningOnce(String key, String msg)
      Logs a warning message to the console with a timestamp, but only once for each unique key.

      This method ensures that a warning message associated with a specific key is logged only once. It uses a set (`logDump`) to track keys of already logged warnings. If the key is not present in the set, the warning message is logged, and the key is added to the set.

      This is useful for avoiding repetitive logging of the same warning message.

      Parameters:
      key - The unique key identifying the warning message.
      msg - The warning message to be logged.
    • getTimestamp

      public static String getTimestamp()
      Returns the current timestamp formatted as a string.

      This method retrieves the current date and time, formats it using the specified date format, and returns it as a string.

      Returns:
      The current timestamp formatted as a string.
    • getDate

      public static String getDate()
      Returns the current date formatted as a string.

      This method retrieves the current date, formats it using the pattern "dd-MM-yy", and returns it as a string. The format includes the day, month, and year in a two-digit format.

      Returns:
      The current date formatted as "dd-MM-yy".