How to best add logging/debugging code?

Peter Simons simons at cryp.to
Sun Nov 16 22:43:55 EST 2003


Ben Escoto writes:

 > 1.  Use some kind of logging monad.

This is definitely the way to go. The 'MonadWriter' class
provides a very general interface, which often proves to be
much more useful than just for logging. Especially, when you
use it to return data types with useful information --
rather than just strings. And 'mapWriter' gives you much
flexibility, when it comes to creating glue code to convert
different types of log events.

You can use the transforming variant to encapsulate other
monads, like IO a. Then, running code that is unaware of
logging requires a call to 'lift', and that's it.

I really don't see the need to resort to unsafePerformIO in
this case. Which, besides, has the disadvantage of being
hard to selectively enable/disable, it's hard to write to
anywhere but stderr, and it's, well, unsafe. :-)

Peter



More information about the Haskell mailing list