0

I have a Rails 4.1 app using log4r 1.1.10 configured via yaml and everything works fine. Now I just want to add a custom level. So at the top of my yaml file I added seven lines after log4r_config:

log4r_config:
  pre_config:
    custom_levels:
      - INFO
      - WARN
      - ERROR
      - SERIOUS
      - FATAL
  # define all loggers ...
  loggers:

The relevant part of my application.rb looks like:

require 'log4r'
require 'log4r/yamlconfigurator'
require File.join(File.dirname(__FILE__),"../lib/rollable_file_outputter.rb")
include Log4r
log4r_config = YAML.load_file(File.join(File.dirname(__FILE__),"log4r.yml"))
YamlConfigurator.decode_yaml( log4r_config['log4r_config'] )

But when I launch the app now I get

ArgumentError: Log level must be in 0..7
/opt/thermyos.com/server/shared/bundle/ruby/2.0.0/gems/log4r-1.1.10/lib/log4r/yamlconfigurator.rb:189:in `decode_logger_common'
/opt/thermyos.com/server/shared/bundle/ruby/2.0.0/gems/log4r-1.1.10/lib/log4r/yamlconfigurator.rb:175:in `decode_logger'
/opt/thermyos.com/server/shared/bundle/ruby/2.0.0/gems/log4r-1.1.10/lib/log4r/yamlconfigurator.rb:69:in `block in decode_yaml'
/opt/thermyos.com/server/shared/bundle/ruby/2.0.0/gems/log4r-1.1.10/lib/log4r/yamlconfigurator.rb:69:in `each'
/opt/thermyos.com/server/shared/bundle/ruby/2.0.0/gems/log4r-1.1.10/lib/log4r/yamlconfigurator.rb:69:in `decode_yaml'
/opt/thermyos.com/server/releases/20141209152838/config/application.rb:23:in `<top (required)>'

What's the right way to define custom log4r levels in yaml?

RussK
  • 199
  • 1
  • 17

1 Answers1

0

The solution is never leave out the DEBUG level. It was just an oversight on my part. Since Log4r uses the debug level to write debug messages, leaving it out is very bad.

RussK
  • 199
  • 1
  • 17
  • It seems that Log4r itself uses an [internal_log](https://github.com/colbygk/log4r/blob/master/lib/log4r/staticlogger.rb#L43) method to write debug messages. Custom levels works fine in [my test cases](https://github.com/bestmike007/log4rails/blob/master/spec/xml_config_spec.rb#L26). Are you replacing Rails logger with Log4r? – bestmike007 Jan 07 '15 at 08:17
  • I am indeed replacing the Rails logger with Log4r. Custom levels worked fine for me once I included DEBUG. I don't have my notes or even my original application.rb file, but it's entirely possible that the reference to DEBUG came from my own log4r.yml file. – RussK Jan 07 '15 at 16:46
  • If you had replaced Rails logger with Log4r in development mode, an error should have occurred using Rails 4.1 when the Rack is trying to access `Rails.logger.format`. I recently created a new fork named [log4rails](http://bestmike007.com/log4rails/) to make log4r much more compatible to rails, especially for latest rails. – bestmike007 Jan 08 '15 at 02:23