Log4j output log to separate log file

Keywords: log4j Apache Java SQL

 

Log4j output log to separate log file

09:01:03, January 31, 2018
Number of readings: 1065

Under normal circumstances, the info log of log4j will be printed to a unified log file. When you need to view the execution of a specific function, print a specific function to a specific log file as follows

log4j.properties

 

### set log levels ###
log4j.rootLogger = INFO , C , D , E
 
### console ###
log4j.appender.C = org.apache.log4j.ConsoleAppender
log4j.appender.C.Target = System.out
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern = [%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n
 
### log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File =../logs/IvaDubboWeb-info.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n
 
### exception ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = ../logs/IvaDubboWeb-error.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
 
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
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
 
# Output liveness log
log4j.logger.liveness = INFO, liveness
# Whether to output the log in the parent log
log4j.additivity.liveness = false
# Daily production log file
log4j.appender.liveness = org.apache.log4j.DailyRollingFileAppender
# Generate log to separate file
log4j.appender.liveness.File =../logs/liveness.log
# Supplemental Log 
log4j.appender.liveness.Append = true
# log level
log4j.appender.liveness.Threshold = INFO
log4j.appender.liveness.layout = org.apache.log4j.PatternLayout
# Log format

 

Test class
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * Created by Administrator on 2018\1\30 0030.
 */
public class LogTest {
 
    private static final Logger log = LoggerFactory.getLogger(LogTest.class);
    private static final Logger log2 = LoggerFactory.getLogger("liveness");
 
    @Test
    public void testLog() {
   // Test print general log
        log.info("test info");
    }
 
    @Test
    public void testLog2() {
   // Test printing liveness Module log to specified log file
        log2.info("test liveness");
    }
}

 

The execution results are as follows. liveness related logs will not be printed to the unified log:

At the beginning of the local test, the log cannot be printed to the log File because the File path in the configuration File needs to be set to the full path, such as D:/projects/logs/liveness.log

 
 
Perfect in my project
 
 1 log4j.rootLogger=DEBUG,LogFileCometd,Console
 2 
 3 # console output 
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender
 5 log4j.appender.Console.Target=System.out
 6 log4j.appender.Console.layout=org.apache.log4j.PatternLayout
 7 
 8 # cometd log file output
 9 log4j.appender.LogFileCometd=org.apache.log4j.DailyRollingFileAppender
10 log4j.appender.LogFileCometd.file=./logs/cometdpush.log
11 log4j.appender.LogFileCometd.append=true
12 log4j.appender.LogFileCometd.layout=org.apache.log4j.PatternLayout
13 log4j.appender.LogFileCometd.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
14 log4j.appender.LogFileCometd.Threshold=INFO
15 
16 
17 
18 # Output push log
19 log4j.logger.push =DEBUG,LogFile,ErrorFile
20 # Output only the logs specified by yourself
21 log4j.additivity.push=false
22 # Daily production log file
23 log4j.appender.LogFile=org.apache.log4j.DailyRollingFileAppender
24 # Generate log to separate file
25 log4j.appender.LogFile.File=./logs/push.log
26 # Supplemental Log 
27 log4j.appender.LogFile.Append=true
28 # log level
29 log4j.appender.LogFile.Threshold=DEBUG
30 log4j.appender.LogFile.layout =org.apache.log4j.PatternLayout
31 # Log format
32 log4j.appender.LogFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c - %F(%L) | %m%n  
33 
34 
35 log4j.appender.ErrorFile=org.apache.log4j.DailyRollingFileAppender
36 log4j.appender.ErrorFile.File=./logs/push_error.log
37 log4j.appender.ErrorFile.Append=true
38 log4j.appender.ErrorFile.Threshold=ERROR
39 log4j.appender.ErrorFile.layout =org.apache.log4j.PatternLayout
40 log4j.appender.ErrorFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c - %F(%L) | %m%n

 

Code test class part code
package cn.com.cfets.cfib.tbs.main;
import org.apache.log4j.Logger;public class StartServer {

    private final static Logger logger = Logger.getLogger("push");
public static void main(String[] args) throws Exception {
        org.apache.log4j.PropertyConfigurator.configure("./cfg/log4j.properties");
        logger.info("=================DSP_TBS_PLUGIN version: " + Version.getVersion() + " =================");
        logger.debug("=================DSP_TBS_PLUGIN version: " + Version.getVersion() + " =================");
        logger.info("Start server...");
        StartServer.instance.initAndStartImt();
        StartServer.instance.startServer();  //Start service
    }
}

 

Posted by rajah on Sat, 25 Apr 2020 07:31:37 -0700