How can I log Rails errors into a separate log file?

前端 未结 2 1034
忘掉有多难
忘掉有多难 2020-12-28 20:35

Our production logs are long and contain a lot more than just errors. I\'d like a second log file with just the errors/exceptions in.

Is this possible?

We\'r

2条回答
  •  失恋的感觉
    2020-12-28 21:09

    For example, to log all ActiveRecord::Base errors in a file called log/exceptions.log

    new_logger = Logger.new('log/exceptions.log')
    new_logger.level = Logger::ERROR
    new_logger.error('THIS IS A NEW EXCEPTION!')
    
    ActiveRecord::Base.logger = new_logger
    

    For controllers and view(because ActionView logger doesn't have it's own logger, so it depends on the ActionController logger):

    ActionController::Base.logger = new_logger
    

提交回复
热议问题