aspose file tools*
The moose likes Other Open Source Projects and the fly likes Logger(log4j) in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Logger(log4j) in Servlet" Watch "Logger(log4j) in Servlet" New topic
Author

Logger(log4j) in Servlet

Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
I have a Servlet in which I am using log4j.

In my servlet I have written

public class MyServlet extends HttpServlet {

static Logger logger = Logger
.getLogger(com.MyServlet.class);

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {


logger.debug(" Logger comment");
}
}

Inside WEB-INF\properties directory there is log4j.properties file with the following content

log4j.rootCategory=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.FileAppender
log4j.appender.dest1.File=/logger.txt
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout

But when this servlet is called I can't find anywhere the logger.txt file.

Cannot put the properties file in classes, since I'll not be shipping the classes folder to the client. Instead jar file for all the classes will be shipped.
Also, properties file cannot be inside the jar file, as client will have to modify the properties file.

Please tell me how it'll be created.
[ April 04, 2007: Message edited by: Anjali S Sharma ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Log4j looks on the classpath to find the properties file.
Since web continers build their own classpaths, you need to place your files in particular directories if you want them to be found in such cases.

I put my log4j.properties file under /WEB-INF/classes for this reason.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
Originally posted by Ben Souther:


I put my log4j.properties file under /WEB-INF/classes for this reason.

i have tried that as well but its still not working.
I have put the properties file in classes directory and in log4j.properties I have written

log4j.rootCategory=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.FileAppender
log4j.appender.dest1.File=${catalina.home}/webapps/mydmin/logger.txt
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not sure if log4j is case sensitive or not.
Mine looks like this:
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Since you now know where to put your properies file in a web application, this is really no longer a servlet issue so I'm going to move it to Other Open Source Projects.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
log4j.appender.dest1.File=${catalina.home}/webapps/mydmin/logger.txt

Referring to a property like "${catalina.home}" will not work. There needs to be an absolute path here.


Ping & DNS - my free Android networking tools app
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
${catalina.home} should work for a system property (not for environment variables)

log4j wiki FAQ
Sirish Kumar Gongal Reddy
Ranch Hand

Joined: Oct 25, 2004
Posts: 109
Hello,

1) My first question is where you are trying to load your property file?

2) Push your log4j.properties to WEB-INF/classes.

First load your property file:
-----------------------------
LogManager.java
---------------
package com.examples;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class LogManager{

static
{
// initializes the log4j stuff
try{
Properties prop = new Properties();
InputStream is = LogManager.class.getResourceAsStream (log4j.properties);
prop.load(is);
PropertyConfigurator.configure(prop);
}catch(Exception ex){
ex.printStackTrace();
}
}
public Logger getLogger(Class arg0){
return Logger.getLogger(arg0);
}

}
log4j.properties
----------------
# Set root logger level to DEBUG and its only appender to stdout
log4j.rootLogger=INFO, filer
log4j.category.org.apache.jk.common.ChannelSocket=DEBUG, filer

# stdout is set to be a RollingFileAppender
log4j.appender.filer=org.apache.log4j.RollingFileAppender

log4j.additivity.org.apache.jk.common.ChannelSocket=false

# stdout uses PatternLayout
log4j.appender.filer=org.apache.log4j.RollingFileAppender
log4j.appender.filer.layout=org.apache.log4j.PatternLayout
log4j.appender.filer.layout.ConversionPattern=%d{MMM dd HH:mm:ss} %-5p [%t] %c{2} - %m%n
log4j.appender.filer.File=C:/test.log
log4j.appender.filer.MaxFileSize=100KB
log4j.appender.filer.MaxBackupIndex=4

Then your custom class it may be a servlet or a plain java class,

package com.examples;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.lf5.LogLevel;

public class LogExample {
public static void main(String[] args) throws Exception{

LogManager manager = new LogManager();
Logger log = manager.getLogger(LogExample.class);
//log.setLevel(LogLevel.INFO);
log.info("Test1");
}
}


Hope it will help you. Thanks!


/Sirish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logger(log4j) in Servlet