log4j logs customization
Configuration file
Configuration
Log4j configuration file can be in any of these formats:
- JSON
- YAML
- properties (text file)
- XML
Configuration discovery
- Log4j will inspect the
log4j.configurationFilesystem property and, if set, will attempt to load the configuration. - If no system property is set log4j will look for
log4j2-test.propertiesin the classpath. - If no such file is found the log4j will look for
log4j2-test.yamlorlog4j2-test.ymlin the classpath. - If no such file is found the log4j will look for
log4j2-test.jsonorlog4j2-test.jsnin the classpath. - If no such file is found the logj4 will look for
log4j2-test.xmlin the classpath. - If a test file cannot be located the log4j will look for
log4j2.propertieson the classpath. - If a properties file cannot be located the log4j will look for
log4j2.yamlorlog4j2.ymlon the classpath. - If a YAML file cannot be located the log4j will look for
log4j2.jsonorlog4j2.jsnon the classpath. - If a JSON file cannot be located the log4j will try to locate
log4j2.xmlon the classpath. - If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
XML Configuration
XML Example
The below configuration configures two appenders (log output). The first logs to standard system output (console) and the other logs to file. In this example, the location of the file can be set statically in configuration (appender file) or dynamically via maven filtering feature (<Property name="APPENDER">). The logs in file will be packed by day. The log line format Conversion Pattern is set as a variable.
<?xml version="1.0" encoding="UTF-8"?>
<!-- 'status' sets log level for parsing configuration file itself -->
<Configuration status="INFO">
<Properties>
<!-- Sets variable PID, if it's not present in log4j context will take value as below: -->
<Property name="PID">????</Property>
<!-- Sets variable 'LOG_PATTERN', defining how log line will look like -->
<Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{%X{usr}}{green} %clr{---}{faint}%clr{[%15.15t]}{faint} %clr{%-40.40c{1.}:%l}{cyan} %clr{:}{faint} %m%n%wEx</Property>
<!-- LOG_DIR may be set by maven filtering feature: -->
<Property name="LOG_DIR">@logging.path@</Property>
<!-- APPENDER may be set by maven filtering feature, to set 'console' or 'file' appender: -->
<Property name="APPENDER">@logging.default.appender@</Property>
</Properties>
<Appenders>
<!-- Sets console output: -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<!-- Sets output to file, and names it 'file' -->
<RollingRandomAccessFile append="true" fileName="${LOG_DIR}/log4j2.log"
filePattern = "${LOG_DIR}/log4j2.%d{yyyy-MM-dd}.nr%i.log.gz" name="file">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<!-- Sets debug for Class 'com.example.package.Clazz', and attaches to file 'file' -->
<Logger name="com.example.package.Clazz" level="debug">
<AppenderRef ref="file"/>
</Logger>
<!-- Sets 'info' for package 'org.springframework' -->
<Logger name="org.springframework" level="info" />
<Root level="WARN">
<AppenderRef ref="${APPENDER}"/>
</Root>
</Loggers>
</Configuration>