[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Logging object.

tobyclemson@gmail.com

8/24/2007 11:13:00 PM

Hi,

Can anyone suggest the best way to pass a Logger object around between
all the classes in an application?

Currently I have it set in a constant in the top-level but I don't
feel that this is the best way to do it as it couples the code in each
of my classes to the current implementation and makes it difficult for
me to reuse the code elsewhere without first setting up a logger
instance of the same name.

Thanks,
Toby

6 Answers

Jeremy Wells

8/25/2007 1:23:00 PM

0

tobyclemson@gmail.com wrote:
> Hi,
>
> Can anyone suggest the best way to pass a Logger object around between
> all the classes in an application?
>
> Currently I have it set in a constant in the top-level but I don't
> feel that this is the best way to do it as it couples the code in each
> of my classes to the current implementation and makes it difficult for
> me to reuse the code elsewhere without first setting up a logger
> instance of the same name.
>
> Thanks,
> Toby
>
>
>
>
Surely any way you do this if a class writes to a logger you have to set
up the logger first? But anyhow, I guess you could make a module that
provides the logging and include that on all appropriate classes:

logging.rb:
LOG = Logger.new(...)

module Logging
def logger
LOG || Logger.new(nil)
end
end

something.rb:
require 'logging'

class Something
include Logging

def do_something
logger.info "something"
end
end



Gordon Thiesfeld

8/25/2007 1:51:00 PM

0

On Aug 25, 8:22 am, Jeremy Wells <jwe...@servalsystems.co.uk> wrote:
> tobyclem...@gmail.com wrote:
> > Hi,
>
> > Can anyone suggest the best way to pass a Logger object around between
> > all the classes in an application?
>
> > Currently I have it set in a constant in the top-level but I don't
> > feel that this is the best way to do it as it couples the code in each
> > of my classes to the current implementation and makes it difficult for
> > me to reuse the code elsewhere without first setting up a logger
> > instance of the same name.
>
> > Thanks,
> > Toby
>
> Surely any way you do this if a class writes to a logger you have to set
> up the logger first? But anyhow, I guess you could make a module that
> provides the logging and include that on all appropriate classes:
>
> logging.rb:
> LOG = Logger.new(...)
>
> module Logging
> def logger
> LOG || Logger.new(nil)
> end
> end
>
> something.rb:
> require 'logging'
>
> class Something
> include Logging
>
> def do_something
> logger.info "something"
> end
> end


khaines

8/25/2007 1:58:00 PM

0

Gordon Thiesfeld

8/25/2007 2:35:00 PM

0

On Aug 25, 8:22 am, Jeremy Wells <jwe...@servalsystems.co.uk> wrote:
> tobyclem...@gmail.com wrote:
> > Hi,
>
> > Can anyone suggest the best way to pass a Logger object around between
> > all the classes in an application?
>
> > Currently I have it set in a constant in the top-level but I don't
> > feel that this is the best way to do it as it couples the code in each
> > of my classes to the current implementation and makes it difficult for
> > me to reuse the code elsewhere without first setting up a logger
> > instance of the same name.


I had a similar need, but I also needed a way to send emails from
different classes as well, so the code started getting a lot of extra
junk in it. I solved it by borrowing some code from zenspider's
autotest. I took his idea of hooks and plugins and extracted it into
a mixin, so I can include it in all of my classes.

So in your toplevel file you might have:

require 'hookable'
require 'foo'
require 'logging'
require 'email'
require 'other_plugins'

foo.rb might look like this

class Foo
include Hookable

def something
hook :something_happened
end

end

then in logging.rb, you would have:

@log = Logger.new(...)

Foo.add_hook(:something_happened){|i| @log.info "something was
called"}

Also, in the block, you have access to the object that called the
hook.
The hookable mixin code is here: http://pastie.cabo...

regards,

Gordon




tobyclemson@gmail.com

8/25/2007 4:01:00 PM

0

On Aug 25, 3:34 pm, Gordon Thiesfeld <gthiesf...@gmail.com> wrote:
> On Aug 25, 8:22 am, Jeremy Wells <jwe...@servalsystems.co.uk> wrote:
>
> > tobyclem...@gmail.com wrote:
> > > Hi,
>
> > > Can anyone suggest the best way to pass a Logger object around between
> > > all the classes in an application?
>
> > > Currently I have it set in a constant in the top-level but I don't
> > > feel that this is the best way to do it as it couples the code in each
> > > of my classes to the current implementation and makes it difficult for
> > > me to reuse the code elsewhere without first setting up a logger
> > > instance of the same name.
>
> I had a similar need, but I also needed a way to send emails from
> different classes as well, so the code started getting a lot of extra
> junk in it. I solved it by borrowing some code from zenspider's
> autotest. I took his idea of hooks and plugins and extracted it into
> a mixin, so I can include it in all of my classes.
>
> So in your toplevel file you might have:
>
> require 'hookable'
> require 'foo'
> require 'logging'
> require 'email'
> require 'other_plugins'
>
> foo.rb might look like this
>
> class Foo
> include Hookable
>
> def something
> hook :something_happened
> end
>
> end
>
> then in logging.rb, you would have:
>
> @log = Logger.new(...)
>
> Foo.add_hook(:something_happened){|i| @log.info "something was
> called"}
>
> Also, in the block, you have access to the object that called the
> hook.
> The hookable mixin code is here:http://pastie.cabo...
>
> regards,
>
> Gordon

Thanks for all of your help. I'm having a bit of trouble getting into
the swing of OO properly - I mean I've read all the theory but in
practice there's still a lot to think about! I think it's mostly the
design patterns that I need to get used to as the actual
technicalities of a language aren't so difficult to pick up.

Anyhow, I think by picking bits from the methods suggested here I'll
be able to solve my problem. One other thing I was wondering about was
my testing. How does one test that a log has actually been written to
without writing to file but I was thinking if I have an object of a
class that acts as a dummy logger that can also work as a real logger
if required, I can query that object to see whether the log was
actually written.

I like the idea of using hooks because then multiple actions can occur
when a certain methods performs a certain task.

Another quick question, slightly off topic but something I've been
thinking about with regard to this:

If I have a test that checks the status of a logger object to
determine whether something is working correctly in the class under
test, is this a functional test or a unit test?

Thanks,
Toby

Again, thanks very much,
Toby

Stefan Rusterholz

8/25/2007 4:50:00 PM

0

tobyclemson@gmail.com wrote:
> Hi,
>
> Can anyone suggest the best way to pass a Logger object around between
> all the classes in an application?
>
> Currently I have it set in a constant in the top-level but I don't
> feel that this is the best way to do it as it couples the code in each
> of my classes to the current implementation and makes it difficult for
> me to reuse the code elsewhere without first setting up a logger
> instance of the same name.
>
> Thanks,
> Toby

Actually I use a whole different approach. I do all logging with
$stderr.puts(LogMessage.new(message, ...)) (I actually have methods like
info, error etc. in Kernel that make the task easier, but that's what's
going on). LogMessage has a to_s method, so if the lib is used in an
environment that doesn't make use of the LogMessage object, it will be
IO#puts which calls to_s on it.

Regards
Stefan
--
Posted via http://www.ruby-....