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