log4j collation in production environment

Keywords: log4j Apache Java xml

log4j collation in production environment

There are many improvements in the log of the company's java underlying code. This article mainly records the difficulties encountered in the process of improvement and records them in order to avoid forgetting (here I use log4j.propertes, not log4j.xml).

  • How to configure log paths in non-web projects?
  • What's the difference between log4j.rootLogger and log4j.rootCategory?
  • What is XXX in log4j.logger.xxx?
  • How to automatically delete the log of N days ago?

premise

maven Import log4j Of jar Bag:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

Description: slf4j is the specification and log4j is the implementation.

Start solving

** How to configure log paths in non-web projects?

At the entry and exit of the program, that is, the first sentence of the class of main method, add a static code block:

static {
    PropertyConfigurator.configure("config/log4j.properties");
}

Note that the two representations are the same, and you can see their source code:

private static final Logger log = Logger.getLogger(Test.class);
private static final Logger log = Logger.getLogger(Test.class.getName());

** What is the difference between log4j.rootLogger and log4j.rootCategory?

Category class in the description of the official website is abandoned, try not to use! Therefore, the global configuration is:

log4j.rootLogger=INFO,A1,CONSOLE
log4j.appender.A1.Threshold=ERROE
log4j.appender.A1=.....
(Omit here)

** What is xxx in log4j. logger. xxxx?

1. Here xxx can be a package name or a class name, and here com.main is a package name.

log4j.logger.com.main=INFO,A2
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.File=/var/log/main.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t] %5p %x - %m%n

2. The xxx here can be the name of the log, and the log using that name will be output to the configuration.

private static final Logger log = Logger.getLogger("main");
log4j.logger.main=INFO,A3
log4j.appender.A3=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A3.File=/var/log/main.log
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t] %5p %x - %m%n

** How to automatically delete the log of N days ago?

If you use log4j, and it's Daily Rolling File Appender, you can't use this feature, you have to rewrite Daily Rolling File Appender, which I won't say much about. It's a bunch of things online. After rewriting, configure it, such as:

#Log Path
log4j.logUrl=/var/log
#Days of log retention
log4j.maxBackupIndex=10

log4j.logger.main=INFO,A3
#Here's a rewrite
log4j.appender.A3=org.apache.log4j.CustomDailyRollingFileAppender
log4j.appender.A3.File=${log4j.logUrl}/main.log
log4j.appender.A3.maxBackupIndex=${log4j.maxBackupIndex}
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t] %5p %x - %m%n

Complete log4j.properties

#Log Path
log4j.logUrl=/var/log
#Days of log retention
log4j.maxBackupIndex=10

#Global configuration A1 info log; A2 error log;
log4j.rootLogger=INFO,A1,A2

log4j.appender.A1.Threshold=INFO
log4j.appender.A1=org.apache.log4j.CustomDailyRollingFileAppender
log4j.appender.A1.File=${log4j.logUrl}/info.log
log4j.appender.A1.maxBackupIndex=${log4j.maxBackupIndex}
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t] %5p %x - %m%n

log4j.appender.A2.Threshold=ERROR
log4j.appender.A2=org.apache.log4j.CustomDailyRollingFileAppender
log4j.appender.A2.File=${log4j.logUrl}/error.log
log4j.appender.A2.maxBackupIndex=${log4j.maxBackupIndex}
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t] %5p %x - %m%n


#Logger.getLogger("sqlMonitor") log output
log4j.logger.sqlMonitor=INFO,A3
log4j.appender.A3=org.apache.log4j.CustomDailyRollingFileAppender
log4j.appender.A3.File=${log4j.logUrl}/sql_monitor.log
log4j.appender.A3.maxBackupIndex=${log4j.maxBackupIndex}
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t] %5p %x - %m%n

#Log output under com.zqh.dao package
log4j.logger.com.zqh.dao=INFO,A4
log4j.appender.A4=org.apache.log4j.CustomDailyRollingFileAppender
log4j.appender.A4.File=${log4j.logUrl}/dao.log
log4j.appender.A4.maxBackupIndex=${log4j.maxBackupIndex}
log4j.appender.A4.layout=org.apache.log4j.PatternLayout
log4j.appender.A4.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t] %5p %x - %m%n
......

Posted by TF@TheMoon on Mon, 27 May 2019 13:26:55 -0700