java - slf4j: how to log formatted message, object array, exception

ID : 10235

viewed : 35

Tags : javaexceptionloggingslf4jjava

Top 5 Answer for java - slf4j: how to log formatted message, object array, exception

vote vote


As of SLF4J 1.6.0, in the presence of multiple parameters and if the last argument in a logging statement is an exception, then SLF4J will presume that the user wants the last argument to be treated as an exception and not a simple parameter. See also the relevant FAQ entry.

So, writing (in SLF4J version 1.7.x and later)

 logger.error("one two three: {} {} {}", "a", "b",                "c", new Exception("something went wrong")); 

or writing (in SLF4J version 1.6.x)

 logger.error("one two three: {} {} {}", new Object[] {"a", "b",                "c", new Exception("something went wrong")}); 

will yield

one two three: a b c java.lang.Exception: something went wrong     at Example.main(     at java.lang.reflect.Method.invoke(     at ... 

The exact output will depend on the underlying framework (e.g. logback, log4j, etc) as well on how the underlying framework is configured. However, if the last parameter is an exception it will be interpreted as such regardless of the underlying framework.

vote vote


In addition to @Ceki 's answer, If you are using logback and setup a config file in your project (usually logback.xml), you can define the log to plot the stack trace as well using

<encoder>     <pattern>%date |%-5level| [%thread] [%file:%line] - %msg%n%ex{full}</pattern>  </encoder> 

the %ex in pattern is what makes the difference

vote vote


The accepted answer is great. I'm just adding here my case that is now working thanks for the answer. This may help someone else.

I'm using SLF4J and logback with a JSON encoder. Furthermore, I use marker and arguments to enrich my output.

    logger.error(getMarker("errorEvent"),                  "An error occurred",                  entries(mapOf("someKey" to "someValue")),                  new Exception()) 

The output:

   {    "level": "ERROR",    "event": "errorEvent",    "eventData": {       "someKey": "someValue"    },    "stacktrace": "...omitted...",    "message": "An error occurred" } 

Of course there is a lot of configuration of logstash behind the scenes, but I just wanted to show that the arguments passed as entries are shown in the configured eventData tag.

vote vote


  • Protobuf serialized objects are about 30% smaller than Thrift.
  • Most actions you may want to do with protobuf objects (create, serialize, deserialize) are much slower than thrift unless you turn on option optimize_for = SPEED.
  • Thrift has richer data structures (Map, Set)
  • Protobuf API looks cleaner, though the generated classes are all packed as inner classes which is not so nice.
  • Thrift enums are not real Java Enums, i.e. they are just ints. Protobuf has real Java enums.

For a closer look at the differences, check out the source code diffs at this open source project.

vote vote


As I've said as "Thrift vs Protocol buffers" topic :

Referring to Thrift vs Protobuf vs JSON comparison :

Additionally, there are plenty of interesting additional tools available for those solutions, which might decide. Here are examples for Protobuf: Protobuf-wireshark , protobufeditor.

Top 3 video Explaining java - slf4j: how to log formatted message, object array, exception