Logging Strategy/best practice

L

Lord0

Hi there,

I am working on a startup J2EE project: JBoss, Spring, Hibernate etc.
One of my responsibilities is to set up the "logging".

I have chosen to use log4j, or more precisely was nudged in that
direction. I've used it before on smaller projects with no problems;
basically a log4j.properties file defining various categories,
appenders and layouts. This approach seems a bit shallow for a larger
project i.e. 100s+ of classes - maybe I'm wrong.....

Questions:

1. Is log4j a suitable solution for this environment?
2. Any URLs for logging best practices/patterns. Here I am thinking
about something more involved than a "Log4j 101" article.
3. Anything else I should consider?

Cheers for yer time

Lord0
 
I

iksrazal

Just a quick tip - Log4J works differently in JBoss then elsewhere.
Typically you put log4.xml/properties in the root of your classpath, or
alternatively load it via a servlet with loadOnStartup. JBoss ignores
that practice - for better or worse.

To control logging with JBoss 4.0.1, I edit:

jboss/server/default/conf/log4j.xml

And have:

<appender name="CALLCENTREWEB_FILE"
class="org.apache.log4j.FileAppender">
<param name="File"
value="${jboss.server.home.dir}/log/call_centre_web.log" />
<param name="Append" value="true" />

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyy HH:mm:ss},
%-6p %C:%M:%L %m %n" />
</layout>
</appender>

<category name="com.hostedtelecom.callcentreweb">
<priority value="DEBUG" />
<appender-ref ref="CALLCENTREWEB_FILE" />
</category>

Hibernate and Spring have there own appenders. Such as (Not tested in
Jboss, just tomcat, but idea is the same):

log4j.category.org.springframework=WARN, console2, apacheOrg

log4j.category.net.sf.hibernate=WARN, console2, apacheOrg

You'll need to convert those to the xml format JBoss uses. Search the
JBoss group for details or post a question there.

HTH,
iksrazal
http://www.braziloutsource.com/
 
R

Raymond DeCampo

Lord0 said:
Hi there,

I am working on a startup J2EE project: JBoss, Spring, Hibernate etc.
One of my responsibilities is to set up the "logging".

I have chosen to use log4j, or more precisely was nudged in that
direction. I've used it before on smaller projects with no problems;
basically a log4j.properties file defining various categories,
appenders and layouts. This approach seems a bit shallow for a larger
project i.e. 100s+ of classes - maybe I'm wrong.....

That depends on how your loggers are used.

Keep in mind that you can configure loggers at the "package" level.
That is, you can set the logging for com.xyz.abc.def to INFO and that
applies to all loggers of the form com.xyz.abc.def.*.
Questions:

1. Is log4j a suitable solution for this environment?

Of course.
2. Any URLs for logging best practices/patterns. Here I am thinking
about something more involved than a "Log4j 101" article.

http://logging.apache.org

The founder of log4j has written a book that serves as a manual. You
should be able to find it from the Apache site.

You may also want to join the log4j-users mailing list, also available
from the Apache site.
3. Anything else I should consider?

Use the XML format for the properties file, not the .properties format.
Cheers for yer time

Lord0

HTH,
Ray
 
A

A_Wieminer

3. Anything else I should consider?
Use the XML format for the properties file, not the .properties format.

FYI, Tomcat5.5.x uses a new JULI (java.util.logging.interface) logging
for web applications. It can use JDKLog, Log4j or other implementations.

I have read from tomcat site, that if you use Log4j as a logging
implementation you _must_ use .properties format. Its something to do
with how webapp-level logging is configured and xml format
incompatibilities. It is supposed to be fixed in later Log4j versions.
 
J

jeak110

I use log4j on Sun Java System Application Server ( jsas)in web
services. It writes log into jsas log file.

static Logger logger;
static {
try{
URL url = new
URL("http://localhost:8080/log4j.properties");
PropertyConfigurator.configure(url);
logger =
Logger.getLogger(WSExcelQueryImpl.class.getPackage().getName());
}catch(java.net.MalformedURLException e){
}
}

/\
|| code in a web service for log4j.
 
R

Raymond DeCampo

A_Wieminer said:
FYI, Tomcat5.5.x uses a new JULI (java.util.logging.interface) logging
for web applications. It can use JDKLog, Log4j or other implementations.

I have read from tomcat site, that if you use Log4j as a logging
implementation you _must_ use .properties format. Its something to do
with how webapp-level logging is configured and xml format
incompatibilities. It is supposed to be fixed in later Log4j versions.

I have checked with the log4j developers and the conclusion was that
there is no issue with using the XML configuration format with tomcat.
Also, one of them noted that the tomcat documentation indicates as much
(http://jakarta.apache.org/tomcat/tomcat-5.5-doc/logging.html).
Finally, one of them actually tested it out and it works.

The only caveat is that you will get warning messages if you configure
loggers with square braces in the name, e.g.
org.apache.catalina.core.ContainerBase.[Catalina].[localhost]. However,
the functionality is not affected. The warnings are an artifact of the
log4j DTD and the data type for id.

If you are interested in more details, the responses from the log4-dev
list should be available at your favorite mail archive.

Ray
 
C

chris.ebert

Hi -- I'm using Log4J 1.2.9 with Tomcat 5.5.9 with XML format and it
seems OK. I haven't tried turning on the 'tomcat' logging much, but the
log statements from my application work just fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top