ILogger
The logger interface allows the user to write messages to the LOG output. A logger is obtained via the logger factory by specifying a class or a freely selectable name.
@NotNull public static ILogger getLogger(@NotNull final Class<?> aClass); @NotNull public static ILogger getLogger(@NotNull final String aName);
LOG levels
As in many other logging systems, there are LOG levels. The following LOG levels are specified:
- TRACE
- DEBUG
- INFO
- WARN
- ERROR
- OFF
Logging methods
For each level there is a set of methods to output a LOG message. Here is only the presentation of the methods for the debug output, the methods for the other LOG levels are analogous.
The simplest way of logging is to output text.
void debug(@NotNull String aMessage);
Texts can be enriched with arguments. Curly brackets are used as placeholders:
void debug(@NotNull String aMessage, @Nullable Object... aArguments);
Example:
Logger.debug("{} is a woman", name);
Properties are used to enrich the LOG entries with context information. They can be used in the LOG formatter for LOG output.
void debug(@NotNull Properties aProperties, @NotNull String aMessage); void debug(@NotNull Properties aProperties, @NotNull String aMessage, @Nullable Object... aArguments);
Sometimes a message or an argument must be computed specifically for logging. For expensive computations, lazy logging with lambda expressions is recommended. The expression code will only be evaluated if a log entry is actually output.
void debug(@NotNull Supplier<?> aSupplier); void debug(@NotNull Properties aProperties, @NotNull Supplier<?> aSupplier);
The used supplier interfaces can return texts or object arrays:
// gives "Value is 32" LOG.debug(() -> { // calculate something complex int value = 16 * 2; return "Value is " + value; }); // gives "32, 64" LOG.debug(() -> { // calculate something complex int value1 = 16 * 2; int value2 = 32 * 2; return new Object[] { value1, value2 }; });
Special methods for the ERROR level
In the ERROR level there are some additional methods to output stack traces. The other arguments are similar to those of the other LOG levels.
void error(@NotNull Throwable aThrowable); void error(@NotNull Properties aProperties, @NotNull Throwable aThrowable); void error(@NotNull Throwable aThrowable, @NotNull String aMessage); void error(@NotNull Properties aProperties, @NotNull Throwable aThrowable, @NotNull String aMessage); void error(@NotNull Throwable aThrowable, @NotNull String aMessage, @Nullable Object... aArguments); void error(@NotNull Properties aProperties, @NotNull Throwable aThrowable, @NotNull String aMessage, @Nullable Object... aArguments); void error(@NotNull Throwable aThrowable, @NotNull Supplier<?> aSupplier); void error(@NotNull Properties aProperties, @NotNull Throwable aThrowable, @NotNull Supplier<?> aSupplier);
If a stack trace is to be output at the current position, you can use this to set the stack depth from which a trace is to be output.
public void error(@NotNull final String aMessage, final int aStackDepthToAdd);
Checking whether a LOG output is made
Before complex LOG outputs are made, it is advisable to check whether the output is made at all.
boolean canLog(ELevel aLevel); boolean isDebugEnabled(); boolean isErrorEnabled(); boolean isInfoEnabled(); boolean isTraceEnabled(); boolean isWarnEnabled();
The current LOG level can be fetched as well.
ELevel getLevel();
Log output on the console
Normally, you would install a console log writer for log output on the console. However, a simple method for occasional output to the console is also available.
void logOnConsole(@NotNull String aMessage, @Nullable Object... aArguments);