From: Andrea Fazzi Date: 2008-06-26T17:53:09+09:00 Subject: Re: Is it ellegant to use a global variable to store a Logger object? Chuck Remes ha scritto: > > On Jun 25, 2008, at 11:04 AM, Andrea Fazzi wrote: > >> Robert Dober ha scritto: >>> each class would need to have an adapted initialize method, well that >>> >> >> Yes, and indeed this is very annoying. When the system grows up you >> have to do with long constructor signatures: >> >> Foo.new(logger, service, another_service ... ) >> >> But I wonder if there is a better method to keep things decoupled.. > > I typically provide a setter on my classes for configuring the log > object. Hardly perfect, but it keeps the constructor signatures short. > I am liking this suggestion to use a global variable though. I think > I'll experiment. Ok, you're using setter injection instead of constructor injection. > > On a related note, I'm curious how people solve the problem of turning > logging on and off during runtime. > > I hate code like: > > logger.log(:debug, "some msg") unless logger.nil? > > I usually provide a null class for my loggers which silently swallow > any messages sent to them if the logging function is disabled. It > cleans up the code considerably. Of course you can do that because you're setting a logger instance for each object with logging capability. But what to do if you are using a global logger instance and you wish to turn off logging selectively? Andrea