Techioz Blog

Rails の実行後にログを生成できない

概要

Rails を実行した後、ターミナルにログが表示されませんが、コードは問題なく正常に動作しています。ログは生成された logs/development.log ファイルを取得しています。ログがないため、デバッガーを使用できません。

=> Booting Puma
=> Rails 5.2.3 application starting in development 
=> Run `rails server -h` for more startup options
[95163] Puma starting in cluster mode...
[95163] * Version 4.1.1 (ruby 2.4.6-p354), codename: Fourth and One
[95163] * Min threads: 2, max threads: 2
[95163] * Environment: development
[95163] * Process workers: 2
[95163] * Phased restart available
[95163] * Listening on tcp://localhost:3000
[95163] Use Ctrl-C to stop

config/dvelopment.log ファイル

Rails.application.configure do
 
 
  config.active_storage.service = :local


  config.action_mailer.perform_caching = false
          
  config.assets.debug = true

  config.assets.quiet = true

  config.assets.raise_runtime_errors = true

  config.action_mailer.default_url_options = {
    host: Rails.application.secrets[:mailer_options][:host],
    port: Rails.application.secrets[:mailer_options][:port],
  }

  # ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)

  config.lograge.enabled = true
  config.log_level = :debug
  config.lograge.formatter = Lograge::Formatters::Logstash.new

  config.lograge.custom_options = lambda do |event|
    {
      :time => Time.now.in_time_zone(Time.zone),
      :params => event.payload[:params].reject { |k| %w(controller action).include? k },
      :subdomain => event.payload[:subdomain],
      :remote_ip => event.payload[:remote_ip],
      :user_agent => event.payload[:user_agent],
      :uuid => event.payload[:uuid],
      :current_user => (event.payload[:current_user])? event.payload[:current_user][:id] : nil,
      :current_appuser => (event.payload[:current_appuser])? event.payload[:current_appuser][:id] : nil
    }
  end
  config.lograge.ignore_actions = ['PublicPagesController#status_check']

  config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 7, 204857600)

     require "awesome_print"
end

解決策

ログ ファイルではなく開発環境の STDOUT にログを記録したい場合は、config/environments/development.rb 内のこの行を変更します。

config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 7, 204857600)

config.logger = Logger.new(STDOUT)