45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:封装log4j的步骤

封装log4j的步骤

2016-08-30 08:42:53 来源:www.45fan.com 【

封装log4j的步骤

Log4jImpl.java //Log4j的实现类

package com.ynwst.enterprise.sys;

/**
*

Title:


*

Description:


*

Copyright: Copyright (c) 2003 xiaowugui


*

Company:http://www.ynwst.com


* @author xiaowugui
* @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 Log4jImpl
{
public static final String PROFILE = "log4j.properties";
/**
* Holds singleton instance
*/
private static Log4jImpl impl;

static
{
impl = new Log4jImpl();
}

private Logger log4j;

/**
* prevents instantiation
*/
private Log4jImpl()
{
log4j = LogManager.getLogger(Log4jImpl.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);
}
}
/**
* 单例模式
*/
static public Log4jImpl getInstance()
{
return impl;
}
}



Log.java //记录Log使用类
package com.ynwst.enterprise.sys;

/**
*

Title:


*

Description:


*

Copyright: Copyright (c) 2003 xiaowugui


*

Company:


* @author xiaowugui
* @version 1.0
*/

public class Log
{
private static Log4jImpl log = Log4jImpl.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);
}
}


log4j.properties //Log4j配置文件
log4j.rootLogger=DEBUG, A2, A1
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=G:/enterprise/log/error.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


testLog.jsp //调用Log类
<%@ language="java" page="" pageencoding="GBK">
<%@ import="com.ynwst.enterprise.sys.Log" page="">
<%
Log.logDebug("DEBUG");
Log.logInfo("INFO");
Log.logWarn("WARN");
Log.logError("ERROR");
try
{
int i = Integer.parseInt("xxx");
}catch(Exception e)
{
Log.logDebug(e.toString());
Log.logInfo(e.toString());
Log.logWarn(e.toString());
Log.logError(e.toString());
}
%>

注:使用此方法封装Log4j的操作,可以使记录日志变得更方便。唯一不足的是无法返回当前类的相关信息,不过还好,我的错误信息都是自己定义的,所以一般在记录错误信息的时候,都会自己把当前类的相关信息做一个标识。
<%@ page="" language="java" pageencoding="GBK"><%@ page="" import="com.ynwst.enterprise.sys.Log"><%
 

本文地址:http://www.45fan.com/dnjc/69603.html
Tags: LOG4J 封装 Log4jImpl.java
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部