日期:2014-05-18  浏览次数:21198 次

log4j中怎样将异常的完整信息写入日志文件?????
log4j中怎样将异常的完整信息写入日志文件?????
比如说,现在出现如下异常:
Java code

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:468)
    at java.lang.Integer.parseInt(Integer.java:497)
    at qing.log.struts.action.LiuAction.execute(LiuAction.java:44)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)


怎样将上面的内容记录到日志文件中???


------解决方案--------------------
%m%n
------解决方案--------------------
Java code
在log4j.properties文件!
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=D://info.log      //这里设置日志文件输出路径
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m %n
log4j.rootLogger=DEBUG, file

//定义一个类
public class LogInfo {
    Logger logger = Logger.getLogger(LogInfo.class.getName());

/**
 * 系统管理
 */
    /**
     * @param addmin(String
     *            name,String addname)添加管理员
     * @param name
     *            String
     * @param addname
     *            String
     */
    public void addadmin(String name, String addname) {
        try {
            logger.debug("管理员:" + name + "添加" + addname + "为管理员");
        } catch (Exception ex) {
            logger.info(ex.getMessage());
        }
    }
    //……各种各样的方法自己定义
}

------解决方案--------------------
当发生异常的时候
在catch处调用该方法即可!
new LogInfo().addadmin(u.getUserName(),userName);//记录到日志中
------解决方案--------------------
Java code

/**
     * org.apache.log4j.Logger
     * 
     * private static Logger log = Logger.getLogger("在log4j.properties文件中的 appenderName"); 
     * 把log4j.properties 放在src下.