Log4J的封裝:
配置文件:log4j.properties
log4j.rootLogger=DEBUG, A2, A1
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=C:developlogerror.log
log4j.appender.A2.Append=true
log4j.appender.R.MaxFileSize=10000KB
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%-5p][%t] %d{yyyy-MM-dd
HH:mm:ss,SSS} message:%m%n
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p - %m%n
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=C:developlogerror.log
log4j.appender.A2.Append=true
log4j.appender.R.MaxFileSize=10000KB
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%-5p][%t] %d{yyyy-MM-dd
HH:mm:ss,SSS} message:%m%n
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p - %m%n
EncapsulationLog4J.java //Log4j的實現類
package com.cn.lx;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2004 lixiang
*
Company:http://www.css.com.cn/
* @author lixiang
* @version 1.0
*/
import org.apache.log4j.*;
import java.io.*;
import java.util.*;
/**
*
* @author Administrator
*
*
*
* To change the template for this generated type comment go to
*
* Window>Preferences>Java>Code Generation>Code and Comments
*
*/
public class EncapsulationLog4J
{
public static final String PROFILE = "log4j.properties";
/**
*
* Holds singleton instance
*
*/
private static EncapsulationLog4J impl;
static
{
impl = new EncapsulationLog4J();
}
private Logger log4j;
/**
*
* prevents instantiation
*
*/
private EncapsulationLog4J()
{
log4j = LogManager.getLogger(EncapsulationLog4J.class);
try
{
Properties pro = new Properties();
InputStream is = getClass().getResourceAsStream(PROFILE);
pro.load(is);
PropertyConfigurator.configure(pro);
}
catch (IOException e)
{
BasicConfigurator.configure();
e.printStackTrace();
}
}
public void log(String level, Object msg)
{
log(level, msg, null);
}
public void log(String level, Throwable e)
{
log(level, null, e);
}
public void log(String level, Object msg, java.lang.Throwable e)
{
if (log4j != null)
{
log4j.log((Priority) Level.toLevel(level), msg, e);
}
}
/**
*
* Singleton Pattern
*
*/
static public EncapsulationLog4J getInstance()
{
return impl;
}
}
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2004 lixiang
*
Company:http://www.css.com.cn/
* @author lixiang
* @version 1.0
*/
import org.apache.log4j.*;
import java.io.*;
import java.util.*;
/**
*
* @author Administrator
*
*
*
* To change the template for this generated type comment go to
*
* Window>Preferences>Java>Code Generation>Code and Comments
*
*/
public class EncapsulationLog4J
{
public static final String PROFILE = "log4j.properties";
/**
*
* Holds singleton instance
*
*/
private static EncapsulationLog4J impl;
static
{
impl = new EncapsulationLog4J();
}
private Logger log4j;
/**
*
* prevents instantiation
*
*/
private EncapsulationLog4J()
{
log4j = LogManager.getLogger(EncapsulationLog4J.class);
try
{
Properties pro = new Properties();
InputStream is = getClass().getResourceAsStream(PROFILE);
pro.load(is);
PropertyConfigurator.configure(pro);
}
catch (IOException e)
{
BasicConfigurator.configure();
e.printStackTrace();
}
}
public void log(String level, Object msg)
{
log(level, msg, null);
}
public void log(String level, Throwable e)
{
log(level, null, e);
}
public void log(String level, Object msg, java.lang.Throwable e)
{
if (log4j != null)
{
log4j.log((Priority) Level.toLevel(level), msg, e);
}
}
/**
*
* Singleton Pattern
*
*/
static public EncapsulationLog4J getInstance()
{
return impl;
}
}
Log.java //記錄Log使用類
package com.cn.lx;
/**
*
*
*
* Title:
*
*
*
* Description:
*
*
*
* Copyright: Copyright (c) 2004 lixiang
*
*
*
* Company: http://www.css.com.cn/
*
* @author lixiang
*
* @version 1.0
*
*/
public class Log
{
private static EncapsulationLog4J log = EncapsulationLog4J.getInstance();
/**
*
*
*
*/
public Log()
{
// super();
}
public static void logError(String msg)
{
log.log("ERROR", msg);
}
public static void logError(Throwable e)
{
log.log("ERROR", null, e);
}
public static void logWarn(String msg)
{
log.log("WARN", msg);
}
public static void logWarn(Throwable e)
{
log.log("WARN", null, e);
}
public static void logInfo(String msg)
{
log.log("INFO", msg);
}
public static void logInfo(Throwable e)
{
log.log("INFO", null, e);
}
public static void logDebug(String msg)
{
log.log("DEBUG", msg);
}
public static void logDebug(Throwable e)
{
log.log("DEBUG", null, e);
}
}
/**
*
*
*
* Title:
*
*
*
* Description:
*
*
*
* Copyright: Copyright (c) 2004 lixiang
*
*
*
* Company: http://www.css.com.cn/
*
* @author lixiang
*
* @version 1.0
*
*/
public class Log
{
private static EncapsulationLog4J log = EncapsulationLog4J.getInstance();
/**
*
*
*
*/
public Log()
{
// super();
}
public static void logError(String msg)
{
log.log("ERROR", msg);
}
public static void logError(Throwable e)
{
log.log("ERROR", null, e);
}
public static void logWarn(String msg)
{
log.log("WARN", msg);
}
public static void logWarn(Throwable e)
{
log.log("WARN", null, e);
}
public static void logInfo(String msg)
{
log.log("INFO", msg);
}
public static void logInfo(Throwable e)
{
log.log("INFO", null, e);
}
public static void logDebug(String msg)
{
log.log("DEBUG", msg);
}
public static void logDebug(Throwable e)
{
log.log("DEBUG", null, e);
}
}
TestLog.java //調用Log類
package com.cn.lx;
public class TestLog {
public static void main(String[] args) {
Log test = new Log();
test.logDebug("DEBUG");
test.logInfo("INFO");
test.logWarn("WARN");
test.logError("ERROR");
try
{
int i = Integer.parseInt("lixiang");
} catch (Exception e)
{
test.logDebug(e.toString());
test.logInfo(e.toString());
test.logWarn(e.toString());
test.logError(e.toString());
}
}
}
public class TestLog {
public static void main(String[] args) {
Log test = new Log();
test.logDebug("DEBUG");
test.logInfo("INFO");
test.logWarn("WARN");
test.logError("ERROR");
try
{
int i = Integer.parseInt("lixiang");
} catch (Exception e)
{
test.logDebug(e.toString());
test.logInfo(e.toString());
test.logWarn(e.toString());
test.logError(e.toString());
}
}
}
執行後的日誌信息:
2004-05-26 21:16:16,474 [main] DEBUG - DEBUG
2004-05-26 21:16:16,484 [main] INFO - INFO
2004-05-26 21:16:16,484 [main] WARN - WARN
2004-05-26 21:16:16,484 [main] ERROR - ERROR
2004-05-26 21:16:16,484 [main] DEBUG - java.lang.NumberFormatException: For input string:
"lixiang"
2004-05-26 21:16:16,484 [main] INFO - java.lang.NumberFormatException: For input string:
"lixiang"
2004-05-26 21:16:16,484 [main] WARN - java.lang.NumberFormatException: For input string:
"lixiang"
2004-05-26 21:16:16,484 [main] ERROR - java.lang.NumberFormatException: For input string:
"lixiang"
2004-05-26 21:16:16,484 [main] INFO - INFO
2004-05-26 21:16:16,484 [main] WARN - WARN
2004-05-26 21:16:16,484 [main] ERROR - ERROR
2004-05-26 21:16:16,484 [main] DEBUG - java.lang.NumberFormatException: For input string:
"lixiang"
2004-05-26 21:16:16,484 [main] INFO - java.lang.NumberFormatException: For input string:
"lixiang"
2004-05-26 21:16:16,484 [main] WARN - java.lang.NumberFormatException: For input string:
"lixiang"
2004-05-26 21:16:16,484 [main] ERROR - java.lang.NumberFormatException: For input string:
"lixiang"
注:使用此方法封裝Log4j的操作,可以使記錄日誌變得更方便。唯一不足的是無法返回當
前類的相關信息。
最後說明:
關於對日誌進行處理的技術有好多,如JDK 1.4 logging、Avalon LogKit、Jakarta 的Log4J
、Jakarta的Commons-Logging等。做的最好的是Commons-Logging。下面對其做一介紹:
The Jakarta Commons Logging (JCL) provides a Log interface that is
intended to be both light-weight and independent of numerous logging
toolkits. It provides the middleware/tooling developer with a simple logging
abstraction, that allows the user (application developer) to plug in a specific
logging implementation.
The Jakarta Commons Logging provides a Log interface with
thin-wrapper implementations for other logging tools, including
Log4J , Avalon LogKit , the Avalon Framework's logging
infrastructure, JDK 1.4, and an implementation of JDK 1.4 logging
APIs (JSR-47) for pre-1.4 systems. The interface maps closely to
Log4J and LogKit.
intended to be both light-weight and independent of numerous logging
toolkits. It provides the middleware/tooling developer with a simple logging
abstraction, that allows the user (application developer) to plug in a specific
logging implementation.
The Jakarta Commons Logging provides a Log interface with
thin-wrapper implementations for other logging tools, including
Log4J , Avalon LogKit , the Avalon Framework's logging
infrastructure, JDK 1.4, and an implementation of JDK 1.4 logging
APIs (JSR-47) for pre-1.4 systems. The interface maps closely to
Log4J and LogKit.