Package ca.corbett.extras.logging
Class LogConsole
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.swing.JFrame
ca.corbett.extras.logging.LogConsole
- All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,EventListener,Accessible,ChangeListener,RootPaneContainer,WindowConstants
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JFrame
JFrame.AccessibleJFrameNested classes/interfaces inherited from class java.awt.Frame
Frame.AccessibleAWTFrameNested classes/interfaces inherited from class java.awt.Window
Window.AccessibleAWTWindow, Window.TypeNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intHow many log messages to store in memory, or 0 for no limit.Fields inherited from class javax.swing.JFrame
accessibleContext, rootPane, rootPaneCheckingEnabledFields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSORFields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTHFields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, EXIT_ON_CLOSE, HIDE_ON_CLOSE -
Method Summary
Modifier and TypeMethodDescriptionvoidaddLogConsoleListener(LogConsoleListener listener) voidAppends a log message to this console with the given log level.voidclear()Removes all log messages from the console.Returns the name of the effective LogConsoleTheme.intReturns the font size currently in use in the LogConsole.static LogConsoleReturns the single instance of LogConsole.Returns an alphabetized list of theme names currently registered with this LogConsole.voidregisterTheme(String name, LogConsoleTheme theme) Registers the given LogConsoleTheme for use with this LogConsole and then immediately switches to it.voidregisterTheme(String name, LogConsoleTheme theme, boolean switchToImmediately) Registers the given LogConsoleTheme, and then optionally switches to it.voidremoveLogConsoleListener(LogConsoleListener listener) voidsetFontPointSize(int size) Sets the current font size for the LogConsole.voidsetVisible(boolean visible) voidvoidswitchTheme(String themeName) Switches to the named theme, if that theme is registered with this LogConsole.voidunregisterTheme(String name) Unregisters the given theme, and switches back to the DEFAULT theme if the given theme was the current theme.Methods inherited from class javax.swing.JFrame
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, updateMethods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setBackground, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setOpacity, setResizable, setShape, setState, setTitle, setUndecoratedMethods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBackground, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOpacity, getOwnedWindows, getOwner, getOwnerlessWindows, getShape, getToolkit, getType, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isAutoRequestFocus, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isOpaque, isShowing, isValidateRoot, pack, paint, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setAutoRequestFocus, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocation, setLocation, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setType, show, toBack, toFrontMethods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, validate, validateTreeMethods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, resize, resize, revalidate, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setMaximumSize, setMixingCutoutShape, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.awt.MenuContainer
getFont, postEvent
-
Field Details
-
LOG_LIMIT
public static int LOG_LIMITHow many log messages to store in memory, or 0 for no limit.
-
-
Method Details
-
addLogConsoleListener
-
removeLogConsoleListener
-
getRegisteredThemeNames
Returns an alphabetized list of theme names currently registered with this LogConsole.- Returns:
- A list of registered theme names.
-
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
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
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
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
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
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:
setVisiblein classWindow
-
append
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
- Specified by:
stateChangedin interfaceChangeListener
-