――Every time I touched log4j for the first time in a while, I completely forgot how to use it, so I decided to understand the structure in my own way. ――Because it was a great opportunity, I wanted to understand the basic structure first, so I basically specify options as much as possible. ――In addition, since it is limited to basic usage, settings that are often used in actual development are sometimes cut off.
logger.fatal("fatal!");
logger.error("error!");
logger.warn("warn!");
logger.info("info!");
logger.debug("debug!");
logger.trace("trace!");
--Even if there is no configuration file, log output will be performed with the default settings (although an error message will be displayed to that effect).
--The default setting is
--Apply Console Appender to Root Logger
--Apply Pattern Layout to Console Appender
--pattern is "% d {HH: mm: ss.SSS} [% t]% -5level% logger {36} --% msg% n "
--Output log level is ERROR
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
15:51:56.904 [main] FATAL Test - fatal!
15:51:56.907 [main] ERROR Test - error!
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
</Configuration>
If you describe so far, the log will be output in the default format while no error will occur.
16:52:31.453 [main] FATAL Test - fatal!
16:52:31.453 [main] ERROR Test - error!
--You can specify the log output level of log4j2 itself by using the status attribute in the Configuration tag. --If there is a file called log4j2-test.xml, this one will be read with priority. This is convenient when you want to temporarily replace the settings. --In addition to xml format, you can also write in json format or yaml format. Also, the legacy properties format. --The priority is properties> yaml> json> xml. The rationale is unknown (why xml is the weakest!).
reference: http://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration
A combination of Console Appender
with no options and RootLogger
with just that.
The name given by the name
attribute of ʻAppender is associated by specifying it by the
ref attribute of ʻAppenderRef
of Logger
.
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT">
</Console>
</Appenders>
<Loggers>
<Root>
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
fatal!
error!
A simple log of only messages is output.
--Appender defines the log output destination (output method), output layout, etc. --Logger specifies the log output target and the Appender to be used. Multiple Appenders can be specified. Even if you do not specify Appender, no error will occur, but of course the log will not be output anywhere.
--Console Appender defaults to standard output. If you want to output to standard error output, specify target = "SYSTEM_ERR". --Actually, even if the output destination is standard output, there are many cases where target = "SYSTEM_OUT" is explicitly described.
--By specifying level for Logger, you can control the log output level of that Logger.
<Root level="info">
fatal!
error!
warn!
info!
reference: http://logging.apache.org/log4j/2.x/manual/configuration.html#Loggers http://logging.apache.org/log4j/2.x/manual/configuration.html#Appenders
To specify the output layout, specify Layout for Appender.
<Console name="STDOUT">
<PatternLayout pattern="%d %p %m%n"/>
</Console>
2017-10-24 18:12:23,469 FATAL fatal!
2017-10-24 18:12:23,472 ERROR error!
2017-10-24 18:12:23,472 WARN warn!
2017-10-24 18:12:23,472 INFO info!
Reference: http://logging.apache.org/log4j/2.x/manual/layouts.html
Define a child Logger of RootLogger as needed.
<Loggers>
<Logger name="Test">
<AppenderRef ref="STDOUT"/>
</Logger>
<Root>
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
--The name attribute is required for the child Logger, and specify the class or package to be logged. --Note that specifying a class name or package name that does not exist does not cause an error. --The name attribute cannot be specified for RootLogger.
2017-10-25 13:06:51,840 FATAL fatal!
2017-10-25 13:06:51,840 FATAL fatal!
2017-10-25 13:06:51,840 ERROR error!
2017-10-25 13:06:51,840 ERROR error!
Since Logger propagates from child to parent, logs are output twice if the log output conditions are the same. To suppress propagation, specify additivity = "false" in the child Logger.
<Logger name="Test" additivity="false">
2017-10-25 13:15:59,282 FATAL fatal!
2017-10-25 13:15:59,282 ERROR error!
Since RootLogger has no parent, additivity cannot be specified.
Reference: http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity
The filter is used when you want to control whether or not to output the log only under specific conditions. Filters can be set in the following four locations.
--Configuration --Filter for the entire context --Logger --Filter for a specific Logger --Appender --Filter for a specific Appender --AppenderRef --Filter for a specific Logger x Appender
When the log is output, it is judged that the filter is applied, and one of the following three statuses is returned.
--ACCEPT --Log output confirmed --DENY --Log non-output confirmed --NEUTRAL --Transfer judgment to the next filter
<Configuration>
<RegexFilter regex=".*err.*"/>
If you want to specify multiple filters, enclose them in Filters.
<Configuration>
<Filters>
<RegexFilter regex=".*err.*"/>
<TimeFilter start="9:00:00" end="17:00:00"/>
</Filters>
--If multiple filters are set, the judgment will be performed in the defined order. --If ACCEPT or DENY is returned, the judgment is confirmed at that point, and the judgment of the remaining filters is not performed. --If NEUTRAL is returned, the judgment moves to the next filter. --If the next filter is no longer available, it will be treated as ACCEPT.
Reference: http://logging.apache.org/log4j/2.x/manual/filters.html
Property values can be defined and referenced in the configuration file.
<Configuration>
<Properties>
<Property name="myPattern">%d %p %m%n</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="${myPattern}"/>
It can be used to extract common elements and literal values.
Reference: http://logging.apache.org/log4j/2.x/manual/configuration.html#Properties
Various values such as system environment variables and system properties can be referenced in the configuration file.
--System environment variables
-- $ {env: os}
, $ {env: computername}
, etc.
--System properties
-- $ {sys: java.home}
, $ {sys: file.encoding}
, etc.
Reference: http://logging.apache.org/log4j/2.x/manual/lookups.html
There are mechanisms such as ScriptFilter and ScriptPatternSelector that work using scripts.
The script can be written directly in the configuration file, or the script file can be referenced.
In the former case, use the <Script>
tag, and in the latter case, use the <ScriptFile>
tag to define.
You can specify the type of script with the language attribute, but the types that can be specified in the version / environment I tried this time were nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript.
<Configuration>
<Scripts>
<Script name="checkSunday" language="javascript"><![CDATA[
var result;
var sunday = 0;
var today = new Date();
var dayOfWeek = today.getDay();
if (dayOfWeek == sunday) {
result = true;
} else {
result = false;
}
result;
]]></Script>
</Scripts>
<ScriptFilter>
<ScriptRef ref="checkSunday"/>
</ScriptFilter>
reference: http://logging.apache.org/log4j/2.x/manual/configuration.html#Scripts http://logging.apache.org/log4j/2.x/manual/filters.html#Script
XInclude allows you to split a configuration file into multiple files.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude">
<Loggers>
<Root>
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
<xi:include href="log4j2-appender.xml"/>
</Configuration>
log4j2-appender.xml
<?xml version="1.0" encoding="UTF-8"?>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%d %p %m%n"/>
</Console>
</Appenders>
Reference: http://logging.apache.org/log4j/2.x/manual/configuration.html#XInclude
Recommended Posts