Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Ruby Standard Library Logger

steven alston
steven alston
16,292 Points

in `log': undefined method `add' for nil:NilClass

is this code deprecated in the current version of Ruby. Here is my code

require 'logger'

class BankAccount
    attr_reader :file_logger, :stdout_logger

    def initialize(name)
        @name = name
        @transactions = []
        @logger = Logger.new(STDOUT)
        @file_logger = Logger.new("./bank_account.log")

    def deposit(amount)
        log "Depositing #{amount}"

    def withdraw(amount)
        log "Withdrawing #{amount}" ###logger.debug

    def log(message, level=Logger::INFO)
        file_logger.add level, message, "#{self.class} (#{@name})"
        stdout_logger.add level, message, "#{self.class} (#{@name})"

account = BankAccount.new("Steven")

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

It works fine in every version above 1.9 if you change line 9 to this:

@stdout_logger = Logger.new(STDOUT)