Package org.djutils.logger
Class CategoryLogger
java.lang.Object
org.djutils.logger.CategoryLogger
public class CategoryLogger extends Object
The CategoryLogger can log for specific Categories. The way to call the logger for messages that always need to be logged,
such as an error with an exception is:
CategoryLogger.always().error(exception, "Parameter {} did not initialize correctly", param1.toString());It is also possible to indicate the category / categories for the message, which will only be logged if at least one of the indicated categories is turned on with addLogCategory() or setLogCategories(), or if one of the added or set LogCategories is LogCategory.ALL:
CategoryLogger.filter(Cat.BASE).debug("Parameter {} initialized correctly", param1.toString());
Copyright (c) 2018-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.
- Author:
- Alexander Verbraeck
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CategoryLogger.ConditionalLogger
ConditionalLogger class that takes care of filtering based on a condition.static class
CategoryLogger.DelegateLogger
DelegateLogger class that takes care of actually logging the message and/or exception. -
Field Summary
Fields Modifier and Type Field Description protected static Set<LogCategory>
categories
The categories to log.protected static CategoryLogger.ConditionalLogger
conditionalLogger
The conditional logger works on the basis of a true condition.protected static CategoryLogger.ConditionalLogger
conditionalNoLogger
The conditional logger works on the basis of a condition, failing the condition.static String
DEFAULT_MESSAGE_FORMAT
The default message format.protected static CategoryLogger.DelegateLogger
delegateLogger
The delegate logger instance that does the actual logging work, after a positive filter outcome.protected static CategoryLogger.DelegateLogger
noLogger
The delegate logger that returns immediately after a negative filter outcome. -
Constructor Summary
Constructors Modifier Constructor Description protected
CategoryLogger()
-
Method Summary
Modifier and Type Method Description static void
addLogCategory(LogCategory logCategory)
Add a category to be logged to the Writers.static CategoryLogger.DelegateLogger
always()
The "pass" filter that will result in always trying to log.protected static void
create()
Create a new logger for the system console.static CategoryLogger.DelegateLogger
filter(Set<LogCategory> logCategories)
Check whether the provided categories contain one or more categories that need to be logged.static CategoryLogger.DelegateLogger
filter(LogCategory logCategory)
Check whether the provided category needs to be logged.static CategoryLogger.DelegateLogger
filter(LogCategory... logCategories)
Check whether the provided categories contain one or more categories that need to be logged.static void
removeLogCategory(LogCategory logCategory)
Remove a category to be logged to the Writers.static void
setAllLogLevel(Level newLevel)
Set a new logging level for all registered writers.static void
setAllLogMessageFormat(String newMessageFormat)
Set a new logging format for the message lines of all writers.static void
setLogCategories(LogCategory... newLogCategories)
Set the categories to be logged to the Writers.static void
setLogLevel(Writer writer, Level newLevel)
Set a new logging level for one of the registered writers.static void
setLogMessageFormat(Writer writer, String newMessageFormat)
Set a new logging format for the message lines of a writer.static CategoryLogger.ConditionalLogger
when(boolean condition)
The conditional filter that will result in the usage of a ConditionalLogger.static CategoryLogger.ConditionalLogger
when(BooleanSupplier supplier)
The conditional filter that will result in the usage of a ConditionalLogger.
-
Field Details
-
DEFAULT_MESSAGE_FORMAT
The default message format.- See Also:
- Constant Field Values
-
categories
The categories to log. -
conditionalLogger
The conditional logger works on the basis of a true condition. -
conditionalNoLogger
The conditional logger works on the basis of a condition, failing the condition. -
delegateLogger
The delegate logger instance that does the actual logging work, after a positive filter outcome. -
noLogger
The delegate logger that returns immediately after a negative filter outcome.
-
-
Constructor Details
-
CategoryLogger
protected CategoryLogger()
-
-
Method Details
-
create
protected static void create()Create a new logger for the system console. Note that this REPLACES current writers. Note that the initial LogCategory is LogCategory.ALL, so all categories will be logged. This category has to be explicitly removed (or new categories have to be set) to log a limited set of categories. -
setAllLogMessageFormat
Set a new logging format for the message lines of all writers. The default message format is:
{class_name}.{method}:{line} {message|indent=4}
A few popular placeholders that can be used:
- {class} Fully-qualified class name where the logging request is issued
- {class_name} Class name (without package) where the logging request is issued
- {date} Date and time of the logging request, e.g. {date:yyyy-MM-dd HH:mm:ss} [SimpleDateFormat]
- {level} Logging level of the created log entry
- {line} Line number from where the logging request is issued
- {message} Associated message of the created log entry
- {method} Method name from where the logging request is issued
- {package} Package where the logging request is issued- Parameters:
newMessageFormat
- String; the new formatting pattern to use for all registered writers- See Also:
- https://tinylog.org/configuration
-
setAllLogLevel
Set a new logging level for all registered writers.- Parameters:
newLevel
- Level; the new log level for all registered writers
-
setLogMessageFormat
Set a new logging format for the message lines of a writer. The default message format is:
{class_name}.{method}:{line} {message|indent=4}
A few popular placeholders that can be used:
- {class} Fully-qualified class name where the logging request is issued
- {class_name} Class name (without package) where the logging request is issued
- {date} Date and time of the logging request, e.g. {date:yyyy-MM-dd HH:mm:ss} [SimpleDateFormat]
- {level} Logging level of the created log entry
- {line} Line number from where the logging request is issued
- {message} Associated message of the created log entry
- {method} Method name from where the logging request is issued
- {package} Package where the logging request is issued- Parameters:
writer
- Writer; the writer to change the message format fornewMessageFormat
- String; the new formatting pattern to use for all registered writers- See Also:
- https://tinylog.org/configuration
-
setLogLevel
Set a new logging level for one of the registered writers.- Parameters:
writer
- Writer; the writer to change the log level fornewLevel
- Level; the new log level for the writer
-
addLogCategory
Add a category to be logged to the Writers.- Parameters:
logCategory
- LogCategory; the LogCategory to add
-
removeLogCategory
Remove a category to be logged to the Writers.- Parameters:
logCategory
- LogCategory; the LogCategory to remove
-
setLogCategories
Set the categories to be logged to the Writers.- Parameters:
newLogCategories
- LogCategory...; the LogCategories to set, replacing the previous ones
-
always
The "pass" filter that will result in always trying to log.- Returns:
- the logger that tries to execute logging (delegateLogger)
-
filter
Check whether the provided category needs to be logged. Note that when LogCategory.ALL is contained in the categories, filter will return true.- Parameters:
logCategory
- LogCategory; the category to check for.- Returns:
- the logger that either tries to log (delegateLogger), or returns without logging (noLogger)
-
filter
Check whether the provided categories contain one or more categories that need to be logged. Note that when LogCategory.ALL is contained in the categories, filter will return true.- Parameters:
logCategories
- LogCategory...; elements or array with the categories to check for- Returns:
- the logger that either tries to log (delegateLogger), or returns without logging (noLogger)
-
filter
Check whether the provided categories contain one or more categories that need to be logged. Note that when LogCategory.ALL is contained in the categories, filter will return true.- Parameters:
logCategories
- Set<LogCategory>; the categories to check for- Returns:
- the logger that either tries to log (delegateLogger), or returns without logging (noLogger)
-
when
The conditional filter that will result in the usage of a ConditionalLogger.- Parameters:
condition
- boolean; the condition that should be evaluated- Returns:
- the logger that further processes logging (ConditionalLogger)
-
when
The conditional filter that will result in the usage of a ConditionalLogger.- Parameters:
supplier
- BooleanSupplier; the function evaluating the condition- Returns:
- the logger that further processes logging (ConditionalLogger)
-