LMCMLJ LMCMLJ - 1 year ago 85
Ruby Question

Ruby logger scope

I am trying to create a simple logging solution for my program using ruby's built in logger function. At the moment, what I have is the initialization for the logger like this:

class Setup

def initialize
logger = Logger.new(logfile.log)
logger.level = 'DEBUG'
logger.datetime_format = '%Y-%m-%d %H:%M:%S'

And then when I try to call to the logger within other functions in the same class with:


I get an error saying:

undefined local variable or method 'logger'

How should I access the logger from outside the local scope of the method it is defined in?

Thanks in advance.

Answer Source

logger is a local variable, which is only visible in the area where it was defined. Rename it to @logger to make it be an instance variable which is stored inside the object and can be used from any of the object's methods.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download