Class LogHelper

java.lang.Object
nl.colorize.util.LogHelper

public final class LogHelper extends Object
Helper methods for initializing and configuring java.util.logging loggers programmatically. A default logging configuration is automatically applied to all logger in the nl.colorize namespace.
  • Method Details

    • getLogger

      public static Logger getLogger(Class<?> name)
      Obtains the logger with the name of the specified class. The logger is returned with its existing, possibly inherited, configuration.
    • getLogger

      public static Logger getLogger(String name)
      Obtains the logger with the specified name. The logger is returned with its existing, possibly inherited, configuration.
    • createConsoleHandler

      public static ConsoleHandler createConsoleHandler()
      Creates a log handler that will log to stderr. The handler will use the platform's default character encoding.
    • createPlainConsoleHandler

      @Deprecated public static ConsoleHandler createPlainConsoleHandler()
      Deprecated.
      Use createConsoleHandler() in combination with a CompactFormatter instead.
      Creates a log handler that will print messages to stderr and will not apply any formatting to messages.
    • createFileHandler

      public static FileHandler createFileHandler(File logfile)
      Creates a log handler that will log to the specified file using the platform's default character encoding.
      Throws:
      IllegalArgumentException - if the file is not writeable.
    • createFileHandler

      public static FileHandler createFileHandler(File logfile, Charset encoding)
      Creates a log handler that will log to the specified file.
      Throws:
      IllegalArgumentException - if the file is not writeable.
    • createCallbackHandler

      public static Handler createCallbackHandler(Consumer<Throwable> callback)
      Creates a log handler that will invoke the specified callback function for every log message that contains an attached Throwable. The log level is initially set to SEVERE.
    • createStringHandler

      public static Handler createStringHandler(StringWriter stringWriter, Formatter formatter)
      Creates a log handler that will write log messages to the specified StringWriter.
    • createStringHandler

      public static Handler createStringHandler(StringWriter stringWriter)
      Creates a log handler that will write log messages to the specified StringWriter and uses a compact formatter.
    • createCompactFormatter

      public static Formatter createCompactFormatter()
      Creates a log message formatter that uses a compact format with log records only taking up a single line. This makes it easier to process log files in other tools, or scan them manually.

      Messages are logged using the default time zone for date and time. See Platform.getDefaultTimeZone() for more information.

    • getStackTrace

      public static String getStackTrace(Throwable e)
      Returns the strack trace for the specified exception as a string.

      When running on TeaVM, the "native" Java stack trace is not available, and this method will instead return the exception message.