Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 24th, 2007, 03:03 PM
Registered User
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sonika
Default configure log4j for j2ee and web application

hi, does anyone knows how to configure log4j for one j2ee and one web application on the same server?
the problem that i have is that the logs for the web app appear on the appender of the j2ee app, i think that the web app doesn't found the right appender...???

Old July 5th, 2007, 12:47 AM
Registered User
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Before logging is enabled, you have to configure log4j first. Configuring Log4j means adding appenders (destinations/targets/handlers) to categories (loggers) and assigning a layout (formatter) to each appender.
Categories can be created and configured in any order. In particular, a category will find and link to its descendants even if it is instantiated after them.
You will usually configure Log4j just once which is during application initialization, usually by reading a configuration file.
You can also configure a category programmatically to log to a particular appender using a category object's addAppender() method. Add as many as you want (or none if you don't). This method is usually not recommended as it involves changing the source code and recompiling it. The better way is to use external configuration files such as a Java properties file or an XML file.
Beware that appenders are not singletons, they are additive! A category inherits all the appenders from its ancestors also (by default). If you add an appender to a category and it writes to the same underlying stream (console, same file etc.) as some other appender, the same log message will appear twice (or more) in the log. In addition, if two categories in a hierarchy are configured to use the same appender name, Log4j will write twice to that appender. Use cat.setAdditivity(false) on a category to disable inheriting of appenders. Then, log messages will only be sent to the appenders specifically configured for that category.
Calling the static BasicConfigurator.configure() method configures log4j to log to console just like a system.out.println(...) statement would. This method is hardwired to add to the root category a ConsoleAppender printing on the console. The output will be formatted using a PatternLayout set to the pattern %-4r [%t] %-5p %c %x - %m%n. This is usually not needed as using log4j.properties file is simpler and more flexible.
The default file which configures log4j is log4j.properties. Place it anywhere in the application classpath.
Use one of the static PropertyConfigurator.configure(...) methods to read in the configuration information from a Java properties file. The existing configuration is neither cleared nor reset. To do so, call the static BasicConfigurator.resetConfiguration() method before calling this method. This has the potential to make long-term code management more complicated. For starters, stick with one log4j.properties in each application's root directory.
A sample log4j configuration file:
# Set root category priority to DEBUG and set its only appender to A1
log4j.rootCategory=DEBUG, A1

# A1 is set to be a ConsoleAppender (writes to system console).

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternL ayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%nThis sample file configures log4j in exactly the same way as the BasicConfigurator.configure() method. But this way, you can later change, for example, DEBUG to FATAL in the properties file and not have to recompile any Java code!

Another more complex log4j configuration file:
#### Use two appenders, one to log to console, another to log to a file
log4j.rootCategory=debug, stdout, R

# Print only messages of priority WARN or higher for your category
# Specifically inherit the priority level

#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
log4j.appender.stdout.layout=org.apache.log4j.Patt ernLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

#### Second appender writes to a file
log4j.appender.R=org.apache.log4j.RollingFileAppen der

# Control the maximum log file size
# Archive log files (one backup file here)

log4j.appender.R.layout=org.apache.log4j.PatternLa yout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%nHere, output will also be appended to the "example.log" file. This file will be rolled over when it reaches 100KB. When roll-over occurs, the old version of example.log is automatically moved to example.log.1. Also available is DailyRollingFileAppender to roll a log file every minute, hour, day, week, month or twice-a-day.

For long-lived programs such as servers, use configureAndWatch(String configFilename, long delay_in_milliseconds) to configure using a Java properties file. Log4j will keep monitoring the property file for any changes every so many milliseconds, and if the file changes, Log4j will update the configuration.
When moving code to production, for speedy performance, you can disable logging wholesale equal to or below a certain priority for all categories in a hierarchy. For example, To log nothing at all ever, specify log.disable=FATAL in the configuration properties file.
Only exception is if log.disableOverride property is not set to false. So, a system administrator can (temporarily) specify log.disableOverride=true in the configuration file to override log.disable property and log all messages to troubleshoot something.

refer to following link for details:http://www.vipan.com/htdocs/log4jhelp.html

Similar Threads
Thread Thread Starter Forum Replies Last Post
deploy j2ee application in jboss m_hassan_aly BOOK: Professional Apache Tomcat 1 March 22nd, 2012 09:44 PM
configure web.config (db details) when deploy app? benilpj ASP.NET 2.0 Basics 3 June 7th, 2006 09:27 PM
Configure Windows 2000 as a Web Server pans HTML Code Clinic 3 December 7th, 2004 11:39 PM
Designing an J2EE Application arvindwrites J2EE 0 March 16th, 2004 04:40 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.