1

I'm searching for a while now a way to log only 404 errors in another log file than the other errors.

Is there a way to do it with the ruby logger or with log4r, in that way that the logging only happens in the new created log file?

Esse
  • 3,278
  • 2
  • 21
  • 25
Joschka Schulz
  • 674
  • 1
  • 8
  • 18

2 Answers2

2

Whilst I don't have a specific recommendation, I do have some experience with this type of thing. Maybe this will help...

--

I've written a gem called exception_handler which basically interjects into the exceptions_app middleware, allowing you to send any exceptions to a controller action.

One of the things you could do would be to use the technology in this gem to capture the 404 exceptions, and perform your own logging with them:

#config/application.rb
...
config.exceptions_app = ->(env) { ApplicationController.action(:exception).call(env) } 

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   def exception
       @error_code = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
       [[own logging]] if @status == 404
   end
end

This would be a start; I don't know how you'd make your own log file etc, and also this is very constrictive in the sense that if the error is anything other than 404, it may time out.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 1
    Thanks I will try if I can produce a solution with that for me. But it's a step in the right direction :) – Joschka Schulz Dec 10 '15 at 15:13
  • No problem - let me know how it goes. I've had a pretty bad cold today so not on top form unfortunately; otherwise would have added extras – Richard Peck Dec 10 '15 at 17:12
0

Errors to error_log are logged once they happen, so you can't configure separate error_log to log specific errors (in contrast to access_log).

However you may control whether to log "not found" errors or not - there is configuration directive "log_not_found" for this, see http://nginx.org/r/log_not_found.

Kai
  • 371
  • 3
  • 4
  • that's an topic for the nginx server and it's already a few years old. My hope is that someone needed to handle the errors in rails too. – Joschka Schulz Dec 10 '15 at 14:15