Java非web项目log4j的配置
1、Log4jConfig类:是用于配置log4j.properties的位置的。
package com.util;
import org.apache.log4j.PropertyConfigurator;
public class Log4jConfig {
private static boolean isReload = true;
public static void load() {
String path = Log4jConfig.class.getClass().getResource("/")
.getPath()
+ "config/log4j.properties";
//String path="config/log4j.properties";
System.out.println("log4j configfile path=" + path);
PropertyConfigurator.configureAndWatch(path,1000);
}
private static void reload() {
if (isReload) {
load();
}
isReload = false;
}
public void setReload(boolean flag) {
isReload = flag;
}
}
package com.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Logger {
private Log log = null;
static{
Log4jConfig.load();//装载log4j配置文件
}
private Logger() {
log = LogFactory.getLog(this.getClass());
}
private Logger(Class c) {
log = LogFactory.getLog(c);
}
private Logger(String className) {
log = LogFactory.getLog(className);
}
public static Logger getLogger() {
return new Logger();
}
public static Logger getLogger(Class c) {
return new Logger(c);
}
public static Logger getLogger(String className) {
return new Logger(className);
}
public void trace(String info) {
if (log.isTraceEnabled())
log.trace(info);
}
public void debug(String info) {
if (log.isDebugEnabled())
log.debug(info);
}
public void info(String info) {
if (log.isInfoEnabled())
log.info(info);
}
public void warn(String info) {
if (log.isWarnEnabled())
log.warn(info);
}
public void error(String info) {
if (log.isErrorEnabled())
log.error(info);
}
public void fatal(String info) {
if (log.isFatalEnabled())
log.fatal(info);
}
public boolean isTraceEnabled() {
return log.isTraceEnabled();
}
public boolean isDebugEnabled() {
return log.isDebugEnabled();
}
public boolean isInfoEnabled() {
return log.isInfoEnabled();
}
public boolean isWarnEnabled() {
return log.isWarnEnabled();
}
public boolean isErrorEnabled() {
return log.isErrorEnabled();
}
public boolean isFatalEnabled() {
return log.isFatalEnabled();
}
}
log4j.rootLogger=info, Console, RollingFile
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=logs/price.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#Project defalult level
log4j.logger.org.springside.examples.miniweb=INFO
#Forbidden struts2 ClassFinder message
log4j.logger.com.opensymphony.xwork2.util.finder.ClassFinder=OFF
4、测试
package testLog;
public class TestLog {
static Logger log=Logger.getLogger(TestLog.class);
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
log.info("----------info");
log.debug("----------debug");
log.error("----------error");
System.out.println("***********************");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}