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.
Created
July 11, 2014 09:41
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" } | |
} | |
]} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
yaml format is better