0

I am currently attempting to change our logs format in Quarkus from String to JSON with some additional fields that are important for our monitoring and data analysis in elastic/kibana.

So far I have added this dependency as specified in the official documentation

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-logging-json</artifactId>
</dependency>

https://quarkus.io/guides/logging

That changed the log format from a normal String to a full JSON format.

For example:

{"timestamp":"2022-09-05T13:30:09.314+01:00","sequence":24441,"loggerClassName":"org.jboss.logging.Logger","loggerName":"org.com.Controller","level":"INFO","message":"Test","threadName":"executor-thread-0","threadId":354,"mdc":{},"ndc":"","hostName":"hostname","processName":"test.jar","processId":9552}

My question is, how do I add additional fields to this log output, for instance I need to add and additional json field called 'pattern' with a value extracted from the code each time. The final json output will look like this:

{"timestamp":"2022-09-05T13:30:09.314+01:00","sequence":24441,"loggerClassName":"org.jboss.logging.Logger","loggerName":"org.com.Controller","level":"INFO","message":"Test","threadName":"executor-thread-0","threadId":354,"mdc":{},"ndc":"","hostName":"hostname","processName":"test.jar","processId":9552, "pattern" :"test-pattern"}

I tried the following as specified in the documentation:

quarkus.log.file.json.additional-field.pattern.value=test-value
quarkus.log.file.json.additional-field.pattern.type=string

But this didn't show anything, and Im not sure how to use it programmatically,

Stephan Strate
  • 1,401
  • 1
  • 12
  • 15
morpheus
  • 1
  • 1

2 Answers2

0

Example configuration

quarkus.log.console.json.additional-field."EXTRA".value=test-value
quarkus.log.console.json.additional-field."EXTRA".type=string

quarkus.log.file.json.additional-field."EXTRA".pattern.value=test-value
quarkus.log.file.json.additional-field."EXTRA".pattern.type=string

Should have double quotes. and example output

{"timestamp":"2022-09-18T14:37:37.687+01:00","sequence":1548,"loggerClassName":"org.jboss.logging.Logger","loggerName":"org.acme.GreetingResource","level":"INFO","message":"Hello","threadName":"executor-thread-0","threadId":101,"mdc":{},"ndc":"","hostName":"mintozzy-mach-wx9","processName":"code-with-quarkus-dev.jar","processId":133458,"EXTRA":"test-value"}

for full working example check

ozkanpakdil
  • 3,199
  • 31
  • 48
  • Thanks, this is useful, is there any way of making this dynamic, ie making this extra field a variable, such as IP address, etc? – morpheus Sep 22 '22 at 09:43
  • not from properties but you can use https://github.com/quarkiverse/quarkus-logging-json#custom-log-handler and add any dynamic value you like. – ozkanpakdil Sep 22 '22 at 13:39
  • Can you please share a working example on how would this work if I had a changing custom value? Here generator.writeStringField("myCustomField", "and my custom value"); How do I plug "and my custom value" dynamically? – morpheus Sep 23 '22 at 08:31
  • can you ask another question for that. I think I can implement it but it is out of scope of this question :) – ozkanpakdil Sep 23 '22 at 10:33
0

You might be able to solve your problem using the quarkiverse logging json extension using their JsonProvider

https://github.com/quarkiverse/quarkus-logging-json

The quarkiverse logging json extension is much more flexible/extensible than the standard quarkus logging json packages, since you can add fields to the json log programatically, instead of in hard coded configuration