views:

648

answers:

6

My web application is using Java, Hibernate's JPA implementation (EntityManager) and Spring. What are my logger choices and what would you recommend. Ideally the configuration would be simple (one config file for Hibernate, Spring and my code).

+7  A: 

log4j is the standard logger that I use. You can configure logging levels and log files by package so Hibernate, Spring and your code can each go to the own log files.

Mike Pone
+3  A: 

I use the Apache Commons Logging api, with a log4j config file (the log4j config file is simple, easy for a novice to understand and has numerous examples to work from.

I prefer using the commons logging API because its agnostic about its configuration file. It can be configured via file formats for any number of popular logging packages.

Here's a simple log4j setup that will print out debug info for your own packages, INFO level for spring and hibernate, and WARN for everything else:

# Global logging configuration
log4j.rootLogger=WARN, stdout
log4j.category.org.mydomain.org=DEBUG
log4j.category.org.hibernate=INFO
log4j.category.org.springframework=INFO


# CONSOLE appender not used by default
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] %c - %m%n
Jherico
+11  A: 

When building applications out of various frameworks you often need to wrap existing loggers, be they log4j or JUL or JULI or whatever. The standard application for this is the Simple Logging Facade for Java (SLF4J). Even if your app doesn't need SLF4J now you may later want to add a library that does need it, and it's your best hope for one config file in the long run.

With that in mind you should stick to logging packages that SLF4J supports, but usually it boils down to java.util.logging (JUL) or Apache's log4j. This has been a subject for debate for some time and you can find many opinions on both sides.

Personally, I think it's unfortunate that Java logging is in this situation, and I'll use whatever gives me the least pain for the situation.

Glenn
+5  A: 

Nobody ever got fired for choosing log4j.

Also check out slf4j.

Licky Lindsay
+3  A: 

Hibernate already relies on SLF4J. Unless I am mistaken, the current version of Spring relies on commons-logging but the next version will be using SLF4J as well. Anyway, you could consolidate all logging through SLF4J (see the section on jcl-over-slf4j.jar). Once that is done, you could choose any of java.util.logging, log4j or logback as your underlying logging framework.

Ceki
A: 

Generally speaking, Commons Logging is unnecessary and a bad choice for web applications (and standalone applications). Some good writing on why:

Think again before adopting the commons-logging API

Commons Logging was my fault

Matt