gingerlime
gingerlime

Reputation: 5346

logging info with rails

Moving over from django / python, I am having a little trouble getting the rails logger to log all the information I want. I am wondering how/if the following can be achieved:

  1. Having in the log format(ter) include the specific file, function name and line where the logging statement itself was found. Essentially the equivalent of LOG_MSG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(funcName)s(%(lineno)d) : %(message)s' in python logger?
  2. Being able to log all requests, via something similar to a django request logging middleware. Particularly, being able to log the username (if logged in) of every request.

Am I missing something obvious? or does this require (lots of) custom code?

Upvotes: 1

Views: 686

Answers (3)

gingerlime
gingerlime

Reputation: 5346

Just to add a quick note in case this is useful for someone:

The lograge gem makes rails logs much similar to django's, plus allows very neat customization, adding parameters such as remote ip address, current_user etc.

It also reduces verbosity of rendered layouts, which I anyway found unnecessary for production. It also plays nicely with logging-rails railtie mentioned by @lukewendling.

Sadly, I still couldn't find anything that shows the file/function/line number like you can easily do with django, but I guess that's just too much to ask.

Upvotes: 1

Unixmonkey
Unixmonkey

Reputation: 18784

I don't know about getting the file, function, and line number, but it's pretty easy to log from application_controller:

class ApplicationController < ActionController::Base
  before_filter :log_user
  def log_user
    if current_user
      Rails.logger.info "Processing Request for #{current_user.name}"
    end
  end
end

Upvotes: 1

Luke W
Luke W

Reputation: 8934

I just found this railtie gem that might help although I imagine it will take some "custom code" to append username to logs. See the Readme section on logging specific controllers and models.

logging-rails railtie

Upvotes: 1

Related Questions