views:

25

answers:

0

Hi, i want to log for a web apps in tomcat6. The log file is located at /project/Log in ubuntu. I am using juli as the logger for my log and i have no logging.properties define. Instead, i just hardcode the handler and the path of the logger in a class called LogHelper. Here is the code of it: `

package de; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList;

import java.util.logging.Filter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; import org.apache.juli.FileHandler; /** * * @author root */ public class LogHelper { private static String nameOfLogger = TargetClass.class.getName();

private static LogHelper _logHelper;
private String _logFilename = "server";
private String parentDir = ".";

private LogHelper(){
    try {

        File file = new File(LogHelper.class.getResource("./config.xml").getPath());
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);
        doc.getDocumentElement().normalize();
        NodeList loggerNodes = doc.getElementsByTagName("Logger");

        if(loggerNodes.getLength() > 0){
        Node loggerNode = loggerNodes.item(0);

            if (loggerNode.getNodeType() == Node.ELEMENT_NODE) {
                this.parentDir = ((Element)loggerNode).getAttribute("ParentDir");
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static LogHelper getInstance(){
    if(LogHelper._logHelper == null){
        LogHelper._logHelper = new LogHelper();
    }
    return LogHelper._logHelper;
}

private Logger getLogger(String id){
    try{
        Logger myLogger = Logger.getLogger(nameOfLogger);

        Handler[] handlers = myLogger.getHandlers();
        for(int i = 0; i < handlers.length; i++){
            myLogger.removeHandler(handlers[i]);
        }

        Handler fileHandler = new FileHandler(parentDir, id + "-", ".log");
        fileHandler.setFilter(new Filter(){
            public boolean isLoggable(LogRecord record) {
                return true;
            }
        });
        myLogger.addHandler(fileHandler);
        return myLogger;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public void Log(String msg){
    this.Log(this._logFilename, msg, Level.INFO);
}

public void Log(String id, String msg){
    this.Log(id, msg, Level.INFO);
}

public void Log(String id, String msg, Level nature){
    Logger logger = this.getLogger(id);
    logger.log(nature, msg);
}

public void LogError(Exception e){
    this.LogError(_logFilename, e);
}

public void LogError(String id, Exception e){
    String msg = e.getClass().toString();
    for(int i = 0; i < e.getStackTrace().length; i++){
        msg += "\n\t" + e.getStackTrace()[i];
    }
    this.Log(id, msg, Level.SEVERE);
}

} `

In TargetClass, i basically just call LogHelper.getInstance().Log("10127", "Ah Yeah Baby - 10127");.

i first test in Windows xp platform. It works great. However, i cannot get it done in Ubuntu platform. The only thing i get in cataline.out is

java.util.logging.ErrorManager: 4 java.io.FileNotFoundException: /project/Log/10127-2010-10-13.log (No such file or directory) at java.io.FileOutputStream.openAppend(Native Method) at java.io.FileOutputStream.(FileOutputStream.java:177) at java.io.FileOutputStream.(FileOutputStream.java:102) at org.apache.juli.FileHandler.openWriter(FileHandler.java:304) at org.apache.juli.FileHandler.(FileHandler.java:63) at de.LogHelper.getLogger(LogHelper.java:71) at de.LogHelper.Log(LogHelper.java:94) at de.LogHelper.Log(LogHelper.java:90) at de.TargetClass.testLog(TargetClass.java:17) at org.apache.jsp.index_jsp._jspService(index_jsp.java:71) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 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:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 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:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619)

INFO: Ah Yeah Baby - 10127

Any help?