Hello Nambiar,
Different application servers does logging in different ways, but for doing
J2EE-logging in a portable way, this is a simple approach:
I give an example of an EJB, but it is easy to do for a servlet too.
In your EJB, define a method that works something like this (There might be
details you want to change, of course there are drawbacks to placing this in
the actual EJB, but I won't worry about that right now):
private void debug (int level, String message)
{
if ( level<= debug.intValue())
{
System.out.println("MyBean> "+debugMessage);
}
}
The level parameter is there to give you the ability to use log-entries with
"priorities". Some log entries could be more detailed and low-level than others
and you might not always want those reported.
Also define an instance variable being a private Integer called debug. This
defines what the current logging level will be (and decides what events will be
written to the log).
In one or more suitable initializer (like ejbCreate() and ejbActivate()) , let
the debug value be set from looking up a value in the InitialContext. That
could look something like this:
debug = (Integer) new InitialContext().lookup("java:comp/env/debug");
To be able to do this lookup you need to define an env-entry in your
ejb-jar.xml, this could look like:
<env-entry>
<description>Logging mode</description>
<env-entry-name>debug</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>2</env-entry-value>
</env-entry>
Now, from your business methods you can do logging:
public void foo(int bar)
{
debug(1, "foo("+bar+")");
debug(2, "some other logging info that is more rarely wanted");
}
The first line will be logged if you are using a debug-level of 1 or higher,
while the second will only be logged if you use a debug-level of 2 or higher.
The best thing about this, is that since the debug level is stored in the jndi
tree, you can easily change the currect debug level from your administration
tools of your J2EE server. You can have some EJBs running using level 1 but
when you start getting trouble, change the level to a higher number to get more
information on what's going on.
Also, it should be easily portable, I can't imagine any application server that
does not provide the ability to log System.out messages to a log file, and I
bet most servers allow you to use application-specific log files, so that you
get one log file per J2EE application.
Of course there could be servers that would not support this.
Regards,
Karl Avedal
The Orion Application Server (http://www.orionserver.com)
> Hi
> Our requirement is to log messages from various components of our
> application (Servlets and EJBs)
> into a file. This is required for debugging purposes if something fails in
> the production environment. But since IO is not possible from an EJB, we
> are looking for alternatives. We do not want to log these messages into a
> database.
> Our requirement does not seem like an uncommon one. I am interested in
> knowing how this is implemented in other applications.
> Any suggestions would be very helpful.
>
> A quick reply would be greatly appreciated.
>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".