You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by A Df <ab...@yahoo.com> on 2011/08/03 12:17:08 UTC

Re: Missing files for Apache and Log4J

Hello Mates:

I got the log file to work however, it does not have the date appended like the usual Tomcat Logs and does not creating a rolling daily file. I also wanted to have control over the usual logs that Tomcat produced so I combined your instructions with those from this other website at http://mrhaki.blogspot.com/2011/02/configure-log4j-on-tomcat.html to get the content for the properties file. However, the problems are:
	* the log files do not have the .log extension so they do not show up file type as text document instead just a type as file

	* the date is not being appended to the name
My code below is in the properties file in the Project default package folder as Mark Eggers suggested. I am not sure which property to modify to get the .log extension and the date appended.



#log4j.properties File

log4j.rootLogger=INFO, CATALINA, CONSOLE

#Tweet Tag Processor
log4j.logger.org.tweettagprocessorpackage=INFO, R

# Define all the appenders

# Configuration for a rolling log file.
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
# Edit the next line to point to your logs directory.
# The last part of the name is the log file name.
log4j.appender.R.file=${catalina.base}/logs/tagprocessor.
log4j.appender.R.encoding=UTF-8
# Roll-over the log once per day
log4j.appender.R.DatePattern='.'yyyy-MM-dd'.log'
# Print the date in ISO 8601 format
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R.append=true

log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.file=${catalina.base}/logs/catalina-my.
log4j.appender.CATALINA.encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.CATALINA.append=true

log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.file=${catalina.base}/logs/localhost-my.
log4j.appender.LOCALHOST.encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOSTlayout=org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.LOCALHOST.append=true

log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.file=${catalina.base}/logs/manager-my.
log4j.appender.MANAGER.encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.MANAGER.append=true

log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.file=${catalina.base}/logs/host-manager-my.
log4j.appender.HOST-MANAGER.encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.HOST-MANAGER.append=true

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c - %m%n

# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
  INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
  INFO, HOST-MANAGER

  



Cheers,
A Df

From: Mark Eggers <it...@yahoo.com>

