NameError: uninitialized constant Logger - Should I be doing this differently?

StackOverflow https://stackoverflow.com/questions/23500702

  •  16-07-2023
  •  | 
  •  

سؤال

Fairly new to ruby, playing with Mini Test and log4r, trying to write a wrapper class.

Getting the following error:

Run options: --seed 18181

# Running tests:

E

Finished tests in 0.015625s, 63.9996 tests/s, 0.0000 assertions/s.

  1) Error:
test_debug_messages(TestLogger):
NameError: uninitialized constant Logger
    /home/jamlopez/scripts/Yatra.Next/rpm_framework/lib/rpm/core/logger.rb:7:in `initialize'
    logger.rb:6:in `new'
    logger.rb:6:in `setup'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

Here's the class:

require 'log4r'

# RPM Framework Logger
# Based on and utilizing log4r >= 1.1.10
class RPMLogger
    def initialize
        @log = ::Logger.new 'log'
        @log.outputters = Outputter.stdout
        @logfile_location = brpm_home
        @timenow = eval( Time.now.utc.iso8601 )
    end
    def debug( msg )
        @log.debug ( '[DEBUG] ' + @timenow + " #{msg}" )
    end
    def info( msg )
        @log.info ( '[INFO] ' + @timenow + " #{msg}" )
    end
    def warn( msg )
        @log.warn ( '[WARNING] ' + @timenow + " #{msg}" )
    end
    def error( msg )
        @log.error ( '[ERROR] ' + @timenow + " #{msg}" )
    end
    def fatal( msg )
        @log.fatal ( '[FATAL] ' + @timenow + " #{msg}" )
    end
end

And here's the test:

require 'minitest/autorun'
require_relative "./../../../lib/rpm/core/logger"

class TestLogger < MiniTest::Unit::TestCase
  def setup
    @logger = RPMLogger.new
    @test_msg = "This is a test log message!"
  end

  def test_debug_messages
    log = @logger.debug( @test_msg )
    assert_match "/^[DEBUG] /", log, msg=nil
  end

end

I'll be the first to concede there are probably several errors in both files (as I'm still learning) but I'll try to take it one step at a time.

There are probably several ways to do this, I'm just not sure if this is one of them.

Using @log = ::Logger.new seems reasonable on the surface, but perhaps I should be doing an include log4r to extend the module instead? (per Ruby: module, require and include)

I've searched for related SO articles, and on the web regarding arguments to 'initialize'. Either they are not directly related, or I misunderstand them. Any assistance appreciated.

هل كانت مفيدة؟

المحلول

From the code sample in the manual:

require 'log4r'
include Log4r

# create a logger named 'mylog' that logs to stdout
mylog = Logger.new 'mylog'
mylog.outputters = Outputter.stdout

What you seem to be missing, in order to be able to use Logger, is to add below your require an include clause:

require 'log4r'
include Log4r

Alternatively you can call Log4r::Logger explicitly.

This is needed since class Logger is declared within module Log4r:

module Log4r

  # See log4r/logger.rb
  class Logger
  ...

Calling include Log4r makes all constants declared in Log4r be available in your code, Logger being one of them.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top