Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logger(log4j) in Servlet

 
Anjali S Sharma
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anjali S Sharma
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if log4j is case sensitive or not.
Mine looks like this:
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${catalina.home} should work for a system property (not for environment variables)

log4j wiki FAQ
 
Sirish Kumar Gongal Reddy
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic