Logger for class method from an included module

Discussion in 'Ruby' started by Iain Barnett, Mar 28, 2011.

  1. Iain Barnett

    Iain Barnett Guest

    Hi,

    I want to log what's happening when a class method is called, using a =
    Logger instance. The code resembles this:=20

    =20
    module Helpers
    def self.included(klass)
    klass.extend ClassMethods
    end
    =20
    module ClassMethods
    =20
    def my_classy_method( arg1, arg2 )
    #I want to log in here
    ...
    end

    end#ClassMethods

    end#Helpers


    class User
    =20
    include Helpers

    #boring stuff...

    end


    #called as such
    User.my_classy_method( "blah", "plop" )


    I've no idea where I should send the logger instance to that I've =
    already set up in the rest of the app. Would someone be good enough to =
    enlighten me? Should I perhaps alter the method signature to send the =
    logger in as an argument on the end like this:

    def my_classy_method( arg1, arg2, options=3D{} )
    options[:logger].debug ...

    User.my_classy_method( "blah", "plop", {logger: @logger} )

    or would that be bad form?

    Any help on the matter is much appreciated.


    Regards,
    Iain=
     
    Iain Barnett, Mar 28, 2011
    #1
    1. Advertising

  2. Iain Barnett

    Tim Pease Guest

    On Mar 28, 2011, at 1:16 PM, Iain Barnett wrote:

    > Hi,
    >=20
    > I want to log what's happening when a class method is called, using a =

    Logger instance. The code resembles this:=20

    <snip>

    > I've no idea where I should send the logger instance to that I've =

    already set up in the rest of the app. Would someone be good enough to =
    enlighten me? Should I perhaps alter the method signature to send the =
    logger in as an argument on the end like this:
    >=20
    > def my_classy_method( arg1, arg2, options=3D{} )
    > options[:logger].debug ...
    >=20
    > User.my_classy_method( "blah", "plop", {logger: @logger} )
    >=20
    > or would that be bad form?
    >=20
    > Any help on the matter is much appreciated.
    >=20


    Since you want to reference a single Logger instance globally, perhaps a =
    global variable might be useful?

    $app_logger =3D Logger.new( ... )

    If global variables turn your stomach, you could create an application =
    level module that contains your logger and/or other common objects.

    module MyApp
    extend self

    def logger
    @logger ||=3D Logger.new( ... )
    end
    end

    And now from anywhere in your code you can get the global logger thus

    MyApp.logger

    Another option to consider is the "logging" gem =
    <https://github.com/TwP/logging>. The advantages of the logging gem are =
    the ability to give each class it's own logger and still send all log =
    events to the same destination (stdout, file, syslog, or any =
    combination). You can then tweak the log level on a class by class =
    basis. You can set the log level for one class to "debug" and keep all =
    the others at "info" or "warn". This is great when you only need to =
    debug a small subset of your application.

    Blessings,
    TwP
     
    Tim Pease, Mar 28, 2011
    #2
    1. Advertising

  3. Iain Barnett

    Iain Barnett Guest

    On 28 Mar 2011, at 23:07, Tim Pease wrote:
    >=20
    > Since you want to reference a single Logger instance globally, perhaps =

    a global variable might be useful?
    >=20
    > $app_logger =3D Logger.new( ... )
    >=20
    > If global variables turn your stomach,


    I think the general ire against global vars prevented my head from =
    considering this :)

    > you could create an application level module that contains your logger =

    and/or other common objects.
    >=20
    > module MyApp
    > extend self
    >=20
    > def logger
    > @logger ||=3D Logger.new( ... )
    > end
    > end
    >=20
    > And now from anywhere in your code you can get the global logger thus
    >=20
    > MyApp.logger


    I can see I start using this in code a lot, not just for loggers, and =
    probably inappropriately, but there you go.


    >=20
    > Another option to consider is the "logging" gem =

    <https://github.com/TwP/logging>. The advantages of the logging gem are =
    the ability to give each class it's own logger and still send all log =
    events to the same destination (stdout, file, syslog, or any =
    combination). You can then tweak the log level on a class by class =
    basis. You can set the log level for one class to "debug" and keep all =
    the others at "info" or "warn". This is great when you only need to =
    debug a small subset of your application.

    I've downloaded it and am giving it a whirl now! Thanks.

    Regards,
    Iain=
     
    Iain Barnett, Mar 31, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sachin
    Replies:
    1
    Views:
    976
    Soren Kuula
    Feb 3, 2006
  2. Replies:
    4
    Views:
    118
    Robert Klemme
    Feb 23, 2005
  3. cap
    Replies:
    3
    Views:
    293
    James Edward Gray II
    Dec 11, 2005
  4. Georges Ko
    Replies:
    4
    Views:
    273
    Georges Ko
    Jul 26, 2006
  5. Prince Nez
    Replies:
    3
    Views:
    255
    Robert Klemme
    Aug 18, 2009
Loading...

Share This Page