[LRUG] Rails log_tags in production

Josh McMillan josh at joshmcmillan.co.uk
Mon Jul 18 02:25:44 PDT 2016


I'm guessing this is because request.cookie_jar.signed[session_key] returns
nil, which means that session_data['current_person_id'] will fail, as #[]
isn't a method that exists on nil. Pingdom won't be sending you through any
cookies, which means there's nothing to read out of the session.

As this is a Rails app and subsequently has ActiveSupport knocking around,
you could potentially change line 49 to read `user_id =
session_data.try(:[], 'current_person_id')`, which will return the intended
value if session_data responds to #[], or nil otherwise.

On Mon, Jul 18, 2016 at 11:19 AM, Andrew Stewart <boss at airbladesoftware.com>
wrote:

> Hello LRUG!
>
> I am trying to log the current user id in a Rails app via the following
> configuration:
>
>   # config/environments/{development,production}.rb
>   config.log_tags = [
>     lambda { |request|
>       session_key = Rails.application.config.session_options[:key]
>       session_data = request.cookie_jar.signed[session_key]
>       user_id = session_data['current_person_id']
>       user_id || 'public'
>     }
>   ]
>
> This works fine in development but in production, although the site works
> perfectly for a user, Pingdom immediately reports a 500 error and
> unicorn.stderr.log shows:
>
>   app error: undefined method `[]' for nil:NilClass (NoMethodError)
>
> /path/to/my/app/releases/20160717162528/config/environments/production.rb:49
>
> Line 49 is the `user_id = session_data['current_person_id']` one above.
>
> In production I'm using Rails 3.2 and Unicorn 4.2.0 (behind Nginx 1.6.2).
>
> Does anyone know why this doesn't work in production for Pingdom's bot?
>
> Many thanks in advance!
>
> Yours,
>
> Andy Stewart
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20160718/63d7c2f9/attachment.html>


More information about the Chat mailing list