Logger
A central utility class for processing log statements at various log levels.
Log messages are not guaranteed to be processed immediately upon calling one of the logging methods, however they will be processed in the order that they are received.
It is preferable to avoid unnecessary code execution when writing log statements. For example, try not to pre-calculate complicated log messages before passing them into the log methods, in case the LogLevel is not sufficiently set for it to actually be logged. As such there are several methods available to support efficient logging, some of which will delay evaluation of the message:
With a simple String input - this is preferred for simple cases where a pre-determined String input is used and no complicated processing would occur outside of the String allocation
logger.trace("category", "Simple message")With String.format and varargs - this is typically useful for cases where a small number of arguments are to be formatted into the log message, but the calculation of any of the arguments' toString method is non-trivial. e.g. formatting a complex object as a JSON string. Therefore, this approach is also not preferred if the args will need to be manipulated in some way before passing to the log method, or where large lists of args make the statement difficult to read.
// good
logger.trace("category", "Color: %s", "red") // Color: red
logger.trace("category", "$%.2f", money) // $10.55
// less good
logger.trace("category", "JSON: %s", jsonObject.toJsonString())
logger.trace("category", "%s, %s, %s, %s, %s, %s, %s,",
arg1, arg2, arg3, arg4, arg5, arg6, arg7,
)With a message supplier - this is typically useful for cases where it would be more readable to build the message in a block, but the calculation of the message would benefit from being evaluated later.
logger.trace("category") {
jsonObject.toJsonString()
}Note. For Kotlin call sites, there are also inline extension methods available that will inline a check that the logger is configured with the required level of logging, before calling the respective method on the logger. This allows the caller to sidestep any complicated message processing whilst also avoiding extra anonymous classes. For instance, the following are equivalent:
if (logger.shouldLog(LogLevel.TRACE) {
logger.trace("category", jsonObject.toJsonString())
}
// or inlined:
logger.logIfTraceEnabled("category") {
jsonObject.toJsonString()
}Most Kotlin call-sites should prefer these extension methods for readability and efficiency, but for cases at the very start of SDK initialization, where no LogLevel has been set yet, the implicit shouldLog check will always return true causing the log message to be eagerly evaluated.
Java call-sites, where inlining is not available, can choose whichever method is more appropriate/readable.
See also
Properties
Convenience method to check that the log level is set to debug or higher
Convenience method to check that the log level is set to error or higher
Convenience method to check that the log level is set to info or higher
Convenience method to check that the log level is set to trace or higher
Convenience method to check that the log level is set to warn or higher
Functions
Logs a LogLevel.DEBUG level message with the given category by evaluating the log message from the given message
Logs a LogLevel.TRACE level message with the given category.
Logs a LogLevel.DEBUG level message with the given category by formatting the message using String.format, inserting the args into the placeholders in the order given in the message
Logs a LogLevel.INFO level message with the given category by evaluating the log message from the given message
Logs a LogLevel.TRACE level message with the given category.
Logs a LogLevel.ERROR level message with the given category by formatting the message using String.format, inserting the args into the placeholders in the order given in the message
Logs a LogLevel.INFO level message with the given category by evaluating the log message from the given message
Logs a LogLevel.TRACE level message with the given category.
Logs a LogLevel.INFO level message with the given category by formatting the message using String.format, inserting the args into the placeholders in the order given in the message
Logs a message at the given level and category by evaluating the log message from the given message
Logs a message at the given level and category.
Logs a message at the given level and category by formatting the message using String.format, inserting the args into the placeholders in the order given in the message
Inlines a check for whether the Logger is currently logging at LogLevel.DEBUG, before evaluating the message, whilst avoiding an unnecessary anonymous class implementation.
Inlines a check for whether the Logger is currently logging at LogLevel.ERROR, before evaluating the message, whilst avoiding an unnecessary anonymous class implementation.
Inlines a check for whether the Logger is currently logging at LogLevel.INFO, before evaluating the message, whilst avoiding an unnecessary anonymous class implementation.
Inlines a check for whether the Logger is currently logging at LogLevel.TRACE, before evaluating the message, whilst avoiding an unnecessary anonymous class implementation.
Inlines a check for whether the Logger is currently logging at LogLevel.WARN, before evaluating the message, whilst avoiding an unnecessary anonymous class implementation.
Logs a LogLevel.TRACE level message with the given category by evaluating the log message from the given message
Logs a LogLevel.TRACE level message with the given category.
Logs a LogLevel.TRACE level message with the given category by formatting the message using String.format, inserting the args into the placeholders in the order given in the message
Logs a LogLevel.WARN level message with the given category by evaluating the log message from the given message
Logs a LogLevel.TRACE level message with the given category.
Logs a LogLevel.WARN level message with the given category by formatting the message using String.format, inserting the args into the placeholders in the order given in the message