Log4j输出日志到单独日志文件

时间:2022-12-22 17:47:45

 

Log4j输出日志到单独日志文件

 

### 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
 
# 输出liveness日志
log4j.logger.liveness = INFO, liveness
# 是否在父log中输出日志
log4j.additivity.liveness = false
# 每天生产日志文件
log4j.appender.liveness = org.apache.log4j.DailyRollingFileAppender
# 生成日志到单独的文件
log4j.appender.liveness.File =../logs/liveness.log
# 追加日志
log4j.appender.liveness.Append = true
# 日志级别
log4j.appender.liveness.Threshold = INFO
log4j.appender.liveness.layout = org.apache.log4j.PatternLayout
# 日志格式

 

测试类
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() {
   // 测试打印普通日志
        log.info("test info");
    }
 
    @Test
    public void testLog2() {
   // 测试打印liveness模块日志到指定日志文件
        log2.info("test liveness");
    }
}

 

执行结果如下,liveness相关的日志不会打印到统一的log中:
Log4j输出日志到单独日志文件

在本地测试时刚开始无法打印日志到log文件,是因为配置文件中的File路径需要设置为全路径,如D:/projects/logs/liveness.log

 
 
我的项目中使用 完美 
 
 1 log4j.rootLogger=DEBUG,LogFileCometd,Console
 2 
 3 # 控制台输出
 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 日志文件输出
 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 # 输出push日志
19 log4j.logger.push =DEBUG,LogFile,ErrorFile
20 # 只输出自己指定的日志
21 log4j.additivity.push=false
22 # 每天生产日志文件
23 log4j.appender.LogFile=org.apache.log4j.DailyRollingFileAppender
24 # 生成日志到单独的文件
25 log4j.appender.LogFile.File=./logs/push.log
26 # 追加日志
27 log4j.appender.LogFile.Append=true
28 # 日志级别
29 log4j.appender.LogFile.Threshold=DEBUG
30 log4j.appender.LogFile.layout =org.apache.log4j.PatternLayout
31 # 日志格式
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  

 

代码测试类 部分代码
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();  //启动服务
    }
}

 

Log4j输出日志到单独日志文件