From: Tim Pease Date: 2011-03-29T07:07:50+09:00 Subject: Re: Logger for class method from an included module On Mar 28, 2011, at 1:16 PM, Iain Barnett wrote: > Hi, > > I want to log what's happening when a class method is called, using a Logger instance. The code resembles this: > 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={} ) > 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. > Since you want to reference a single Logger instance globally, perhaps a global variable might be useful? $app_logger = 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 ||= 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 . 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