Class LogFormatterConsole

java.lang.Object
java.util.logging.Formatter
org.nanonative.nano.helper.logger.logic.LogFormatterConsole

public class LogFormatterConsole extends Formatter
Formatter for logging messages to the console.

This formatter provides a consistent log format comprising the timestamp, log level, logger name, and message. The formatted log entries are easy to read and allow for quick scanning of log files. The log format is as follows:

 [Timestamp] [Log Level] [Logger Name] - Message
 

The formatter supports parameterized messages, allowing insertion of values at runtime. To include dynamic content in your log messages, use placeholders '{}' for automatic replacement or '%s' for manual specification. Each placeholder will be replaced with corresponding parameters provided in the logging method call.

Usage Example:

 logger.info(() -> throwable, "Processed records - success: [{}], failure: [%s], ignored; [{2}]", successCount, failureCount, ignoreCount);
 
In this example, 'successCount' replaces the first '{}' placeholder, 'failureCount' replaces the '%s' placeholder and 'ignoreCount' replaces the last [{2}] placeholder.

Note: The formatter also handles exceptions by appending the stack trace to the log entry, should an exception be thrown during execution.

  • Field Details

    • dateFormat

      protected final SimpleDateFormat dateFormat
    • paddingLogLevel

      protected final int paddingLogLevel
  • Constructor Details

    • LogFormatterConsole

      public LogFormatterConsole()
  • Method Details

    • format

      public String format(LogRecord logRecord)
      Format a LogRecord into a string representation.
      Specified by:
      format in class Formatter
      Parameters:
      logRecord - the log record to be formatted.
      Returns:
      a formatted log string.
    • formatLoggerName

      protected static String formatLoggerName(LogRecord logRecord)
    • applyCustomFormat

      protected static String applyCustomFormat(String message, Object... params)
      Replacing placeholders with parameters.
      Parameters:
      message - the message to be formatted.
      params - the parameters for the message.
      Returns:
      a string with the formatted message.