To: Tomcat Users List <us...@tomcat.apache.org>; A Df <ab...@yahoo.com>
>Sent: Friday, 29 July 2011, 19:04
>Subject: Re: Missing files for Apache and Log4J
>
>> From: A Df <ab...@yahoo.com>
>
>> Dear All:
>> 
>> I have created a Java web application and I want to have logging to ensure that 
>> the appropriate messages are stored in log files instead of to standard output. 
>> I found a link at http://tomcat.apache.org/tomcat-6.0-doc/logging.html and was 
>> following the instruction but I am unclear for these steps. My details are:
>> 
>>     * Apache Tomcat 6.0.26
>>     * Log 4j 1.2.16
>> 
>> The instructions specify
>> 
>>     * Build or download the additional logging components.
>> I am new to logging, so I am not sure what other extra are needed!
>> 
>>     * Replace $CATALINA_HOME/bin/tomcat-juli.jar with 
>> output/extras/tomcat-juli.jar.
>> Does this mean that I move the jar from that location and create the folders to 
>> put it in another location to have :C:\Program Files\Apache Software 
>> Foundation\Apache Tomcat 6.0.26\output\extras?
>> 
>> 
>>     * Place output/extras/tomcat-juli-adapters.jar in $CATALINA_HOME/lib.I 
>> cannot find the jar file in the Log4J download or Apache files so where is it 
>> located?
>> 
>> Is Log4J the best logging to use with a Java web app? Do you have any 
>> suggestions?
>> 
>> Thanks for your help,
>> A Df
>>
>
>Logging for a particular web application is different (or can be) than
>logging for Tomcat.
>
>What I normally do is leave Tomcat logging as is (or modify it for
>better cluster logging), and use logging-commons / log4j for web
>applications.
>
>Using logging-commons / log4j for a particular web application is
>pretty straightforward.
>
>1. Include commons-logging-1.1.1.jar and log4j-1.2.15.jar in your
>   application's WEB-INF/lib folder
>
>2. Create a logging.properties file or a log4j.xml file and place it
>   in the application's WEB-INF/class folder
>
>3. Add logging instructions to your classes
>
>By default logging-commons will use log4j, so no other special
>configuration is necessary.
>
>It's nice to use logging-commons, since this makes it easier to switch
>out logging from log4j to another package should you choose to.
>
>Since I know you use NetBeans, the following is NetBeans-specific:
>
>* Add the jars
>
>There are several ways to include third party jars in your
>project. The quick and dirty way is to do the following.
>
>1. Create a folder (call it libs) in your project
>2. Copy the jars into that folder
>3. Right-mouse click on the Libraries node in your project
>4. Select Add Jar/Folder
>5. Browse to where you copied the jar files
>6. Add them
>
>By default, those jars will now be packaged up in the WAR file and
>included in your application's WEB-INF/lib folder.
>
>For individual development this is probably OK. For a more robust
>environment, it might be nice to add the jars to version control, create a
>NetBeans library with the two jars, or even use Maven or Ivy to manage
>dependencies.
>
>* Create a properties file
>
>I actually prefer using the xml file, but there are several open
>issues with using custom DTDs or schemas when editing XML files with
>NetBeans. So, a properties file is easier to use.
>
>1. Navigate to your Source Packages -> <default package> node
>2. Right-mouse click and select New -> Other
>3. Find Properties File in the dialog box (it's under Other)
>4. Call it log4j (NetBeans adds the .properties)
>5. Edit away
>
>NetBeans will package up the file in WEB-INF/classes of your
>application, where it can be found by the log4j classes.
>
>Here's a quick example of a log4j.properties file:
>
>### direct messages to file simple.log ###
>log4j.appender.file=org.apache.log4j.FileAppender
>log4j.appender.file.File=${catalina.base}/logs/simple.log
>log4j.appender.file.layout=org.apache.log4j.PatternLayout
>log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
>
>### default logging level
>log4j.rootLogger=warn,file
>
>### for the simple application
>log4j.logger.simple.controller=info,file
>
>Read the documentation that comes with log4j on how to create
>log4j.properties files for more information. For example, change the
>log4j.logger definitions to fit your package names and desired level
>of logging.
>
>One thing of interest to note here is where the log gets written. I've
>used ${catalina.base}/logs as the directory for the log file. This
>will write simple.log in the same logging directory that Tomcat
>uses. This may or may not be what you want. Change the location as
>appropriate.
>
>
>. . . . just my two cents.
>/mde/
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>
>

Re: Missing files for Apache and Log4J

Posted by Mark Eggers <it...@yahoo.com>.
----- Original Message -----

> From: A Df <ab...@yahoo.com>
> To: Tomcat Users List <us...@tomcat.apache.org>; Mark Eggers <it...@yahoo.com>
> Cc: 
> Sent: Wednesday, August 3, 2011 3:17 AM
> Subject: Re: Missing files for Apache and Log4J
> 
> Hello Mates:
> 
> I got the log file to work however, it does not have the date appended like the 
> usual Tomcat Logs and does not creating a rolling daily file. I also wanted to 
> have control over the usual logs that Tomcat produced so I combined your 
> instructions with those from this other website at 
> http://mrhaki.blogspot.com/2011/02/configure-log4j-on-tomcat.html to get the 
> content for the properties file. However, the problems are:
>     * the log files do not have the .log extension so they do not show up file 
> type as text document instead just a type as file
> 
>     * the date is not being appended to the name
> My code below is in the properties file in the Project default package folder as 
> Mark Eggers suggested. I am not sure which property to modify to get the .log 
> extension and the date appended.
> 
> 
> 
> #log4j.properties File
> 
> log4j.rootLogger=INFO, CATALINA, CONSOLE
> 
> #Tweet Tag Processor
> log4j.logger.org.tweettagprocessorpackage=INFO, R
> 
> # Define all the appenders
> 
> # Configuration for a rolling log file.
> log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
> # Edit the next line to point to your logs directory.
> # The last part of the name is the log file name.
> log4j.appender.R.file=${catalina.base}/logs/tagprocessor.
> log4j.appender.R.encoding=UTF-8
> # Roll-over the log once per day
> log4j.appender.R.DatePattern='.'yyyy-MM-dd'.log'
> # Print the date in ISO 8601 format
> log4j.appender.R.layout=org.apache.log4j.PatternLayout
> log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
> log4j.appender.R.append=true
> 
> log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
> log4j.appender.CATALINA.file=${catalina.base}/logs/catalina-my.
> log4j.appender.CATALINA.encoding=UTF-8
> # Roll-over the log once per day
> log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
> log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
> log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
> log4j.appender.CATALINA.append=true
> 
> log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
> log4j.appender.LOCALHOST.file=${catalina.base}/logs/localhost-my.
> log4j.appender.LOCALHOST.encoding=UTF-8
> log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
> log4j.appender.LOCALHOSTlayout=org.apache.log4j.PatternLayout
> log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
> log4j.appender.LOCALHOST.append=true
> 
> log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
> log4j.appender.MANAGER.file=${catalina.base}/logs/manager-my.
> log4j.appender.MANAGER.encoding=UTF-8
> log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
> log4j.appender.MANAGER.layout=org.apache.log4j.PatternLayout
> log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
> log4j.appender.MANAGER.append=true
> 
> log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
> log4j.appender.HOST-MANAGER.file=${catalina.base}/logs/host-manager-my.
> log4j.appender.HOST-MANAGER.encoding=UTF-8
> log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
> log4j.appender.HOST-MANAGER.layout=org.apache.log4j.PatternLayout
> log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
> log4j.appender.HOST-MANAGER.append=true
> 
> log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
> log4j.appender.CONSOLE.encoding=UTF-8
> log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
> log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c - %m%n
> 
> # Configure which loggers log to which appenders
> log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, 
> LOCALHOST
> log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
>   INFO, MANAGER
> log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
>   INFO, HOST-MANAGER


. . . . lots of stuff deleted . . . .

In general:

Do not mix application logging and Tomcat logging in the same
properties file. Follow Chris's excellent comments and the
instructions at:

http://tomcat.apache.org/tomcat-6.0-doc/logging.html

to enable Tomcat to use log4j. Unfortunately I didn't keep Chris's mail message, but you should be able to find it in the archives.

Configure Tomcat logging in $CATALINA_BASE/conf/logging.properties and
your application logging in
$CATALINA_BASE/webapps/AppName/WEB-INF/classes/log4j.properties

On file naming:

From the javadoc for DailyRollingFileAppender:

For example, if the File option is set to /foo/bar.log and the
DatePattern set to '.'yyyy-MM-dd, on 2001-02-16 at midnight, the
logging file /foo/bar.log will be copied to /foo/bar.log.2001-02-16
and logging for 2001-02-17 will continue in /foo/bar.log until it
rolls over the next day.

It doesn't appear that you will get the naming you desire until the
log is copied over.

On utility:

Also from the Javadoc for DailyRollingFileAppender:

DailyRollingFileAppender has been observed to exhibit synchronization
issues and data loss. The log4j extras companion includes alternatives
which should be considered for new deployments and which are discussed
in the documentation for org.apache.log4j.rolling.RollingFileAppender.

So, I'm thinking that you probably do not want to use the
DailyRollingFileAppender in any case.

Solutions:

If you want your log files to look like this:

bar.{current-date}.log
bar.{yesterday}.log

then it appears that you'll want to use RollingFileAppender and an
appropriate policy from the log4j extras jar file.

To do that for your web application, add apache-log4j-extras-1.1.jar
to your project. Then you'll have to configure the RollingFileAppender
and the required rollingPolicy in order to get the desired results.

According to the log4j Wiki, RollingFileAppender and the required
rollingPolicy can only be configured using an xml file
(log4j.xml). Here's an application example:

<!DOCTYPE log4j:configuration PUBLIC
 'PUBLIC:-//log4j/log4j Configuration//EN' 'log4j.dtd'>
<log4j:configuration>
    <appender name="SIMPLE"
     class="org.apache.log4j.rolling.RollingFileAppender">
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern"
             value="${catalina.base}/logs/simple.%d.log"/>
        </rollingPolicy>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/>
        </layout>
    </appender>
    <logger name="simple">
        <level value="INFO"/>
        <appender-ref ref="SIMPLE"/>
    </logger>
</log4j:configuration>

This will log all classes in the package "simple" and below to
$CATALINA_BASE/logs/simple.{date}.log.

The file will be rotated nightly around midnight (according to the javadoc).

You could try this type of configuration for Tomcat logging by placing
apache-log4j-extras-1.1.jar in $CATALINA_HOME/lib. Then configure the
appropriate loggers and appenders in $CATALINA_HOME/conf/log4j.xml.

Unfortunately, according to the Tomcat documentation you may have
trouble with a logger name that contains square brackets. In other
words:

<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]">
  <level value="INFO"/>
  <appender-ref ref="LOCALHOST"/>
</logger>

may not be read properly by log4j.

So I think you have some choices. In all cases, keep the logging
configuration for Tomcat separate from your application.

1. Keep Tomcat logging via JULI (the default) and use log4j in your
   application

You can use whatever application configuration you desire. If you do
want to rotate logs, the javadoc for log4j suggests that you use the
extras package.

2. Convert Tomcat to log4j following the Tomcat documentation and
   Chris's excellent comments.

You will have to live with fact that the active logging files will not
have a date in the name. Also, you'll need to quote the DatePattern in
log4j.properties if you want your rotated file name to look like
localhost.{date}.log. The Tomcat documentation has an example of this
at:

http://tomcat.apache.org/tomcat-6.0-doc/logging.html

Again, you can configure your application to log however you want.

3. Try using the log4j extras package for Tomcat logging

This will give you the file format that you want. It also has some
nice properties like file compression on rotation. Unfortunately, you
must use log4j.xml when using the extras package. This may present
problems when trying to specify Tomcat logging names such as
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].

Again, this is (should be) completely separate from your application
logging.

. . . just my two cents.
/mde/


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org