Logger.new cannot run

7 views
Skip to first unread message

Koji

unread,
Feb 6, 2023, 3:56:41 AM2/6/23
to rspec
Hello,

I am trying to test a pure ruby program (not a Rails program) that contains 'Logger.new'.

The command 'bundle exec ruby' requires Logger.new to have 1..3 arguments.

On the other hand, the command 'bundle exec rspec' requires 0 argument as shown below. Also Logger.new with 0 argument fails.

How do you treat Logger.new in a rspec program?

Thanks you for your advice.

Koji

----------
## spec/logger_spec.rb
require 'logger'
RSpec.describe "Logger.new" do
    it "has 1 argument" do
        logger1 = Logger.new('logfile')
    end
    it "has 0 argument" do
        logger2 = Logger.new
        logger2.info("HELLO WORLD")
     end
end


## results of 'bundle exec rspec spec/logger_spec.rb'
Logger.new
has 1 argument (FAILED - 1)
has 0 argument (FAILED - 2)

Failures:

1) Logger.new has 1 argument
Failure/Error: logger1 = Logger.new('logfile')

ArgumentError:
wrong number of arguments (given 1, expected 0)
# ./spec/logger_spec.rb:4:in `initialize'
# ./spec/logger_spec.rb:4:in `new'
# ./spec/logger_spec.rb:4:in `block (2 levels) in <top (required)>'

2) Logger.new has 0 argument
Failure/Error: logger2.info("HELLO WORLD")

NoMethodError:
undefined method `info' for #<Logger:0x00007ff59f396880>
# ./spec/logger_spec.rb:8:in `block (2 levels) in <top (required)>'

## software versions
RSpec 3.12
ruby 3.2.0

Jon Rowe

unread,
Feb 6, 2023, 4:08:48 AM2/6/23
to rspec
Hi

Something must be wrong with the your local setup as there is no Logger.new/0 (https://ruby-doc.org/3.2.0/stdlibs/logger/Logger.html#method-c-new) and this is backed up by irb:

```
 require 'logger'
=> true
irb(main):004:0> Logger.new
/Users/jon/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/logger.rb:577:in `initialize': wrong number of arguments (given 0, expected 1..3) (ArgumentError)
```

Running your spec on my machine produces the right result:

```
.F

Failures:

  1) Logger.new has 0 argument
     Failure/Error: logger2 = Logger.new

     ArgumentError:
       wrong number of arguments (given 0, expected 1..3)
     # ./logger_spec.rb:7:in `new'
     # ./logger_spec.rb:7:in `block (2 levels) in <top (required)>'
     # ./.bundle/gems/ruby/3.2.0/gems/bundler-2.3.7/exe/bundler:4:in `load'
     # ./.bundle/gems/ruby/3.2.0/gems/bundler-2.3.7/exe/bundler:4:in `<top (required)>'

Finished in 0.00144 seconds (files took 0.04754 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./logger_spec.rb:6 # Logger.new has 0 argument
```

Do you for some reason have a local `logger.rb` file in your `lib` directory that redefines it? What other gems are in your Gemfile? This doesn't seem like an RSpec issue as we certainly don't override it.

Cheers
Jon

Koji

unread,
Feb 6, 2023, 4:44:23 AM2/6/23
to rspec
Hello Jon,

Thank you very much for your prompt reply and helpful advice.

As you pointed out, I had my own logger.rb in the lib directory like this:

> require "logger"
> class Logger
>     class << self
>         def logger_factory(debug: true)
>             if debug
>                 @logger = Logger.new(STDOUT, level: :debug)
>             else
>                 @logger = Logger.new('log/logfile', 'daily', level: :info)
>             end

By renaming this file and class to 'mylogger.rb' and 'MyLogger',
everything has become good.

Thank you very much again.
And I also thanks to everyone who develops this wonderful RSpec gem.

Best Regards,
Koji

2023年2月6日月曜日 18:08:48 UTC+9 ma...@jonrowe.co.uk:
Reply all
Reply to author
Forward
0 new messages