[prev in list] [next in list] [prev in thread] [next in thread] 

List:       log4j-user
Subject:    Re: Why log4j logs to ${TOMCAT}/bin?
From:       Chen Jerry <tech.jerry.chen () gmail ! com>
Date:       2005-11-19 4:20:45
Message-ID: 2d45a5670511182020w725eddbbr () mail ! gmail ! com
[Download RAW message or body]


Dear Jacob,

Your explanation clear me up. Thank you very much!

Jerry


2005/11/19, Jacob Kjome <hoju@visi.com>:
>
> Quoting Chen Jerry <tech.jerry.chen@gmail.com>:
>
> > Hi All,
> >
> > I use log4j in my app, expecting messages logged to
> > ${TOMCAT}/webapps/jsp-examples/WEB-INF/classes/mylog.txt,
> > so I leverage a FileAppender, whose file is set to ./mylog.txt.
> > When the following code is tested in
> Win2000/JDK1.4.2_07/Tomcat5.0.28(zip),
> > I was astonished to find that mylog.txt had been generated in
> ${TOMCAT}/bin
> > and logged messages!
> >
> > What is wrong?
> >
>
> Well, if it **didn't** work the way you describe above, I'd be astonished.
> Keep
> in mind that relative file paths are resolved to the directory where the
> JVM
> started. Tomcat's batch files set this to CATALINA_HOME/bin. As such, a
> file
> path of "./mylog.txt" should show up no other place than
> CATALINA_HOME/bin.
>
> You seem to expect that a file path has something to do with classloaders,
> specifically with your current webapp's classloader. The relationship
> between
> files and classloaders is absolutely non-existent.
>
> If you want to log to a location inside your webapp (which is not
> guaranteed to
> work, BTW, since your webapp could be deployed directly from a .WAR file.
> The
> servlet spec specifically makes no guarantee to file system access to a
> directory structure inside your webapp since this could simply be an
> archive),
> then you should set a system property upon startup and obtiain the file
> path
> via something like servletContext.getRealPath("/WEB-INF/logs") and set the
> value to said system propery. Then, in your config file, reference the
> path
> like this..
>
> log4j.appender.stdout.File=${mywebapp.logging.path}/mylog.txt
>
> Of course, you'll have to perform manual configuration after the system
> propery
> has been set. All of this can be done in a servlet context listener at
> webapp
> startup.
>
>
> Jake
>
> > Thanks in advance.
> >
> > Jerry
> >
> > logit.jsp, placed in ${TOMCAT}/webapps/jsp-examples/
> >
> > <%@ page language="java" import="my.*,org.apache.log4j.helpers.*,
> > org.apache.log4j.*,java.io.*"%>
> > <%MyApp.main(new String[]{"pa","pb"});%>
> >
> > Bar.java , whose class file is placed in
> > ${TOMCAT}/webapps/jsp-examples/WEB-INF/classes/my
> >
> > package my;
> >
> > import org.apache.log4j.Logger;
> >
> > public class Bar {
> > static Logger logger = Logger.getLogger(Bar.class);
> >
> > public void doIt() {
> > logger.debug("Did it again!");
> > }
> > }
> >
> >
> > MyApp.java, whose class file is placed in
> > ${TOMCAT}/webapps/jsp-examples/WEB-INF/classes/my
> >
> > package my;
> >
> > // Import log4j classes.
> > import org.apache.log4j.Logger;
> > import org.apache.log4j.*;
> > import org.apache.log4j.helpers.*;
> > import java.util.*;
> >
> > public class MyApp {
> >
> > // Define a static logger variable so that it references the
> > // Logger instance named "MyApp".
> > // static String resource = Loader.getResource("mylog.txt").toString();
> > static Logger logger = Logger.getLogger(MyApp.class);
> >
> > public static void main(String[] args) {
> > logger.info("Entering application.");
> > Bar bar = new Bar();
> > bar.doIt();
> > logger.info("Exiting application.");
> > }
> > }
> >
> >
> > log4j.properties, placed in
> ${TOMCAT}/webapps/jsp-examples/WEB-INF/classes/
> >
> > # Set root logger level to DEBUG and its only appender to A1.
> > log4j.rootLogger=DEBUG, A1, stdout
> >
> > # A1 is set to be a ConsoleAppender.
> > log4j.appender.A1=org.apache.log4j.ConsoleAppender
> >
> > log4j.appender.stdout=org.apache.log4j.FileAppender
> > log4j.appender.stdout.File=./mylog.txt
> > log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
> >
> > # A1 uses PatternLayout.
> > log4j.appender.A1.layout=org.apache.log4j.PatternLayout
> > log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
> >
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic