Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Simple Log4j config.

  1. #1
    Join Date
    Oct 2005
    Posts
    26

    Default Simple Log4j config.

    Hi all
    I have some trubble getting my log4j config to work. I have a small app that runs quartz on an "older" Oracle oc4j. So i think i need to use servlet's and not a a listener to get it running.

    I just woundered it there were any good links/examples on how to configure this in web.xml etc...?

    -Erik

  2. #2
    Join Date
    Jan 2005
    Location
    Barcelona (Spain)
    Posts
    6

    Default

    If you can't use listeners in your web.xml, you can define your Log4j using Servlets. According to the Javadoc, you must start Log4jConfigServlet before ContextLoaderServlet, so you must put an smaller value for <load-on-startup> for Log4j.
    This could be an example:
    <servlet>
    <servlet-name>log4j</servlet-name>
    <servlet-class>org.springframework.web.util.Log4jConfigServ let</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
    </servlet>

    <!-- If your application is named "web" -->
    <servlet>
    <servlet-name>web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>3</load-on-startup>
    </servlet>


    In order for this to work, you must have defined the context-param log4jConfigLocation:
    <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/web/log4j.properties</param-value>
    </context-param>

    If nothing works, you always can put your log4j.properties at the root of all your classes.

    You can browse Log4jWebConfigurer JavaDoc for more documentation.
    --
    Toni Tassani

  3. #3
    Join Date
    Oct 2005
    Posts
    26

    Default

    Thank you for your replay.

    I have been trying to something like you described.
    Here is my config at the moment:
    web.xml
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    		"http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    
    	<context-param>
    		<param-name>webAppRootKey</param-name>
    		<param-value>modelexport.root</param-value>
    	</context-param>
    
    	<context-param>
    		<param-name>log4jConfigLocation</param-name>
    		<param-value>/WEB-INF/classes/log4j.properties</param-value>
    	</context-param>
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/modelexport-quartz.xml,/WEB-INF/modelexport-service.xml,/WEB-INF/modelexport-data.xml</param-value>
    	</context-param>
    
    	<servlet>
    		<servlet-name>log4j</servlet-name>
    		<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet>
    		<servlet-name>context</servlet-name>
    		<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    		<load-on-startup>2</load-on-startup>
    	</servlet>
    
    	<servlet>
    		<servlet-name>modelexport</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>3</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>modelexport</servlet-name>
    		<url-pattern>*.htm</url-pattern>
    	</servlet-mapping>
    
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    		<welcome-file>index.htm</welcome-file>
    	</welcome-file-list>
      
    </web-app>
    And my log4j.properties (under WEB-INF\classes\):
    Code:
    log4j.rootLogger=INFO, stdout
    log4j.logger.no.toyota=DEBUG, stdout
    log4j.additivity.no.toyota=false
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p [%c] - %m%n
    I still can't see much in the log. This application mostly use Quartz though, but that seem to run well and delivers the things as asked. I just dont see any in the log. If i run the same application from Eclipse it output to the log. System.out.println() also work on my server.. just not Log4j

    -Erik

  4. #4
    Join Date
    Oct 2005
    Posts
    26

    Default

    ...and this is the log output on my server in my application.log
    (Where e.g. System.out.println() were written before)

    Code:
    modelexport: jsp: init 
    modelexport: log4j: init 
    modelexport: Set web app root system property: 'modelexport.root' = [<MY_ROOT>\modelexport\] 
    modelexport: Initializing Log4J from [<MY_ROOT>\modelexport\WEB-INF\classes\log4j.properties] 
    modelexport: context: init 
    modelexport: Loading Spring root WebApplicationContext 
    modelexport: modelexport: init 
    modelexport: Loading WebApplicationContext for Spring FrameworkServlet 'modelexport'
    Last edited by erikgu; Dec 20th, 2005 at 09:41 AM.

  5. #5
    Join Date
    Oct 2005
    Posts
    26

    Default

    Any ideas?
    Is it possible that there are any other settings on the server that can override my settings?

    It's just a little strange coz it is a new container (with nothing else in it) and i use log4j with other projects (without spring) in other containers.

    - Erik

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    If you are using Tomcat (4.x, 5.x) you can check the documentation on their site: http://tomcat.apache.org/tomcat-5.5-doc/logging.html
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  7. #7
    Join Date
    Oct 2005
    Posts
    26

    Default

    Thanks Costin.
    On this project i run on Oracle OC4J.

  8. #8
    Join Date
    Jan 2005
    Location
    Barcelona (Spain)
    Posts
    6

    Default

    The other day we started our application using OC4J and we had problems with log4j, so we used the initialization parameter -Dlog4j.debug=true. This way you can see how log4j is reading its config files. In our case we were having trouble with the classloader, because it was reading other log4j.properties file. Maybe it can help you.

    By the way, we had trouble with the OC4J which comes with JDevelper 10g preview. The problem was with commons-logging.jar, but this does not seems your problem.
    --
    Toni Tassani

  9. #9
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Guys, there is a project called SLF4J created by log4j's author which allows you to migrate from jakarta-commons without changing much code (if any):
    http://www.slf4j.org/manual.html
    Check it out.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  10. #10
    Join Date
    Oct 2005
    Posts
    26

    Default

    Thanks for helpful tips :)

    It seems to work for me now!
    We just did a patch on the oc4j container and it seem to done alot on the logging. I still run the same config as above and i tried both commons-log4j logging (currently in use) and direct log4j / without the use of commons.

    Just currious but what is the big difference of commons-logging and SLF4J? Seems to me to be much the same.

    Anyway, Merry Christmas to you all o<|:)>(|)=|

    -Erik

Posting Permissions

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