0

I am using spring boot in a project and currently exploring the logging behaviour. For that I'm using the zipkin service.

I have exported my logs to a json file using proper logback.xml:

{"@timestamp":"2018-07-12T17:53:44.613+05:30","@version":"1","message":"in meth3","logger_name":"com.example.demo.Controller.Controller","thread_name":"http-nio-8089-exec-3","level":"INFO","level_value":20000,"traceId":"62bcfafad3a37a09","spanId":"62bcfafad3a37a09","spanExportable":"true","X-Span-Export":"true","X-B3-SpanId":"62bcfafad3a37a09","X-B3-TraceId":"62bcfafad3a37a09","caller_class_name":"com.example.demo.Controller.Controller","caller_method_name":"meth3","caller_file_name":"Controller.java","caller_line_number":43,"appname":"pom.artifactId_IS_UNDEFINED","version":"pom.version_IS_UNDEFINED"}

Is there a way so that I could insert a jsonObject in my message part of the log. Something like:

logger.info(<some_json_object>)

I have tried searching a way extensively but to no avail. Is it even possible?

Swapnil Pandey
  • 577
  • 3
  • 8
  • 25
  • Zipkin is a trace collector. It has nothing to do with logging except the client instrumentation libraries optionally print trace information in log messages. – zeagord Jul 16 '18 at 04:01

1 Answers1

0

The slf4j API only takes String as the input to the info, debug, warn, error messages.

What you could do is create your own JsonLogger wrapper, which takes a normal Logger (maybe wraps around it), which you could include at the top of your classes like:

private static final JsonLogger logger = new JsonLogger(LoggerFactory.getLogger(MyClass.class));

You can then use Jackson, GSON or your favourite object to JSON mapper inside your JsonLogger so that you could do what you want. It can then offer the info, debug, warn, error methods like a normal logger.

You can also create your own JsonLoggerFactory which encapsulates this for you so that the line to include in each class is more concise.

jbx
  • 21,365
  • 18
  • 90
  • 144