This article is the 16th day article of Hamee Advent Calendar 2020. It's my first time to participate in ad-care, I'm really nervous (lie)
From Official
Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture.
It's an improved version of log4j. The harmful effects of taking over the structure of Logback have also been resolved nicely.
And that.
What is log4j in the first place? If you explain in one line to those who say log4j is a logger API for Java.
It's also called log rotation.
I think that any system outputs logs, but the amount of output will increase over time and will devour storage. That's not good, so you need to automatically delete logs after a certain period of time, or delete old ones when the size of the log reaches a certain amount.
This is log rotation.
First, to define the logger, write log4j2.xml as follows.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
<Properties>
<Property name="standard">[%d{yyyy/MM/dd HH:mm:ss.SSS}] %m%n</Property>
<Property name="log_path">logs</Property>
<Property name="log_filename">app</Property>
</Properties>
<Appenders>
<RollingFile name="app" fileName="${log_path}/${log_filename}.log" filePattern="${log_path}/${log_filename}.%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="[%d{yyyy.MM.dd HH:mm:ss.SSS}] %p - %m%n" />
<TimeBasedTriggeringPolicy />
<DefaultRolloverStrategy>
<Delete basePath="${log_path}" maxDepth="1">
<IfFileName glob="${log_filename}*.log.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root name="app_logger" level="info">
<AppenderRef ref="app" />
</Root>
</Loggers>
</Configuration>
I will explain the settings a little bit.
<Properties>
<Properties>
<Property name="standard">[%d{yyyy/MM/dd HH:mm:ss.SSS}] %m%n</Property>
<Property name="log_path">logs</Property>
<Property name="log_filename">app</Property>
</Properties>
A tag that can be used when you want to use a constant in log4j2.xml.
For example, with the above settings, you can use the format enclosed in <Property name =" standard ">
by writing $ {standard}
in one shot.
After all, this setting
<RollingFile name="app" fileName="${log_path}/${log_filename}.log" filePattern="${log_path}/${log_filename}.%d{yyyy-MM-dd}.log.gz">
It is converted like this.
<RollingFile name="app" fileName="logs/app.log" filePattern="logs/app.%d{yyyy-MM-dd}.log.gz">
<Loggers>
<Loggers>
<Root name="app_logger" level="info">
<AppenderRef ref="app" />
</Root>
</Loggers>
I will write the definition of logger here.
<Root>
defines the logger to be used by default, but this time there is only one, so this is OK.
ref =" app "
receives the output from the name =" app "
guy defined in <Appenders>
, meaning.
Now you have defined the interface that outputs the log (I think it is selfish).
<RollingFile>
<Appenders>
<RollingFile name="app" fileName="${log_path}/${log_filename}.log" filePattern="${log_path}/${log_filename}.%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="[%d{yyyy.MM.dd HH:mm:ss.SSS}] %p - %m%n" />
<TimeBasedTriggeringPolicy />
<DefaultRolloverStrategy>
<Delete basePath="${log_path}" maxDepth="1">
<IfFileName glob="${log_filename}*.log.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
Define the format that is actually output as a log in <Appenders>
.
<RollingFile>
is one of them.
I will omit the details, ** Log rotation is done on a daily basis, and logs older than 7 days are deleted! ** ** The settings like this are made here.
On the server side, add some kind of log output operation, deploy, and wait for the date to change. When I ssh and check it with excitement, is that?
$ ls -la
drwxr-xr-x 3 tomcat tomcat 4096 December 12 23:39 .
drwxr-xr-x 3 tomcat tomcat 4096 December 12 16:44 ..
-rw-r--r--1 tomcat tomcat 207 December 13 00:00 app.log
** Are? ** **
No matter how many times I check today's date, 12/13 ... Normally, log rotation should be done and 12/12 days worth of logs should remain ...
Originally I want it to be ↓.
$ ls -la
drwxr-xr-x 3 tomcat tomcat 4096 December 12 23:39 .
drwxr-xr-x 3 tomcat tomcat 4096 December 12 16:44 ..
-rw-r--r--1 tomcat tomcat 20507 December 13 00:00 app.2020-12-12.log
-rw-r--r--1 tomcat tomcat 207 December 13 00:00 app.log
(I'll write it lightly here, but it usually takes about a week to fix it.)
First, take a look at my crontab.
*/10 * * * * sh ./batch1
0 0 1 * * sh ./batch2
I'm just setting up a periodic batch with cron.
At first glance, it's a normal setting, but the point here is that ** both batches are executed at 0:00:00 **. This is the cause of everything.
Apparently in log4j2
--** Simultaneous ** writing to logs by multiple processes --Target for log rotation
When the above two conditions are met, log rotation is not performed normally and the log of the previous day disappears. (Is that okay ...?)
It doesn't matter if batch2 doesn't run at 0:00:00, so I tried logrotate with a slight time lag and it worked fine.
(By the way, there seems to be a ScoketAppender to solve this? I'm sorry if I make a mistake.)
Despite the framework specifications, I didn't think I would be bothered by log output so far. Have a good log life!
Recommended Posts