mbdev
mbdev

Reputation: 6563

How to convert log level symbol into constant?

I am creating a new logger and want to configure the log level to equal Rails configuration value.

@logger = Logger.new(STDOUT)
@logger.level = Rails.configuration.log_level

I get an error since level expects integer and log_level is a symbol (:info). How can I convert the :info into something Logger understands (Logger::INFO)?

I tried using:

@logger.level = "Logger::#{Rails.configuration.log_level.to_s.upcase}".constantize

But constantize does not recognize the symbol:

NameError: uninitialized constant Logger::INFO

Upvotes: 2

Views: 1024

Answers (1)

mbdev
mbdev

Reputation: 6563

Just found it:

Logger.const_get(Rails.configuration.log_level.to_s.upcase)

Upvotes: 4

Related Questions