'log4j not creating log files

Below is the log4j.properties file which i have used in a project to generate logs. But the logs are not getting generated at the mentioned location.

log4j.rootLogger=DEBUG,file
#log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.org.apache.ibatis.MaxFileSize=15MB 
log4j.appender.org.apache.ibatis.MaxBackupIndex=10
log4j.appender.file.File=E:\logs\file.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p] %d %c %M - %m%n

So I followed it up with writing a sample jsp as below and found that only ConsoleAppender class is returned by out.println(app.getClass()); and this happens when I deploy the code in TOMCAT on windows server 2012. If the same code is deployed on windows server 2008 on TOMCAT, logs are generating properly as well as proper appender mentioned in properties file is returned. I have tried placing the log location inside and outside the Tomcat folder but still no luck. log4j version which we are using is 1.2.17.

<%@page import="org.apache.log4j.*"%>
<%@page import="java.io.*"%>
<%@page import="java.lang.*"%>
<%@page import="java.util.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%! static Logger logger = Logger.getLogger("sample_jsp");
 %>
        <%
          Enumeration e = Logger.getRootLogger().getAllAppenders();
    while ( e.hasMoreElements() ){
      Appender app = (Appender)e.nextElement();
      out.println(app.getClass());
     if ( app instanceof ConsoleAppender ){
    //out.println("File: " + ((ConsoleAppender)app).getFile());
  }
  }
        %>
    </body>
</html>

What could be the reason for the abnormal behavior? Please help



Solution 1:[1]

log4j.properties

# Root logger option
log4j.rootLogger=INFO,FILE,stdout
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=automatio.out
log4j.appender.FILE.Append=true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p 
%c{1}:%L - %m%n
log4j.appender.file.MaxFileSize=48
log4j.appender.file.MaxBackupIndex=9

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p 
%c{1}:%L - %m%n

In Baseclass.java

    public void init() throws IOException 
    {
      String log4jConfPath="log4j.properties";
      PropertyConfigurator.configure(log4jConfPath);
    }

   public void log(String data) 
   {
      log.info(data);
      Reporter.log(data);   
   }

Solution 2:[2]

Check logs of app start up.

In my case project was deployed as WAR file to tomcat, project had 2 SLF4J implementations, "log4j" and "logback". On application deployment catalina.out had this message "SLF4J: Class path contains multiple SLF4J bindings....". Removed logback by dependency exclusion in pom and after that file was created and populated by logs.

See this link https://www.baeldung.com/slf4j-classpath-multiple-bindings

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Guram Kankava