Results 1 to 3 of 3
  1. #1
    alyoshakaz is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default Log4J will not write to log file.

    This log file specified is always empty. The paht of the file is right. The level of the appender is right. Don't understand what is wrong.

    Java Code:
    
    import java.io.IOException;
    import javax.servlet.http.HttpServlet;
    import org.apache.log4j.Level;
    import org.apache.log4j.Logger;
    import org.apache.log4j.PatternLayout;
    import org.apache.log4j.RollingFileAppender;
    
    public class LoggerServlet extends HttpServlet {
         
        @Override
        public void init() {
            try {
                RollingFileAppender appender = 
                        new RollingFileAppender(new PatternLayout("%m")
                        , getServletContext().getRealPath("/WEB-INF/logs/error.log") , true);
                appender.setMaxBackupIndex(5);
                appender.setMaxFileSize("1MB");
                Logger logger = Logger.getRootLogger();
                logger.setLevel(Level.ERROR);
                logger.addAppender(appender);
                logger.info("Starting " + logger.getName());
                for(int i = 0; i < 100; i++)
                    logger.error("this is a message error");     
                System.out.println("Logger Server init()");
                System.out.println(getServletContext().getRealPath("/WEB-INF/logs/error.log"));
                System.out.println(appender.getFile() + " append? " + appender.getAppend());
                System.out.println(logger.getName() + " " + logger.getLevel());
            }catch(IOException ex){
                System.out.println("error");
            }
        }
      
        
    }

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Post

    Code is perfectly fine.

    Probably there is some error here:
    getServletContext().getRealPath("/WEB-INF/logs/error.log")
    Change this line to some hard-coded filename and then see the result.
    It works, so look how to resolve this error. Log4j is working fine

  3. #3
    alyoshakaz is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks, that helped me figure out the problem. It seems that the log file is only written under the /build directory and the changes are not reflected in the projects view, which is annoying.

Similar Threads

  1. Adding a .jar file (log4j)
    By SeanC in forum New To Java
    Replies: 10
    Last Post: 01-13-2011, 12:04 PM
  2. Replies: 0
    Last Post: 07-15-2010, 02:23 PM
  3. Replies: 1
    Last Post: 03-31-2010, 09:04 PM
  4. log4j file location
    By Netzilla in forum New To Java
    Replies: 0
    Last Post: 03-17-2010, 04:00 PM
  5. Log4j - setting up name of log file
    By Virkant Pawar in forum New To Java
    Replies: 3
    Last Post: 06-03-2008, 08:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •