• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about slf4j  RSS feed

 
Von South
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking at implementing logging for my application and I just want to know if it is safe to do the following: I want to configure tomcat and hibernate to directly use log4j and my application to use slf4j which uses log4j as the backend. I want to use slf4j because then I don't have to do the if(level == debug) { logger.debug() } thingy. I've attached an image to explain exactly what I want to achieve. So basically what I want to know is, if I use slf4j does all the software components have to use slf4j, or can they still talk directly to log4j?

slf4j.jpg
[Thumbnail for slf4j.jpg]
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Full disclosure: I have not used slf4j, but from what I can see, slf4j can use log4j as it's logger. slf4j is just a facade that has no logging capabilities of its own. So yes, I think it's safe to use slf4j in your app with other libraries using log4j.
 
Tim Holloway
Bartender
Posts: 18720
72
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that slf4j was ever intended to be used directly as a logger. (does quick web search.) OK, so it does. I've never used it that way, though. I've always used a traditional logger.

What slf4j does for me, mostly, is reconcile the fact that my projects pull in lots of libraries from lots of sources and not all of them used the same loggers. So slf4j normalizes things so that I can use, for example, log4j as the backend regardless of what logger they use.

Your diagram is incorrect, by the way. Log4j is not a central logging component like the Windows Event Manager or Unix syslogd. It's a library that's included with the product doing the logging. Also, Tomcat's logger only logs Tomcat (and its stdout/stderr), not web applications. Each web application is responsible for its own logging and the closest thing to a central webapp logger there is is the servlet log function which is actually just a pass-through to the Tomcat stdout. So that big log4j block at the bottom of your diagram is incorrect. If you want Tomcat to use log4j, you have to build a custom Tomcat that uses log4j instead of juli, which is the default. That log4j will be completely independent of the log4j's (if any) in the webapps - which is why, incidentally, that a webapp that uses log4j has to include the log4j implementation jar in its WEB-INF/lib directory.

Further, Hibernate is also not an independent facility, so if you want a Tomcat webapp to use Hibernate, you have to include the Hibernate jar(s) in the webapp's WEB-INF/lib directory as well.

Don't even THINK of putting the log4j or Hibernate jars in the TOMCAT_HOME/lib directory. They're not designed to be used that way and you will not like the results. Well, actually, you probably would put the log4 jar in TOMCAT_HOME/lib, but only if you'd built Tomcat use use log4j instead of juli.

Actually, I think Hibernate might be using Apache Commons logging internally, which is why I started using slf4j in the first place. Because they used that logger, but my application code used log4j. Using slf4j ensured that everyone was happy and all the logs got routed consistently.
 
Von South
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the input. Tom I see what you mean and so I also think it would be better to separate the tomcat logging from the web app's logging. So I'm going to use log4j or standard logging for tomcat on it's own(still have to decide), and then slf4j and log4j for my web app and for hibernate.
 
Von South
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I meant Tim not Tom.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!