Dropwizard is a is a Java framework for developing ops-friendly, high-performance, RESTful web services. It is internally integrate Jaskson, and Logback and slf4j, so that it is easy to output JSON logs for the application that created based on Dropwizard.
In the official document, it provides a sample about how to configure the config.yml to make the log format to JSON.
Below is an example to configure the JSON log
server:
applicationConnectors:
- type: http
port: 9080
adminConnectors:
- type: http
port: 9081
logging:
level: INFO
loggers:
my.example.nccsdropwizard: DEBUG
appenders:
- type: console
layout:
type: json
timestampFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
prettyPrint: false
appendLineSeparator: true
includes: [timestamp, threadName, level, loggerName, message, mdc, exception]
customFieldNames:
timestamp: "time"
message: "msg"
additionalFields:
service-name: "user-service"
includesMdcKeys: [userId]
flattenMdc: true
exception:
rootFirst: true
depth: full
evaluators: [org.apache]
However, there may be the case that that you want to customize the output JSON log keys like introduce the new key for each log, here is the way how to write your own appender?
- Extends AbstractAppenderFactory class, and override the build() function, inside this function, you could try to use your own JsonLayout class, or your own JaksonJSONFromatter if you need.
-
package my.example.Log; impo