0

I read a lot of posts and tried a lot of approaches but still I am unable to log my code flow. The reason could be that I got confused due to various opinions.

My WebContent/conf/log4j.properties

# Direct log messages to a log file
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=D:/mylog.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.Threshold=debug
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
log4j.rootLogger = INFO, rollingFile

My /WEB-INF/web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringSort</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  <servlet>
        <servlet-name>sort</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sort</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

A part of my controller Controller.java

private static final Logger logger = LoggerFactory.getLogger(Controller.class);
System.out.println(logger);
System.out.println(logger.isDebugEnabled());
logger.info("Logger Initiliazed");

The System.out.println(logger); gives a reference address and the
System.out.println(logger.isDebugEnabled()); gives true. But no log file is being created.

Can anyone please help me out of this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Freakyuser
  • 2,774
  • 17
  • 45
  • 72
  • What is the package name of LoggerFactory? Is it org.apache.log4j.spi? Also please check that at least `"Welcome home!"` is being logged – Boris Treukhov Dec 12 '12 at 19:18
  • check out log4j samples at http://stackoverflow.com/questions/3387441/how-do-i-configure-spring-and-slf4j-so-that-i-can-get-logging/ - if you are using slf4j I think you must disable commons-logging 1) add an exclusion for `commons-logging` 2) add a bridge to slf4j from commons-logging(which is a drop in replacement for commons logging) i.e. `jcl-over-slf4j` 3) slf4j itself `slf4j-api` 4) bridge from slf4j to log4j `slf4j-log4j12` 5)and finally have log4j itself `log4j` – Boris Treukhov Dec 12 '12 at 19:28
  • Spring framework uses commons logging(and the authors now regret about that), please see the following link, to see how to redirect the output to log4j http://www.slf4j.org/legacy.html – Boris Treukhov Dec 12 '12 at 20:06
  • Also I'm not sure that you can write to the root directory of the system hard drive in Windows. Also what is your log4j configuration listener declaration(maybe you should add your web.xml). – Boris Treukhov Dec 13 '12 at 09:17
  • @BorisTreukhov I have already added the web.xml part you have asked for. Do you want more of it. Please mention! I tried D drive too, its not working. Please let me know, if you want me to post anything else. – Freakyuser Dec 13 '12 at 09:29
  • the content of your log4j.properties is correct(I actually tested it on Spring MVC template object), please check that there no other log4j configuration files. Also as your `log4j.properties` is in the class path it makes no sense to use a Log4jConfigListener(btw you didn't show `` declaration in the web.xml) at all. Please check that there is no `log4j.xml` in the classpath, because it has higher priority than `log4j.properties`. Also if you want to put a file into the class path you can add it the root of any source folder(`src/main/java` or `src/main/resources`). – Boris Treukhov Dec 13 '12 at 11:04
  • What is the full content of log4j.xml? and web.xml? – Boris Treukhov Dec 13 '12 at 11:48
  • @BorisTreukhov added the log4j.xml and web.xml as asked. – Freakyuser Dec 13 '12 at 11:57
  • You have not decalred the log4j Log4jConfigListener in your web.xml http://stackoverflow.com/questions/4400583/initializing-log4j-with-spring try declaring it before ContextLoaderListener – Boris Treukhov Dec 13 '12 at 12:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21082/discussion-between-freakyuser-and-boris-treukhov) – Freakyuser Dec 13 '12 at 13:17

1 Answers1

0

Depencies mentioned in the tutorial are for Maven (so if you don't use maven just be sure to have log4j jars in your buildpath).

Another simple tutorial to add log4j to your project and then on how to configure it.

Benoit Wickramarachi
  • 6,096
  • 5
  • 36
  • 46