views:

392

answers:

2

So, I'm beating my head against the wall with logging again. I know, how complex can it be? Well, let's see...

I'm starting a new project to be run on WebSphere Application Server 6.1 (actually Portal Server 6.1, but it's WAS 6.1 under the hood - whatever). I usually use java.util.logging for my WAS projects and everything is fine. This customer is a SLF4J fan and wants to use that. Fair enough, sounds easy.

So, I deploy slf4j-api-1.5.8.jar and slf4j-jdk14-1.5.8.jar in my WEB-INF/lib directory. In my code I do a --

// These classes are coming from org.slf4j.*
private static Logger log = LoggerFactory.getLogger(MyClass.class);
...
log.debug("This is a log message");

As expected I get an entry in the SystemOut.log. However, it's the format of that message that I can't figure out. A sample would be --

[12/15/09 15:43:15:071 EST] 00000042 MyClass D com.example.MyClass This is a log message

Let me explain what's in that sample log entry. I assume everything to the left of com.example.MyClass is coming from the j.u.l formatter. Everything to the right of it is what I included in my log.debug(). So, who's adding the com.example.MyClass? Only thing I can think is that SLF4J is adding it before it passes the message through to the underlying j.u.l.

It's the com.example.MyClass part that's irritating me. I don't want that included in the SLF4J-generated log message. The class name is already included, so it's extra fluff that's not needed. Plus, the real package names are quite long and their inclusion just pushes the real meat of the log entry too far off to the right.

When I use just plain java.util.loggging, the log entry is exactly the same except that the "com.example.MyClass" piece is not included. Exactly as I want!

So, the question is - how can I get rid of this extra class name entry in the log messages generated via SLF4J under WAS?

Thanks!

A: 

Basically you want to configure the layout of the logging messages produced by the underlying logging mechanism.

SLF4J does not actually perform the logging, but delegates to other logging systems (log4j, JUL, etc) based on how you set it up.

So if you are binding SLF4J with JUL, then I think the real question you are asking is either one of

matt b
Thanks, but nope. I'm seeing two distinctly different formats. If I use jul directly I DO NOT get the package+class prepended. If I use the SLF4J binding to jul I DO get the package+class. I'm making no underlying jul logging config changes (either programmatically or via properties). In fact, I don't think there are any jul config options inside the WAS container other than setting levels. Certainly nothing with formatters or handlers.Someplace deep in the bowels of WAS, SLF4J is prepending that stuff. Convinced of that.
K-Boo
+1  A: 

You bind slf4j to java.util.logging which most likely is configured inside WebSphere as it doesn't look like the standard message format.

I do not know WebSphere, but you may get a better result by telling slf4j to bind to something else. Would the slf4j-simple backend do? It just prints out info-or-higher messages instead of invoking java.util.logging.

Thorbjørn Ravn Andersen
Thanks for you reply (and Matt B. too!). Of course, I didn't explain myself very well.First off, WAS uses j.u.l as it's framework. Since I'm using SLF4J to "pass through" to j.u.l, my expectation is that I'll pick up it's format that's tucked away someplace. That's not happening.If I log to a SLF4J Logger instance and then immediately to a j.u.l instance, I get two differently formatted entries. The SLF4J has that damned extra package+class entry.Only thing I can think is that SLF4J is prepending the package+class onto the log message before it send it along to j.u.l. Make sense?
K-Boo
No. Thats not how it works. Are you sure you are not picking up classes from the web container?
Thorbjørn Ravn Andersen