Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:31:42+00:00 2026-06-01T09:31:42+00:00

I want to use log4j in my jsp s and servlets. I read apache

  • 0

I want to use log4j in my jsp s and servlets. I read apache docs and this blog.I tried as that blog’s second method. and here is my property file

log4j.logger.myapplogger=DEBUG, C, fileappender
 
log4j.additivity.myapplogger=true
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.layout=org.apache.log4j.PatternLayout
#basic pattern
log4j.appender.C.layout.ConversionPattern=[%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n
#advanced pattern (slow)
#log4j.appender.C.layout.ConversionPattern=[%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m - in %M() at line %L of class %C %n 
 
log4j.appender.fileappender=org.apache.log4j.RollingFileAppender
log4j.appender.fileappender.File=${appRootPath}WEB-INF/logs/app_log.log
log4j.appender.fileappender.MaxFileSize=500KB
 
## Keep one backup file
log4j.appender.fileappender.MaxBackupIndex=3
log4j.appender.fileappender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileappender.layout.ConversionPattern=%p %t %c - %m%n
#log4j.appender.C.layout.ConversionPattern=[%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n

Here is my servlet Listener

public class ApplicationServletContextListener implements ServletContextListener{

    public void contextInitialized(ServletContextEvent e) {

        ServletContext ctx=e.getServletContext();
      String prefix=ctx.getRealPath("/");
String file="WEB-INF"+System.getProperty("file.separator")+"classes"+System.getProperty("file.separator")+"log4j.properties";

if(file !=null){
PropertyConfigurator.configure(prefix+file);
    System.out.println("Log4J Logging started for application: " + prefix+file);
}else{
System.out.println("Log4J Is not configured for application Application: " + prefix+file);
}  

And console displays “Log4J Logging started for application…” .

Here is my servlet where I try to test.

private Logger log = Logger.getLogger("myapplogger");
 log.error("this is a testign error");  

The console shows the log.But still there is no any log file created under WEB-INF/logs/ .Please let me know where I have to check and how to correct this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:31:43+00:00Added an answer on June 1, 2026 at 9:31 am

    Its because

    You have the variable ${appRootPath} in the File property for the append.It has not been set so, its empty.

    Since it’s empty, it ends up creating a WEB-INF/logs dir in your home directory, rather than the tomcat webapps/.

    Define the property in contextInitialized()

    System.setProperty("appRootPath", context.getRealPath("/"));
    

    Restart the servlet container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use log4j viewer (Chainsaw) to read error logs logged in a
I want to use several standard third-party libs (e.g. Apache's log4j) for a small
I want to use the log4j logging framework in a jsp file. The typical
I have a java main application, and I want to use log4j. I don't
We use Log4j (and Commons Logging) to log our error messages. Now we want
I have a transaction log file in CSV format that I want use to
Here is my log4j.properties file # Set loggers' levels log4j.rootLogger=warn, trace_file # Appender log4j.appender.trace_file=org.apache.log4j.RollingFileAppender
i am a Java beginner and i want to use log4j , but i
I use log4j for logging and i want to print all logger.debug statements in
In log4j, I need to use Logger.getLogger(MyClass.class); I want to know what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.