Class LogConsole

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, ChangeListener, RootPaneContainer, WindowConstants

public final class LogConsole extends JFrame implements ChangeListener
A singleton LogConsole window that works with LogConsoleHandler to display log messages within an application (viewable via the AboutPanel or by showing it programmatically). Works transparently with any application, provided the logging configuration is set up for it: you must specify "ca.corbett.extras.logging.LogConsoleHandler" in the "handlers" list in your logging.properties in order for LogConsole to receive log messages. Alternatively, you can skip logging.properties and java.util.logging altogether and log directly to LogConsole via the append() method.

Somewhere in your startup code, you can set an ImageIcon and a title for the window (default is no icon and "Log console" title):

LogConsole.getInstance().setIconImage(Toolkit.getDefaultToolkit().createImage("/some/image.png"));
LogConsole.getInstance().setTitle(Version.NAME + " log console");

You also have the option of specifying custom styling for your application's log messages, based either on the log.Level of each log message, or by looking for some string token within each log message. See LogConsoleTheme and LogConsoleStyle for details on setting up custom log styling.

Out of the box, LogConsole supports three themes: default (black text on white background), matrix (green text on black background), and paper (black text on grey background). These are provided as conveniences, or you can add your own theme by creating and configuring an instance of this class.

Since:
2023-03-17
Author:
scorbo2
See Also:
  • Field Details

    • LOG_LIMIT

      public static int LOG_LIMIT
      How many log messages to store in memory, or 0 for no limit.
  • Method Details

    • addLogConsoleListener

      public void addLogConsoleListener(LogConsoleListener listener)
    • removeLogConsoleListener

      public void removeLogConsoleListener(LogConsoleListener listener)
    • getRegisteredThemeNames

      public SortedSet<String> getRegisteredThemeNames()
      Returns an alphabetized list of theme names currently registered with this LogConsole.
      Returns:
      A list of registered theme names.
    • getCurrentThemeName

      public String getCurrentThemeName()
      Returns the name of the effective LogConsoleTheme.
      Returns:
      The name of the current theme.
    • getFontPointSize

      public int getFontPointSize()
      Returns the font size currently in use in the LogConsole.
      Returns:
      a font point size.
    • setFontPointSize

      public void setFontPointSize(int size)
      Sets the current font size for the LogConsole.
      Parameters:
      size - The new font point size.
    • registerTheme

      public void registerTheme(String name, LogConsoleTheme theme)
      Registers the given LogConsoleTheme for use with this LogConsole and then immediately switches to it.
      Parameters:
      name - The name of the theme to register. Will overwrite if this theme exists.
      theme - The new theme.
    • registerTheme

      public void registerTheme(String name, LogConsoleTheme theme, boolean switchToImmediately)
      Registers the given LogConsoleTheme, and then optionally switches to it.
      Parameters:
      name - THe name of the theme to register. Will overwrite if this theme exists.
      theme - The new theme.
      switchToImmediately - If true, LogConsole will switch to the new theme immediately.
    • unregisterTheme

      public void unregisterTheme(String name)
      Unregisters the given theme, and switches back to the DEFAULT theme if the given theme was the current theme. Note that you cannot unregister the DEFAULT theme (though you can unregister the other built-in themes if you want). If no theme is registered by the given name, this method does nothing.
      Parameters:
      name - The name of the theme to unregister. Cannot be "Default".
    • switchTheme

      public void switchTheme(String themeName)
      Switches to the named theme, if that theme is registered with this LogConsole. Does nothing if the given theme name is null or unrecognized.
      Parameters:
      themeName - The name of the theme in question.
    • getInstance

      public static LogConsole getInstance()
      Returns the single instance of LogConsole.
      Returns:
      The LogConsole instance.
    • clear

      public void clear()
      Removes all log messages from the console.
    • setVisible

      public void setVisible(boolean visible)
      Overrides:
      setVisible in class Window
    • append

      public void append(String msg, Level level)
      Appends a log message to this console with the given log level. Intended to be invoked by LogConsoleHandler automatically when log messages are received, but can also be invoked manually if you are not using the usual java.util.logging mechanism.
      Parameters:
      msg - The message to log.
      level - The log level.
    • stateChanged

      public void stateChanged(ChangeEvent e)
      Specified by:
      stateChanged in interface ChangeListener