Work with a small Ruby script that goes online and scans various services. I have a module with several classes inside:
module Crawler class Runner class Options class Engine end
I want to share one registrar among all these classes. Usually I just put this in a constant in the module and referenced like this:
Crawler::LOGGER.info("Hello, world")
The problem is that I canβt create my own copy of the magazine until I find out where the exit goes. You start the scanner through the command line, and at this point you can say that you want to run it in development (the output of the log goes to STDOUT) or to production (the output of the log goes to the file, crawler.log):
crawler --environment=production
I have an Options class that parses parameters passed through the command line. Only at this moment I know how to create a registrar instance with the correct output location.
So my question is: how / where do I put my logger object so that all my classes have access to it?
I can pass my log instance for every call to new() for every instance of the class I create, but I know that there must be a better, Rubyish way to do this. I imagine some weird class variable in a module that shares with class << self or some other magic. :)
A bit more detailed information: Runner starts everything by passing command-line options to the Options class and returns an object with several instance variables:
module Crawler class Runner def initialize(argv) @options = Options.new(argv)
I need code in Engine to have access to the log object (along with a few more classes initialized inside Engine ). Help!
All this could have been avoided if you could just dynamically change the output location of the already created Logger (similar to how you change the log level). I would create it in STDOUT and then go to the file if I am in production. I saw somewhere a proposal to change the global variable Ruby $ stdout, which would redirect the output somewhere other than STDOUT, but it seems pretty hacked.
Thank!
ruby logging class-design
Rob Cameron May 27 '09 at 19:23 2009-05-27 19:23
source share