Skip to content

Basic Usage

Echopraxia is simple and easy to use, and looks very similar to SLF4J.

Add the import:

import com.tersesystems.echopraxia.*;

Define a logger (usually in a controller or singleton -- getClass() is particularly useful for abstract controllers):

final Logger<PresentationFieldBuilder> basicLogger = LoggerFactory.getLogger(getClass());

Logging simple messages and exceptions are done as in SLF4J:

try {
  ...
  basicLogger.info("Simple message");
} catch (Exception e) {
  basicLogger.error("Error message", e);  
}

However, when you log arguments, you pass a function which provides you with a field builder and returns a FieldBuilderResult -- a Field is a FieldBuilderResult, so you can do:

basicLogger.info("Message name {}", fb -> fb.string("name", "value"));

If you are returning multiple fields, then using fb.list will return a FieldBuilderResult:

basicLogger.info("Message name {} age {}", fb -> fb.list(
  fb.string("name", "value"),
  fb.number("age", 13)
));

And fb.list can take many inputs as needed, for example a stream:

String[]
basicLogger.info("Message name {}", fb -> {
  Stream<Field> fieldStream = ...;
  return fb.list(arrayOfFields);
});

You can log multiple arguments and include the exception if you want the stack trace:

basicLogger.info("Message name {}", fb -> fb.list(
  fb.string("name", "value"),
  fb.exception(e)
));

In older versions, fb.only() was required to convert a Field -- this is no longer required, but a FieldBuilderWithOnly interface is available to maintain those methods.

Note that unlike SLF4J, you don't have to worry about including the exception as an argument "swallowing" the stacktrace. If an exception is present, it's always applied to the underlying logger.