Click to See Complete Forum and Search --> : wrapper vs. decorator


dberansky
January 9th, 2007, 11:46 AM
Just like many other developers we are using log4xx tool for our logging needs. Recently a new requirement has come up that for some debugging messages at certain times we want to log the stack trace at the time of the logging call. It looks like there are two ways I can implement this functionality:

1. create a wrapper on top of the log4xx library that would create the stack trace and add it to the message.

2. create a new log4xx specific layout decorator.

solution (1) has the advantage of being independent of the underlying logging implementation, when (2) would only work for log4xx, but my got feeling tells me that (2) is much cleaner (there is something I don't quite like about (1), but I don't know what it is).

Any thoughts?

boudino
January 10th, 2007, 02:31 AM
I think that (1), the wrapper is the right solution. Why? The purpose of decorator is to extend the protocol of an object, not to modify it. But you need to modify the behavior - add the trace log facility over standard behavior of logging methods. This the the purpose of wrapper (aka adaptor).

Surely, this is true if I understood well the requirement. If the stack trace logging should be standalone method invoked the same way as common logging methods, the decorator would be better.

dberansky
January 10th, 2007, 10:52 AM
If the stack trace logging should be standalone method invoked the same way as common logging methods, the decorator would be better.

That's exactly it. Now I understand why I was weary of the wrapper. As far as the API is concerned, nothing changes. The only difference is in configuration, which would say "now for every debug level message, print (or don't) the stack trace"