13

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're using rails 2.x

Thanks.

2
  • possible duplicate of How to log something in Rails in an independent log file? Commented Mar 29, 2011 at 15:30
  • 4
    I don't think it's a duplicate - it's a different question. About how to automatically split the standard log files into more fine grained parts - instead of manually logging to a separate file. Commented Mar 29, 2011 at 16:16

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to do this for exceptions in general? or to maybe log erb / controller errors to their own files too?
Posted how to do controllers/views.
How is it going to have only the errors? You're clearly logging an info and it ends up in exceptions.log?
This answer does not answer the question. It is off topic. It tells how to log to different files from different app layers / modules, not how to log debug/info/error/warn etc. level messages in to separate files.
7

Try the following. Put the rescue_from method in your controller.

I haven't tested this. But maybe it puts you in the right direction

class ApplicationController < ActionController::Base
  rescue_from StandardError do |exception|
    new_logger = Logger.new('log/exceptions.log')
    new_logger.info('THIS IS A NEW EXCEPTION!')
    new_logger.info(exception.message)
    new_logger.info(exception.backtrace)
    # Raise it anyway because you just want to put it in the log
    raise exception
  end
end

If you use Rails 2.1 (also not tested)

class ApplicationController < ActionController::Base
  def rescue_action_in_public(exception)
    new_logger = Logger.new('log/exceptions.log')
    new_logger.info('THIS IS A NEW EXCEPTION!')
    new_logger.info(exception.message)
    new_logger.info(exception.backtrace)
    # Raise it anyway because you just want to put it in the log
    raise exception
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.