java中使用log4j日志文件,读取配置文件的方式
public void init() {//该方法必须在所有log4j使用前调用
最好时初始化时就调用执行 加载好配置
Properties props = new Properties();//创建一个系统参数对象
try {
FileInputStream istream = new FileInputStream(filePath);//读取配置文件(log4j.properties)位置
props.load(istream);//将配置加载到系统参数对象中
istream.close();
//String logFile = prefix + props.getProperty("log4j.appender.file.File");//设置路径可以不经配置
//props.setProperty("log4j.appender.file.File",logFile);//可以不配置
PropertyConfigurator.configure(props);//装入log4j配置信息
} catch (IOException e) {
toPrint("Could not read configuration file [" + filePath + "].");
toPrint("Ignoring configuration file [" + filePath + "].");
return;
}
}
//System.out.println(System.getProperty("user.dir")); 获取当前的绝对路径
实际例子:
其中代码 log4j.properties中的配置
# Define the property
log.dir=../logs
log.appender=my.ceshi.data.log4j.LogRollingAppender 他的父类RollingAppender只支持日志分割(大小控制和数量控制)不能以时间命名日志
log.appender=my.ceshi.data.log4j.LogDailyRollingAppender 他的父类DailyRollingAppender只支持按照天或者月备份日志也就是以时间命名日志(不支持日志分割(大小控制和数量控制))
log.max.file.size=100KB
log.max.backup.index=4
log4j.rootLogger=all,debug,info,warn,error
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS}:[%-5p] method:%l%n%m%n
log4j.appender.debug=${log.appender}
#log4j.appender.debug=com.LogDailyRollingAppender
log4j.appender.debug.MaxFileSize=${log.max.file.size}
log4j.appender.debug.MaxBackupIndex=${log.max.backup.index}
log4j.appender.debug.File=${log.dir}/test_debug.log
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d %t %p[%c]-%m%n
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.append=true
log4j.appender.info=${log.appender}
#log4j.appender.info=com.LogDailyRollingAppender
log4j.appender.info.MaxFileSize=${log.max.file.size}
log4j.appender.info.MaxBackupIndex=${log.max.backup.index}
log4j.appender.info.File=${log.dir}/test_info.log
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d %t %p[%c]-%m%n
log4j.appender.info.Threshold=INFO
log4j.appender.info.append=true
log4j.appender.warn=${log.appender}
#log4j.appender.warn=com.LogDailyRollingAppender
log4j.appender.warn.MaxFileSize=${log.max.file.size}
log4j.appender.warn.MaxBackupIndex=${log.max.backup.index}
log4j.appender.warn.File=${log.dir}/test_warn.log
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%d %t %p[%c]-%m%n
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.append=true
log4j.appender.error=${log.appender}
#log4j.appender.error=com.LogDailyRollingAppender
log4j.appender.error.MaxFileSize=${log.max.file.size}
log4j.appender.error.MaxBackupIndex=${log.max.backup.index}
log4j.appender.error.File=${log.dir}/test_error.log
log4j.appender.error.Threshold=ERROR
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d %t %p[%c]-%m%n
log4j.appender.error.append=true
其中日志配置可参考http://www.cnblogs.com/alipayhutu/archive/2012/06/21/2558249.html
其中类 LogDailyRollingAppender.java 的代码
package my.ceshi.data.log4j;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Priority;
/**
* 日志级别工具类
*/
public class LogDailyRollingAppender extends DailyRollingFileAppender{
/**
* 只打印等于当前log定义级别的log。
*/
public boolean isAsSevereAsThreshold(Priority priority) {
return this.getThreshold().equals(priority);
}
}
其中LogRollingAppender.java的代码
package my.ceshi.data.log4j;
import org.apache.log4j.Priority;
import org.apache.log4j.RollingFileAppender;
public class LogRollingAppender extends RollingFileAppender {
/**
* 只打印等于当前log定义级别的log。
*/
public boolean isAsSevereAsThreshold(Priority priority) {
return this.getThreshold().equals(priority);
}
}
测试方法类 Test.java类代码
package my.ceshi.data.log4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
private static final Logger log = Logger.getLogger(Test.class);
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
init();
while (true) {
log.debug(
"这是debug打印的日志 充字数曾经介绍过 KolourPaint,一款和 Windows 下 “画图” 概念相同的简易画图工具。不过 KolourPaint 是 KDE 下的");
log.info(
"这是info打印的日志 充字数曾经介绍过 KolourPaint,一款和 Windows 下 “画图” 概念相同的简易画图工具。不过 KolourPaint 是 KDE 下的");
log.warn(
"这是warn打印的日志 充字数曾经介绍过 KolourPaint,一款和 Windows 下 “画图” 概念相同的简易画图工具。不过 KolourPaint 是 KDE 下的");
log.error(
"这是error打印的日志 充字数曾经介绍过 KolourPaint,一款和 Windows 下 “画图” 概念相同的简易画图工具。不过 KolourPaint 是 KDE 下的");
Thread.sleep(2000);
}
}
public static void init() throws IOException {
String s = System.getProperty("user.dir");
FileInputStream in = new FileInputStream(new File(s + "/log4j.properties"));
Properties p = new Properties();
p.load(in);
in.close();
PropertyConfigurator.configure(p);
}
}
其中的pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.ceshi</groupId>
<artifactId>data</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>data</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--配置的代理服务器-->
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://***.168.***.***:8181/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://***.168.***.***:8181/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<!-- redis相关 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- log4j日志文件 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- testNg -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
<!-- StringUtils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.opensymphony.quartz/quartz -->
<dependency>
<groupId>org.opensymphony.quartz</groupId>
<artifactId>quartz</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging
quartz依赖的包 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections
quartz依赖的包 -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
</project>