In this paper, the content

  • Based on the log4j.properties configuration file, the specified log is output to the specified file
  • I may not be accurate when I first write it, but I’ll refine it when I review it

Front knowledge

  • maven
  • log4j
  • slf4j
  • Including but not limited to the above

Demand background

1. The logs on one module and one machine require 10+GB, so I feel very tired to look through the logs when encountering problems. 3. If the system runs to module A and tries to catch the fault, the system prints error logs, outputs only or otherwise to A specified directory, and creates A specified log file. If there are logs, the error log file is saved in the format of the module name and date at the end of the day. If there are no logs, no log file is generated. 4. The above assumptions are completed to save time to locate whether the module throws an exception level problem. 5. There are still some problems to be solved, which will be gradually improved in the future

Specific operation

1. Create a Maven project

2. Create a folder

3. The directory structure is shown in the figure

4. Configure poM files

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <! Slf4j </groupId> <artifactId> slf4J-api </artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId> slf4j12 </artifactId> <version>1.7.12</version> </dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>Copy the code

5. Test class writing

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/ * * * *@author left 2019-06-25 23:16:42
 *
 */
public class LogTest {
 
    private static final Logger log = LoggerFactory.getLogger(LogTest.class);
    private static final Logger log2 = LoggerFactory.getLogger("special");
 
    @Test
    public void testLog(a) {
   // Tests printing ordinary logs
        log.info("test info");
    }
 
    @Test
    public void testLog2(a) {
   // Test printing liVENESS module logs to the specified log file
        log2.info("test special"); }}Copy the code

6. Log4j. Properties configuration

Log4j. rootLogger = INFO, C, F, E # # # # # # console console. Log4j appenders. C = org.. Apache log4j. ConsoleAppender log4j. Appender. C.T arget = System. Out log4j.appender.C.layout = org.apache.log4j.PatternLayout log4j.appender.C.layout.ConversionPattern = [%p] [% - d {MM - dd yyyy - HH: MM: ss}] % C % M % (L) | % M % n # # # # # # file file log4j. Appender. F = org.. Apache log4j. DailyRollingFileAppender  log4j.appender.F.File =/home/left/logs/mall/mall_info_log_ log4j.appender.F.File.DatePattern=yyyy-MM-dd'.log' log4j.appender.F.Append = true log4j.appender.F.Threshold = INFO log4j.appender.F.layout = org.apache.log4j.PatternLayout log4j.appender.F.layout.ConversionPattern = [%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | % m % n abnormal exception # # # # # #. Log4j appenders. E = org.. Apache log4j. DailyRollingFileAppender log4j. Appender. E.F ile =/home/left/logs/mall/mall_error_log_ log4j.appender.E.File.DatePattern=yyyy-MM-dd'.log' log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = [%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n ### JDBC ### log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.org.mybatis=DEBUG ### SQL ### log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG Log4j. Logger. Java, SQL PreparedStatement = DEBUG log4j. Logger. Java. SQL. The ResultSet = # DEBUG output special log log4j. Logger. Special =  INFO, Special # Log4j.additivity. Special = false # Log4j.appender.special = Org, apache log4j. DailyRollingFileAppender # generate log into separate files log4j. Appender. Special. The File = / home/left/logs/mall/special/special_. Log4j appenders. Special. The File. The DatePattern = yyyy - MM - dd 'log' # additional log . Log4j appenders. Special. Append = true # log level log4j. Appender. Special. The Threshold = INFO. Log4j appenders. Special. Layout = Org. Apache. Log4j.. # PatternLayout log format log4j appenders. Special. Layout. ConversionPattern = % [p] - [% d {MM - dd yyyy - HH: MM: ss}] %C.%M(%L) | %m%nCopy the code

7. The log file of the final running result is shown in the figure