Skip to content

Instantly share code, notes, and snippets.

@hmarggraff
Created July 11, 2014 09:41
Show Gist options
  • Save hmarggraff/9a8024f1d5292d539895 to your computer and use it in GitHub Desktop.
Save hmarggraff/9a8024f1d5292d539895 to your computer and use it in GitHub Desktop.
Configure log4j2 with yaml, json and xml for different message layout of different log levels

This shows roughly similar configurations to distinguish message formats for different error levels in log4j version 2. I believe the yaml configuration is the easiert to read and maintain.

{
"status": "info",
"appenders": {
"Console": [{
"name": "info",
"PatternLayout": { "pattern": "inf %5p - %m%n" } },
{"name": "err",
"PatternLayout": { "pattern": "err %5p %logger %m %l%n" } }
]},
"loggers": {
"root": { "level": "off" },
"logger": [
{
"name": "a",
"level": "all",
"appender-ref": [ { "ref": "info" },
{ "ref": "err", "level":"error"}
]
},
{
"name": "b",
"level": "off",
"appender-ref": { "ref": "err" }
}
]}
}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" strict="true" name="XMLConfigTest"
packages="org.apache.logging.log4j.test">
<Appenders>
<Appender type="Console" name="short">
<Filter type="ThresholdFilter" level="error"/>
<Layout type="PatternLayout" pattern="short %p %m%n"/>
</Appender>
<Appender type="Console" name="long">
<Layout type="PatternLayout" pattern="long %p %m%n"/>
<Filter type="ThresholdFilter" level="error" onMatch="deny" onMismatch="accept"/>
</Appender>
</Appenders>
<Loggers>
<Logger name="a" level="debug" additivity="false">
<AppenderRef ref="short" level="warn"/>
<AppenderRef ref="long"/>
</Logger>
<Logger name="b" level="off" additivity="false">
<AppenderRef ref="long"/>
</Logger>
<Root level="trace">
<AppenderRef ref="long"/>
</Root>
</Loggers>
</Configuration>
status: info # log level of log4j itself when configuring
appenders: # it is a map of appender types
Console: # appender type - a list of appender configurations
- name: info # name to use in logger references
ThresholdFilter: # filters out error messages
level: error
onMatch: deny # hide error level
onMismatch: accept # show others
PatternLayout: { pattern: "inf %5p %m%n" } # short pattern
- name: err # the next list entry is indivcated by the dash
ThresholdFilter:
level: error # show for error
PatternLayout: { pattern: "err %p %l %msg%n" } # pattern with source code location (%l}
loggers:
root: {level: error} # like the default, but to keep log4j from complaining # level off does not work in R2.02
logger: # logger is a list of loggers configurations
- name: a
level: trace # general level for this logger, further restricted in the appenders
appender-ref: # a list of appenders. All messages
- ref: err
- ref: info
- name: b # another logger
level: info
appender-ref:
- ref: err
- ref: info
@steventzeng-base
Copy link

yaml format is better

@dvaneynde
Copy link

With xml I know what I can put in the xml because there is an xsd. I don't have to search endlessly for examples, documentation...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment