LMCMLJ
LMCMLJ

Reputation: 39

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:

logger.info('testlog')

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.

Upvotes: 2

Views: 253

Answers (1)

David Grayson
David Grayson

Reputation: 87406

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.

Upvotes: 1

Related Questions