Results 1 to 5 of 5
  1. #1
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default log4j with web services

    Hi everyone,

    I am new to log4j and i was hoping to get some help with setting it up on a unix machine.

    I have deployed my web service to the, tomcat5.5-webapps/axis/WEB-INF/classes directory.

    I have stored the log4jxxx.jar file in a location where my CLASSPATH is pointing.

    My log4j properties file looks like the following,

    Java Code:
    log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
    log4j.appender.rollingFile.File=/usr/share/tomcat5.5-webapps/axis/WEB-INF/classes/com/test/play/perimeter/mylog.log
    log4j.appender.rollingFile.MaxFileSize=2MB
    log4j.appender.rollingFile.MaxBackupIndex=2
    log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
    log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
    Within my class code i have the following,

    Java Code:
    import org.apache.log4j.Logger;
    ...
    
    public class Perimeter_Game implements com.test.play.perimeter.Perimeter{
    ...
    private static Logger logger = Logger.getLogger(Perimeter_Game.class);

    Within one of my methods called from the webapp client,

    Java Code:
    logger.info("this is a sample log message.");

    My main questions are,
    1. What directory does the .properties file need to be in?
    2. Are there any problems with the above?

    Im currently not seeing a logfile created in the directory i specify above.

    Thankyou.
    colpwd
    Last edited by colpwd; 09-06-2010 at 03:36 AM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    1. The log4j.properties file needs to be in the top level package. e.g. inside the axis/WEB-INF/classes within your web application.

    2 & 3. Usually I have created a logging folder that is writable by my tomcat user process, such as /var/log/tomcat , and then set my log4j.conf :
    <pre>
    log4j.appender.rollingFile.File=/var/log/tomcat/mylog.log
    </pre>

    That is, stuffing the log file out of the webapp folder, and certainly out of the classes folder, because log files don't need to be in the class path. Other possible log file folders might include the tomcat "temp" folder ($CATALINA_BASE/tmp)

    To get at log files, I either ssh to the system and use unix tail or less command lines, or for remote access, because usually I also have Apache HTTPD service connected to tomcat with mod_jk, in my Apache virtual host where I have JKMount for my tomcat webapp, I usually create an additional directory tag for the location of the log files
    <pre>
    Alias /logs "/var/log/tomcat"
    <Directory "/var/log/tomcat">
    order deny,allow
    deny from all
    allow from 192.168.1.0/24
    </Directory>
    </pre>

    Where here I have set up the /logs path to point to my tomcat log file output folder, and have restricted it so that only machines inside my local network can access it. Where in your case you might have different access restrictions, or perhaps require HTTP basic authentication to this folder. The general idea is its usually not a good thing to have the log files available for the general internet public to have the ability to consume them.

  3. #3
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    2 & 3. Usually I have created a logging folder that is writable by my tomcat user process, such as /var/log/tomcat , and then set my log4j.conf :
    <pre>
    log4j.appender.rollingFile.File=/var/log/tomcat/mylog.log
    </pre>
    I should still get a .log output at the directory i specified though, shouldn't i?

    I am not getting anything at the moment.

    $CLASSPATH gives me a whole lot of jar files, of which one of them is log4j. So i think my classpath is ok.

    EDIT: My objective is include a log comment for each method of the class. When the web service client accesses a method, the logger will post a message into the logfile. Im not concerned about viewing/accessing the logfile at this point, but rather writing into it.
    Last edited by colpwd; 09-02-2010 at 05:31 AM.

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well yes, if the log4j.properties is in the WEB-INF/classes folder or if it is the top level inside one of your .jar files on the $CLASSPATH, it should be outputting a file. And if its not, this is because your log4j.properties file is not being used or found for some reason.

    Its possible another jar also has a log4j.properties and that one is being found instead of (before) your log4j.properties on the classpath. Usually placing your log4j.properties on the WEB-INF/classes/log4j.properties makes your file found first though.

    Another idea, I think axis might be making use of jakarta commons logging (JCL), as a facade to abstract the logging back end. if so, we might need to also have a "commons-logging.properties" in the same location as log4j.properties, where this file configures the JCL to output to log4j.

    for example, the commons-logging.properties would contain
    <pre>
    # Use Log4j
    org.apache.commons.logging.Log=org.apache.commons. logging.impl.Log4JLogger

    # Configuration file of the log
    log4j.configuration=log4j.properties
    </pre>
    See also: Commons Logging - Overview

    I have had that problem before, where when I didn't have a commons-logging.properties in my classpath that was set to have it's stuff output to a log4j appender, then the log messages from anything that is using JCL seem to go out into space.
    Last edited by travishein; 09-02-2010 at 06:03 AM.

  5. #5
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    I notice that if i copy the .properties file down into WEB-INF/classes/com/test/play/perimeter (where my .class and .java files are), that the following occurs when i deploy my service.

    Java Code:
    log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    Processing file deploy.wsdd
    <Admin>Done processing</Admin>
    EDIT: Also, in regards to the commons-logging API, "Normally this library routes the logging to the Log4j library, provided that an implementation of log4j is on the classpath of the server or client"

Similar Threads

  1. Web Services
    By Cbani in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2010, 02:15 PM
  2. web services
    By k_paramesh82 in forum Java Software
    Replies: 5
    Last Post: 04-02-2009, 12:03 PM
  3. Web Services: Axis vs Metro vs Spring Web Services
    By davidbaker29 in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2009, 11:20 AM
  4. Web Services
    By d_raghu9 in forum New To Java
    Replies: 0
    Last Post: 02-17-2008, 02:12 AM
  5. Web Services
    By d_raghu9 in forum XML
    Replies: 0
    Last Post: 02-17-2008, 02:11 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
  •