log4j2
目录
依赖
配置文件
指定配置文件路径
示例
依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.2</version>
</dependency>
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="myConsole" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{MM-dd HH:mm:ss,SSS\} %-5p] [%t] %C{1} - %m%n"/>
</Console>
<RollingFile name="common" fileName="/aaa/bbb/ccc/filename.log" filePattern="/aaa/bbb/ccc/filename.log.%d{yyyy-MM-dd}">
<PatternLayout>
<Pattern>[%d{MM-dd HH:mm:ss SSS\} %-5p] [%t] %C{1} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="payment" fileName="/aaa/bbb/ccc/filename.log" filePattern="/aaa/bbb/ccc/filename.log.%d{yyyy-MM-dd}.log">
<PatternLayout>
<Pattern>[%d{MM-dd HH:mm:ss SSS\} %-5p] [%t] %C{3\} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<logger name="payment" level="INFO" additivity="false">
<AppenderRef ref="payment" />
</logger>
<Root level="info">
<AppenderRef ref="myConsole" />
<AppenderRef ref="common" />
</Root>
</Loggers>
</Configuration>
指定配置文件路径
public static String setLogConfigFilePath(){
Logger log;
ConfigurationSource source;
String config = System.getProperty("user.dir");
String fullPath = config + "\\log4j2.xml";
System.out.println("Log4j2 fullPath = " + fullPath);
File file = new File(fullPath);
//this ConfigurationSource() could load xml dynamicly, info->debug->info test OK!!!
try {
source = new ConfigurationSource(new FileInputStream(file), file);
Configurator.initialize(null, source);
log = LogManager.getLogger("receive");
log.info("haha~~");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fullPath;
}
示例
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class AnotherClazz {
private static Logger log = LogManager.getLogger(AnotherClazz.class);
public static void sayHi(){
System.out.println("hi~~~");
log.info("hi~~~");
}
}
log4j
配置文件
log4j.rootLogger=INFO, deallog
log4j.appender.deallog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.deallog.File=/aaa/bbb/logs/post-payback-esb.log
log4j.appender.deallog.Append=true
log4j.appender.deallog.Threshold=INFO
log4j.appender.deallog.layout=org.apache.log4j.PatternLayout
log4j.appender.deallog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.additivity.esblog=false
log4j.logger.esblog=INFO,esblog
log4j.appender.esblog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.esblog.File=/aaa/bbb/logs/post_payback.esbreceive.log
log4j.appender.esblog.Append=true
log4j.appender.esblog.Threshold=INFO
log4j.appender.esblog.layout=org.apache.log4j.PatternLayout
log4j.appender.esblog.layout.ConversionPattern=%d{ABSOLUTE} - %m%n
指定配置文件路径
public static String setLogConfigFilePath(){
PropertyConfigurator.configure("E://log4j.properties");
Logger log = Logger.getLogger(TaskMain.class);
log.info("haha");
}