Package ca.corbett.extras.logging
Class LogConsoleTheme
java.lang.Object
ca.corbett.extras.properties.AbstractProperty
ca.corbett.extras.logging.LogConsoleTheme
- All Implemented Interfaces:
EventListener,ChangeListener
A LogConsoleTheme represents a set of LogConsoleStyle objects that can be applied
within the LogConsole.
Styles
Every LogConsoleTheme contains at minimum one LogConsoleStyle - the "Default" style can be customized but cannot be removed. If no other styles are specified, then all log messages in the LogConsole will be displayed in that default style when used with this theme.You can specify additional styles either by selecting one of the pre-built themes via the create...() factory methods in this class, or by specifying them manually by creating and configuring one or more LogConsoleStyle instances and handing them to this theme via the setStyle() method. There is no limit to the number of styles a theme can have, but note the matching rules below.
Matching styles to log messages
The LogConsole will ask the current theme for a matching LogConsoleStyle to use for each log message that comes in. Style matching is done either by matching a log Level, or by matching a string token that appears in a log message, or both.- If a log message matches a style's log token and its log level, that style is considered a strong match for that log message.
- If a log message matches a style's log token, but the style doesn't specify a log level, it is still considered a strong match.
- If a log message matches a style's log level, but the style doesn't specify a log token, then this is considered a weak match. That means that this style will match only if no other style matches the log message.
- If no style matches, either by log token or by log level, then the default style will be used for that log message.
Persisting a custom style
LogConsoleTheme (and also LogConsoleStyle) extend ConfigObject, so they can be easily persisted to disk using a FileBasedProperties instance. It is recommended to use a prefix when doing so to differentiate different styles in the same properties file.Listening for style changes
LogConsoleTheme accepts ChangeListeners, and will send out a change message whenever any style properties are changed, or when styles are added or removed.- Since:
- 2023-03-17
- Author:
- scorbo2
-
Field Summary
FieldsFields inherited from class ca.corbett.extras.properties.AbstractProperty
categoryName, DEFAULT_CATEGORY, DEFAULT_PROPERTY_NAME, extraAttributes, fullyQualifiedName, helpText, isEnabled, isExposed, isInitiallyEditable, isInitiallyVisible, propertyLabel, propertyName, subCategoryName -
Constructor Summary
ConstructorsConstructorDescriptionLogConsoleTheme(String fullyQualifiedName) Creates a plain theme with black text on a white background, and no styled matchers for any particular log messages. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChangeListener(ChangeListener listener) Register to receive notifications when any style properties are changed, added, or removed from this theme.voidclear()Clears all log styles except for the Default style, and resets the default background color to white.static LogConsoleThemeCreates and returns a LogConsoleTheme with some basic log matchers.static LogConsoleThemeCreates and returns a "matrix"-style LogConsoleTheme, with a black background and green text.static LogConsoleThemeCreates and returns a "paper" style LogConsoleTheme, with mostly black text on a grey background.static LogConsoleThemeCreates and returns a plain theme with black text and no matchers.protected FormFieldDescendant classes must implement this method to generate a FormField associated with this property.Returns the default background color - this is the background that will be used unless specifically overridden by some style matcher.getMatchingStyle(String logMsg, Level logLevel) Returns the appropriate LogConsoleStyle within this theme for the given log message and log level.Returns the LogConsoleStyle of the given name, if it exists.getStyleName(LogConsoleStyle style) Returns the name of the given style, if it exists within this theme.Returns a list of all style names contained in this theme.voidloadFromFormField(FormField field) Populates this Property's value(s) from the given form field, assuming the field is of the correct type.voidloadFromProps(Properties props) Loads the value(s) for this property from the given Properties instance, overwriting any current value.voidremoveChangeListener(ChangeListener listener) Unregister from receiving notifications when style properties are changed, added, or removed from this theme.voidremoveStyle(String name) Removes the style with the given name, if it exists.voidsaveToProps(Properties props) Saves the current value(s) of this property to the given Properties instance.voidsetDefaultBgColor(Color color) Sets the default background color - this is the background that will be used unless specifically overridden by some style matcher.voidsetFontPointSize(int size) As a shortcut, it's possible to set the font point size of all style matchers with one call, instead of one by one.voidsetStyle(String name, LogConsoleStyle style) Sets or replaces the style with the given name.voidTriggered when one of our style objects is changed.Methods inherited from class ca.corbett.extras.properties.AbstractProperty
addAllExtraAttributes, addFormFieldChangeListener, clearExtraAttribute, clearExtraAttributes, equals, fireFormFieldChangedEvent, generateFormField, generateFormField, getCategoryName, getExtraAttribute, getFullyQualifiedName, getHelpText, getPropertyLabel, getPropertyName, getSubCategoryName, hashCode, isEnabled, isExposed, isInitiallyEditable, isInitiallyVisible, removeAllListeners, removeFormFieldChangeListener, setAllExtraAttributes, setEnabled, setExposed, setExtraAttribute, setHelpText, setInitiallyEditable, setInitiallyVisible, setPropertyLabel
-
Field Details
-
DEFAULT_STYLE_NAME
- See Also:
-
-
Constructor Details
-
LogConsoleTheme
Creates a plain theme with black text on a white background, and no styled matchers for any particular log messages. This is equivalent to calling LogConsoleTheme.createPlainTheme(), but you may also want to look at the static factory methods in this class to create something with some preset styling options already set.
-
-
Method Details
-
clear
public void clear()Clears all log styles except for the Default style, and resets the default background color to white. -
createPlainTheme
Creates and returns a plain theme with black text and no matchers. This is equivalent to just doing a new LogConsoleTheme() but is provided for consistency with other static creator methods.- Returns:
- A new, default, rather boringly unstyled LogConsoleTheme.
-
createDefaultStyledTheme
Creates and returns a LogConsoleTheme with some basic log matchers. Most log messages will be written in plain black text, but messages of level WARNING will be written in bold orange text, and messages of level SEVERE will be written in bold red text.- Returns:
- A LogConsoleTheme with a default styling (black on white).
-
createMatrixStyledTheme
Creates and returns a "matrix"-style LogConsoleTheme, with a black background and green text. Also includes highlighting of errors and warnings.- Returns:
- A LogConsoleTheme with a "matrix" styling (green on black).
-
createPaperStyledTheme
Creates and returns a "paper" style LogConsoleTheme, with mostly black text on a grey background. Also includes highlighting of errors and warnings.- Returns:
- A LogConsoleTheme with a "paper" styling (black on grey).
-
addChangeListener
Register to receive notifications when any style properties are changed, added, or removed from this theme.- Parameters:
listener- The listener to add.
-
removeChangeListener
Unregister from receiving notifications when style properties are changed, added, or removed from this theme.- Parameters:
listener- The listener to add.
-
setDefaultBgColor
Sets the default background color - this is the background that will be used unless specifically overridden by some style matcher.- Parameters:
color- The new default background color. Ignored if null.
-
getDefaultBgColor
Returns the default background color - this is the background that will be used unless specifically overridden by some style matcher.- Returns:
- The default background color.
-
setFontPointSize
public void setFontPointSize(int size) As a shortcut, it's possible to set the font point size of all style matchers with one call, instead of one by one.- Parameters:
size- The new font point size to apply to all styles in this theme.
-
setStyle
Sets or replaces the style with the given name. If the given style is null, then this method will defer to removeStyle(name).- Parameters:
style- The style object to set. If null, will invoke removeStyle(name).
-
getStyle
Returns the LogConsoleStyle of the given name, if it exists.- Parameters:
name- The name of the style in question.- Returns:
- A LogConsolStyle object, or null if name not found.
-
getStyleNames
Returns a list of all style names contained in this theme. This list is guaranteed to always contain at least one name ("Default"). The "Default" entry is always returned in position 0 in the list. The remainder of the list is sorted alphabetically.- Returns:
- A list of all style names contained in this theme.
-
getStyleName
Returns the name of the given style, if it exists within this theme.- Parameters:
style- A LogConsoleStyle instance.- Returns:
- The name of the given LogConsoleStyle if it is present, else null.
-
removeStyle
Removes the style with the given name, if it exists. If the given name is null, or if you try to remove the "Default" style, an IllegalArgumentException will be thrown.- Parameters:
name- The name of the style to remove.- Throws:
IllegalArgumentException- If name is null or "Default"
-
getMatchingStyle
Returns the appropriate LogConsoleStyle within this theme for the given log message and log level. If no style within this theme is a match, then the default theme is returned.- Parameters:
logMsg- The log message in question.logLevel- The Level at which this message was logged.- Returns:
- A LogConsoleStyle from this theme that matches the given parameters.
-
stateChanged
Triggered when one of our style objects is changed. We simply pass this on to whoever is listening to this theme.- Specified by:
stateChangedin interfaceChangeListener- Parameters:
e- A ChangeEvent
-
saveToProps
Description copied from class:AbstractPropertySaves the current value(s) of this property to the given Properties instance.- Specified by:
saveToPropsin classAbstractProperty- Parameters:
props- Any Properties instance which will receive the value(s) of this property.
-
loadFromProps
Description copied from class:AbstractPropertyLoads the value(s) for this property from the given Properties instance, overwriting any current value. The current value of this property will be used as a default value in the event that this property does not exist in the given Properties instance.- Specified by:
loadFromPropsin classAbstractProperty- Parameters:
props- Any Properties instance which contains value(s) for this property.
-
generateFormFieldImpl
Description copied from class:AbstractPropertyDescendant classes must implement this method to generate a FormField associated with this property. The generateFormField() method in this class will call this abstract method to create the FormField, which will then be augmented with our fully qualified name, read-only state, help text, and extra attributes.- Specified by:
generateFormFieldImplin classAbstractProperty- Returns:
- A FormField associated with this property.
-
loadFromFormField
Description copied from class:AbstractPropertyPopulates this Property's value(s) from the given form field, assuming the field is of the correct type.- Specified by:
loadFromFormFieldin classAbstractProperty- Parameters:
field- The FormField containing a value for this property.
-