• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to get logging working in Tomcat application

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to get log4j working within a Tomcat 6 application. It is a Maven project.

I have included these dependencies in Maven:

slf4j
slf4j-api
log4j

I created an xml configuration file and put it in src/main/resources (log4j.xml)

[code=java]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="file"
class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="100KB" />
<param name="maxBackupIndex" value="5" />
<param name="File" value="test.log" />
<param name="threshold" value="info" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"></priority>
<appender-ref ref="file" />
</root>
</log4j:configuration>
[/code]

In a JSP, I am doing something simple like instantiating a class. Within the class constructor, I am merely logging a simple message. However, the log file never appears. I would expect to see it directly under the root of the application.

[code=java]
import org.apache.log4j.Logger;
public class Person
{
private String lastName;
private String firstname;
private String ssn;

private static Logger logger = Logger.getLogger(Person.class);
public Person(String lastName, String firstname, String ssn, int age)
{
super();
this.lastName = lastName;
this.firstname = firstname;
this.ssn = ssn;
this.age = age;
logger.debug("New Person" + lastName);
System.out.println("New Person " + lastName);
}
}
[/code]

What am I doing wrong?
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found out that the xml configuration just doesn't work for a web application as it does for a regular Java application. Does anyone know why? I was able to get the logging to work when I replaced the xml configuration with a properties file.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
log4j.xml should be placed in WEB-INF/classes. For a WAR-producing POM, the source would go in src/main/webapps/WEB-INF/classes, not in src/main/resources.
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim, however, when the application is deployed, the log4j.xml goes to the WEB-INF/classes folder just the same. Anyway, I'll just use the properties file for web apps.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
src/main/resources is the default classpath resource directory for jar-based products, but src/main/webapp is better for WARs, since it models the entire non-generated subset of the WAR exactly as it will appear in the final product.

I have a goodly number of webapps that all use the WEB-INF/classes/log4j.xml file with no problems. In fact, my understanding is that the log4j.properties file doesn't support rolling logfiles, so I don't really have the option. Although being yelled at by the XML validator because the extensions aren't part of the stock DTD is not appreciated.
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

You're right. I was able to get the .xml configuration working.

Now I am looking to do something more sophisticated, please tell me if I should direct this to a different forum.

I only want to log application-specific messages, as in, only log.info or log.debug messages I have in code to this log file. Developer would look at this one. I would like the Tomcat-specific messages to go to catalina.out. A sysadmin would look at this one.

I would like to log hibernate messages to a competely separate log file. I would like to write out the hibernate sql to its own file. Maybe a DBA would look at this one.

This is what I have now...

[code=xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="file"
class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="100KB" />
<param name="maxBackupIndex" value="5" />
<param name="File" value="c:/development/logs/contactmanager.log" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"></priority>
<appender-ref ref="debugfile" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
[/code]
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you get to that stage, your best resources are the log4j forum.

You need 3 appenders, though. One for basic app logging, which is where you started.

Added to this would be a stdout appender for whatever you consider "tomcat messages" to be. Tomcat itself is using its own logging independent of whatever systems the webapps use.

And, of course, you want an appender for the hibernate messages, which would be a target for the org.hibernate log subtree.
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but I do not see a log4j forum in Java Ranch. Do you mean on the log4j web site?
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found out you can create multiple file appenders in one configuration file. I found a nice tutorial online -- http://ibswings.blogspot.com/2009/03/log4j-configuration-controlling-logging.html -- but for the most part, I figured this out on my own. One of the keys is additivity. By default, one file appender's messages will be appended to another file appender until it gets to root. If you set this to false then you are able to achieve distinct log messages for specific packages, such as within your application, or even hibernate SQL messages.

I'm including the configuration file I arrived at in case it is helpful to someone else:

[code=xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">


<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>
</layout>
</appender>


<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="c:\\development\\logs\\excellor.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>
</layout>
</appender>

<appender name="FILE1" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="c:\\development\\logs\\controller.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>
</layout>
</appender>

<appender name="FILE2" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="c:\\development\\logs\\hibernate.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>
</layout>
</appender>

<appender name="FILE3" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="c:\\development\\logs\\hibernateSQL.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c (%F:%L) - %m%n"/>
</layout>
</appender>


<category name="com.excellor" additivity="false">
<priority value="debug"/>
<appender-ref ref="FILE"/>
</category>

<category name="com.excellor.contactmanager.controller" additivity="false">
<priority value="debug"/>
<appender-ref ref="FILE1"/>
</category>

<category name="org.hibernate" additivity="false">
<priority value="debug"/>
<appender-ref ref="FILE2"/>
</category>

<category name="org.hibernate.SQL" additivity="false">
<priority value="debug"/>
<appender-ref ref="FILE3"/>
</category>


<root>
<priority value="error"/>
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>

[/code]
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One app I work with has 8 appenders. And I'd like to add one more to page me when severe exceptions are logged someday.
reply
    Bookmark Topic Watch Topic
  • New Topic