Reputation: 2719
When I try to use Ruby's logger
, it comes up as nil
. Is an instance variable the correct way to do this?
require 'logger'
class LogStuff
# Log to STDOUT and `tee` it to a file
@logger = Logger.new("| tee output.log")
@logger.level = :debug
def run
(1..10).each do |n|
@logger.debug('l1') { @logger.debug "proceessing #{n}" }
end
['a', 'b', 'c', 'd', 'e'].each do |letter|
@logger.debug('letters') { @logger.debug "proceessing #{letter}" }
end
end
end
if __FILE__ == $0
lggr = LogStuff.new
lggr.run
end
Error message:
$ ruby script.rb
script.rb:11:in `block in run': undefined method `debug' for nil:NilClass (NoMethodError)
from script.rb:10:in `each'
from script.rb:10:in `run'
from script.rb:23:in `<main>'
Upvotes: 1
Views: 1014
Reputation: 30056
Try this one
def initialize
# Log to STDOUT and `tee` it to a file
@logger = Logger.new("| tee output.log")
@logger.level = :debug
end
Obviously into the LogStuff
class. Basically, otherwise your @logger
variable is defined in another scope.
Upvotes: 1