log.debug vs. logger.debug - which do you prefer?
This is probably a bit of a religious debate, but it can't hurt to ask. Do you prefer to use log.debug()
or logger.debug()
in your Java classes? A fair amount of open source projects use Commons Logging and many of them seem to use logger. Personally, I prefer log (esp. b/c it's shorter), but I'm willing to change based on what the community (particularly AppFuse users) prefer.
Here's another tip I learned today. I typically declare a log variable for each class, such as this one in BaseAction.java:
protected static Log log = LogFactory.getLog(BaseAction.class);
|
A better design can be found in Spring's DaoSupport classes. They have a logger variable that all its subclasses can use - eliminating the need to initialize a log
variable in each class.
protected final Log logger = LogFactory.getLog(getClass());
|
Obviously this is cleaner than AppFuse's current design - so I'll be changing it for 1.6. Any reasons why I shouldn't?
Posted by Francisco Hernandez on August 17, 2004 at 04:31 AM MDT #
Posted by Pat on August 17, 2004 at 07:30 AM MDT #
Posted by Mathias Bogaert on August 17, 2004 at 07:33 AM MDT #
Posted by Pål Brattberg on August 17, 2004 at 08:27 AM MDT #
Posted by Lars Fischer on August 17, 2004 at 09:39 AM MDT #
Posted by lasas on August 17, 2004 at 10:12 AM MDT #
Posted by Howard M. Lewis Ship on August 17, 2004 at 10:56 AM MDT #
I use log and prefer to use the first approach. Unless I'm going mad, that's a new Log instance per "owning" class <strong>instance</strong>.
I'm not sure I'd want a new Log instance for each of the objects I instantiate. I'm sure it's lightweight enough, but still another object per object ... <em>nah!</em>
Of course, having said all that, it would depend on the circumstance. I would be tempted to use it in my BaseAction (as they're pseudo-Singletons), but not as a general rule.
Posted by Carl on August 17, 2004 at 10:58 AM MDT #
-
A
-
B extends A
-
C extends A
where A provides a <code>protected static Logger log = Logger.getLog(A.class);</code> that's shared, then what you'll see in the logs is instead of right? So I'd go for the latter approach even though it'd cost me a few more objects, it's much more clearer. Or do the logging libraries take the class name from somewhere else?Posted by Santa Claus on August 17, 2004 at 11:23 AM MDT #
Posted by Johann Reyes on August 17, 2004 at 11:55 AM MDT #
Posted by Karsten Voges (Weblog) on August 17, 2004 at 12:44 PM MDT #
Posted by Keller on August 17, 2004 at 01:14 PM MDT #
Posted by Ryan Breidenbach on August 17, 2004 at 01:33 PM MDT #
Santa Claus,
My apologies, I didn't take enough notice of the code for option A.
Instead of <code>protected static Log ...</code>, I use <code>private static Log ...</code> (thereby preventing subclasses using the Log instance, and exhibiting the behaviour you describe).
So, I'd go for my version of option A! (but feel okay about option B for things like <code>Action</code>s
Posted by Carl on August 17, 2004 at 01:39 PM MDT #
Santa & All, sometimes you want to do both. The simple reason is sometimes you want to know (via the log) when the base class is performing an operation that is completely independent of the subclasses. If the implementation is in the subclass, you want to know exactly which one and not have all of them tagged with the name of the base class.
I don't know about the earlier comment about log4j not supporting a couple of filters in once case, but that would be good to know. I don't actually use log4j for my logging, but I'd be curious to know why some of the filters wouldn't work.
Posted by gerryg on August 17, 2004 at 03:33 PM MDT #
Well... I was used to use the first approach. But seemed to be a good design to put the static log (in time, I used "log" instead of "logger") in the superclass. I use Log4j and I can confirm that some filters really don't work with the second approach. Since I don't had the time yet do do my homework and test or search wich one works and wich don't, all i get in the console is (Unknown Source)[?:?].
log4j.properties:
Posted by Alexandre Jacques on August 17, 2004 at 06:17 PM MDT #
Posted by Seth Ladd on August 17, 2004 at 06:20 PM MDT #
Posted by 67.20.182.8 on August 18, 2004 at 02:06 AM MDT #
Posted by Atoy on August 18, 2004 at 04:09 AM MDT #
Posted by Jano on August 21, 2004 at 11:24 PM MDT #
Posted by 212.188.183.94 on August 14, 2008 at 03:44 PM MDT #