Solved

Logger in Servlet

Posted on 2007-04-03
11
545 Views
Last Modified: 2013-11-24
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.
Please tell me how it'll be created.


0
Comment
Question by:ryna_s
  • 5
  • 3
  • 3
11 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 18843002
the properties file belongs in WEB-INF/classes
0
 
LVL 13

Expert Comment

by:kawas
ID: 18843013
also, use the full path insteasd of /logger.txt, or the file will end up in the System.getProperty("user.dir") directory
0
 

Author Comment

by:ryna_s
ID: 18848484
>>also, use the full path insteasd of /logger.txt, or the file will end up in the System.getProperty("user.dir") directory

How to specify the full path. The path needs to be in context of tomcat.
I cannot specify something like C:\logger.txt because I don't know whether user will have a C: directory in his system or not. The logger file should be created somewhere inside the webapp which is deployed in Tomcat
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:ryna_s
ID: 18848495
>>the properties file belongs in WEB-INF/classes

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.
0
 

Author Comment

by:ryna_s
ID: 18849610
Anyone please
0
 
LVL 13

Accepted Solution

by:
kawas earned 400 total points
ID: 18850620
the path could be specified like so:
    ${catalina.home}/webapps/YOUR_APP_NAME
${catalina.home} is set when tomcat starts.

The place for the properties file is the classes folder, sorry about that.
0
 
LVL 13

Expert Comment

by:kawas
ID: 18850631
> The place for the properties file is the classes folder
You might want to try emailing the log people and asking them how you can load the file from somewhere else.
0
 
LVL 8

Expert Comment

by:thomas908
ID: 18850724
>>  ${catalina.home}/webapps/YOUR_APP_NAME

I have put it insdie classes, but its still not creating the logger file
0
 
LVL 13

Expert Comment

by:kawas
ID: 18850731
should have read ${catalina.home}/webapps/YOUR_APP_NAME/log.txt

Is that what you have?
0
 
LVL 8

Expert Comment

by:thomas908
ID: 18850738
You an try by giving java home, catalina home does not work on my system
0
 
LVL 8

Expert Comment

by:thomas908
ID: 18850743
oops,sorry the answer had already been accepted
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 36
ejb on wildfly 5 45
ejb entity bean example 2 31
How to log java errors in tomcat 8 24
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question