The default pattern for Dropwizard
1 answer
The protocol uses implementations of the Layout interface doLayout()
to translate log events into strings that can be output, as specified in the documentation . Dropwizard provides an extension for the PatternLayout
class (which in turn is an extension of the abstract PatternLayoutBase
class ):
PatternLayout
takes a registration event and returnsString
. However, thisString
one can be customized by customizing the PatternLayout transformation pattern.
public class DropwizardLayout extends PatternLayout {
public DropwizardLayout(LoggerContext context, TimeZone timeZone) {
super();
setOutputPatternAsHeader(false);
getDefaultConverterMap().put("ex", PrefixedThrowableProxyConverter.class.getName());
getDefaultConverterMap().put("xEx", PrefixedExtendedThrowableProxyConverter.class.getName());
getDefaultConverterMap().put("rEx", PrefixedRootCauseFirstThrowableProxyConverter.class.getName());
setPattern("%-5p [%d{ISO8601," + timeZone.getID() + "}] %c: %m%n%rEx");
setContext(context);
}
}
What it is:
- Ensures
outputPatternAsHeader
disabled is a flag that outputs the String pattern you want at the top of any logs. One way to confirm the pattern in use could be to include this flag in your appender as stated in the protocol documentation . - Overrides the conversion word count associated with print exceptions to be used by the ThrowableProxyConverter implementation , which prefixes the stack trace with exclamation marks.
- Specify the template
"%-5p [%d{ISO8601," + timeZone.getID() + "}] %c: %m%n%rEx"
using the passed time zone. If you don't add time zone information explicitly, the log will use the JVM or GMT time zone. If that's okay, you can just use%d
, since theISO8601
default format is. - Sets the context.
+2
source to share