File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes ejb and web app. logging with log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ejb and web app. logging with log4j" Watch "ejb and web app. logging with log4j" New topic

ejb and web app. logging with log4j

Yan Zhou
Ranch Hand

Joined: Sep 02, 2003
Posts: 137

Since EJB standards discourage using file i/o, how do people use log4j in EJBs to log to text files? I would like to use text file logging because alternatives such as logging to JMS or DB might be too expensive for loggin. For instance, how to load, where is the logging file physically located, how would that affect my EJBs?

What about J2EE Web app, why the same restriction on EJB does not apply on web app.? Again, if I use log4j in servlet, how do I load, and where is the logging file physically located?

Bruno Collet
Ranch Hand

Joined: Jun 08, 2004
Posts: 40
You should read Log4J tutorial.
A few beginning of answers:
- EJBs cannot directly access the file system because of their distributed nature.
- You do not need to explicitly load the log4j properties file. The log4j component does it for you. In this file you can configure appenders. Obviously you only need a FileAppender (i.e. an ordinary log file).

In case you use JBoss I have a small document showing a basic log4j config for JBoss on my website.

Bruno Collet<br /><a href="" target="_blank" rel="nofollow"></a><br />- The Paradox of Software Architecture: It is easy to make a complex architecture, but it is difficult to make a simple architecture.
Praveena Venigalla
Ranch Hand

Joined: Oct 29, 2003
Posts: 36
As EJB's cannot directly acess the file system:
one solution for logging to text file from EJB is write some Helper(Java) class.
First put the file in one location like C://log4j/
In you have to mention where the log file has to be created.
In helper class:
Logger logger=Logger.getLogger("some string val");
PropertyConfigurator.configure("/log4j/");--this will read the file.
write a method which creats Logger object and return the logger to calling method.
In your EJB call this method and get the logger.
From this logger you can write debug or error or warn statements.
You can use the same class from servlet also.

Let me know if u have any Questions.

[ December 08, 2004: Message edited by: Praveena Venigalla ]

Praveena Venigalla<br />OCP,SCJP,SCWCD,SCBCD<br />--Knowledge is attained by learning to hold one's tongue
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hi Praveena,

PropertyConfigurator.configure("/log4j/");--this will read the file.

Where is the file expected to be placed?

We did keep this file in the classes folder of Weblogic 8.1 Server but we get a message that the Resource file not found. The classpath in Environment variables setting has the name of the folder "classes".


I agree. Here's the link:
subject: ejb and web app. logging with log4j
It's not a secret anymore!