• 0.24.1

Decors

Log an event like this:

import com.jcabi.log.Logger;
class Foo {
  void bar(int value) {
    final long start = System.nanoTime();
    // some operations
    Logger.debug(
      this,
      "bar(%d): done in %[nano]d",
      value,
      System.nanoTime() - start
    );
  }
}

You will see something like this in log:

[DEBUG] Foo: #bar(1234): done in 3.45ms

Nano-seconds will be formatted as time through a built-in "decor".

There are a few other pre-defined decors:

  • dom -- converts org.w3c.dom.Node to text.
  • exception -- exception message and full stacktrace;
  • list -- array or list of elements into text;
  • ms -- milliseconds into their text presentation;
  • nano -- nanoseconds into their text presentation;
  • text -- makes text readable;
  • type -- type name of the object.

In order to use your own decor just implement java.util.Formattable:

import com.jcabi.log.Decor;
import java.util.Formattable;
import java.util.Formatter;
public final class DataDecor implements Formattable {
  private final transient Data data;
  public DataDecor(final Data dat) {
    this.data = dat;
  }
  @Override
  public void formatTo(final Formatter formatter, final int flags,
    final int width, final int precision) {
    formatter.format("%s", this.data.getSomeValueOutOfIt());
  }
}

Then, provide its class name in log formatting string, for example:

import com.jcabi.log.Logger;
public class Main {
  public static void main(String[] args) {
    Logger.debug(
      this,
      "bar(%d): show some data: %[com.example.DataDecor]s",
      value,
      data
    );
  }
}