# Request logging: around\_filters can't see error responses!

**URL:** <https://www.ruby-forum.com/t/request-logging-around-filters-cant-see-error-responses/92763>\
**Category:** Rails\
**Created:** [April 24, 2007, 12:02am UTC](https://www.ruby-forum.com/t/request-logging-around-filters-cant-see-error-responses/92763 "2007-04-24T00:02:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5fc32e/32.png) [@joshmh](https://www.ruby-forum.com/u/joshmh)\
**Post date:** [April 24, 2007, 12:02am UTC](https://www.ruby-forum.com/t/request-logging-around-filters-cant-see-error-responses/92763/1 "2007-04-24T00:02:05Z")

</div>

Hello,

I want to emit a structured log entry (in YAML) for every request that  
hits my Rails app. For example, an entry in this log would look  
something like:

* * *

SYSTEM\_TIME: 0.0166666666666667  
USER\_TIME: 0.0999999999999996  
DB\_TIME: 0.00247621536254883  
REAL\_TIME: 0.091976  
STATUS\_CODE: 500  
SESSION\_ID: 64ad46a8841c66bbf64e93335cedc50d  
REQUEST\_ID: 32d6a42f-da58-43e2-ab27-09d3e2a775cb  
RESPONSE\_BODY\_SIZE: 7001  
REMOTE\_IP: 127.0.1.1  
TIMESTAMP: 2007-04-23 12:05:41.555780 -07:00  
USER\_AGENT: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/  
20060601 Firefox/2.0.0.3 (Ubuntu-edgy)  
USER\_ID: 102  
QUERY\_STRING:  
PATH: /home  
IS\_AJAX: false  
PARAMS: {}

I tried doing this using an around\_filter. The #before part stored  
the time and generated a request ID, and the #after part dumped all  
the remaining data from the controller.request and controller.response  
objects into this log. This worked great, except in the case that any  
error occurred. In this case, Rails would run my after filter before  
the status was set to 500, so my log would never see STATUS\_CODE: 500.

My second try was to wrap/override ActionController::Base#process:

class ActionController::Base  
unless instance\_methods.include?(“original\_process”)  
alias\_method :original\_process, :process

```
    def process(*args)
        # do "before" stuff

        ret = framework_process(*args)

        # do "after" stuff
        return ret
    end
end

```

end

This works, but naturally I would prefer not to muck with the guts of  
Rails if I can help it. Is there any better way to accomplish my  
goal?

Josh
