7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

logrageを使う

Last updated at Posted at 2019-04-07

Railsのログをjson化するためにlogrageを使う

Gemfile
gem 'lograge'
gem 'logstash-event' # json化するために必要
config/initializers/lograge.rb
Rails.application.configure do
  config.lograge.enabled = true

  # If you're using Rails 5's API-only mode and inherit from ActionController::API, you must define it as the controller base class which lograge will patch:
  # config.lograge.base_controller_class = 'ActionController::API'

  # JSON(logstash形式)で出力
  config.lograge.formatter = Lograge::Formatters::Logstash.new

  # オリジナルのlogを残す
  config.lograge.keep_original_rails_log = true

  # logrageのlogを別ファイルに保存
  config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"

  # 独自パラメータを取得 (controllerが落ちてくる)
  config.lograge.custom_payload do |controller|
    params = controller.request.params.except(* %w[controller action])

    {
      request_id: controller.request.request_id,
      user_id: controller.try(:current_user).try(:id), # 例) 確定してるなら&.のほうが良い
      params: params
    }
  end

  # 独自パラメータを設定 (ActiveSupport::Notifications::Event name='process_action.action_controller' が落ちてくる)
  config.lograge.custom_options = lambda do |event|
    ret = {
      request_ip: event.payload[:request_ip],
      error_message: nil,
      error_stacktrace: nil
    }

    if (eo = event.payload[:exception_object])
      ret.merge!(
        error_message: eo.message,
        error_stacktrace: eo.backtrace.join("\n")
      )
    end

    ret
  end
end

こんなログが出力される

#{Rails.root}/log/lograge_#{Rails.env}.log
正常時
{"method":"GET","path":"/api/workflows","format":"html","controller":"Api::WorkflowsController","action":"index","status":200,"duration":58.42,"view":11.2,"db":40.94,"request_ip":null,"exception":null,"exception_object":null,"error_message":null,"error_stacktrace":null,"request_id":"4dbf2f3c-ff83-422e-b16d-d8584cbdcf6e","user_id":null,"params":{},"@timestamp":"2019-04-07T04:51:39.978Z","@version":"1","message":"[200] GET /api/workflows (Api::WorkflowsController#index)"}

例外時
{"method":"GET","path":"/api/workflows","format":"html","controller":"Api::WorkflowsController","action":"index","status":500,"error":"RuntimeError: ","duration":0.63,"view":0.0,"db":0.0,"request_ip":null,"exception":null,"exception_object":null,"error_message":"","error_stacktrace":"<<<ここにスタックトレースが出力>>>","request_id":"b680ad4a-9eea-4392-9084-2df09d0ab850","user_id":null,"params":{},"@timestamp":"2019-04-07T04:55:13.013Z","@version":"1","message":"[500] GET /api/workflows (Api::WorkflowsController#index)"}

参考:
https://github.com/roidrage/lograge

custom_optionsで落ちてくるeventは 'process_action.action_controller'
https://edgeguides.rubyonrails.org/active_support_instrumentation.html#process-action-action-controller

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?