Search code examples
ruby-on-railsloggingstack-traceruby-on-rails-7

Rails 7.1.3 how to view stack traces in a production environment


How can I view stack traces for a Rails 7.1.3 app running in production? I am used to tailing the production.log file however this no longer exists After referring to This answer plus the various links to rails documentation there are various solutions described however they are all buggy and result in stack traces being pushed to the production.log file but all solutions prevent any page content being rendered whatsoever and result in blank screens, remove the logging functionality and I get content rendered.

Options I have tried include adding various logging code to the environments production.rb

The auto generated code for a new rails app has the following lines

  # Log to STDOUT by default
  config.logger = ActiveSupport::Logger.new(STDOUT)
    .tap  { |logger| logger.formatter = ::Logger::Formatter.new }
    .then { |logger| ActiveSupport::TaggedLogging.new(logger) }

Taken from here

broadcast = BroadcastLogger.new(stdout_logger, stderr_logger, file_logger)

Another attempt taken from the accepted answer linked to above was this

config.logger = ActiveSupport::BroadcastLogger.new(
  ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout,              formatter: Logger::Formatter.new)),
  ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new("log/production.log", formatter: Logger::Formatter.new))
)

I assume file logging was removed as default for a reason and there must now be an alternative preferred means of debugging a production app?

All attempts apart from the original code result in no content being displayed despite the log traces showing successful actions.

I need to debug a specific page that is having trouble finding tinymce javascript resources which may well be down to routes. Without the ability to view stack traces I am stuck.


Solution

  • It's better to figure out how to view your logs in production. Example for systemd setup:

    $ systemctl list-units | grep rails
      rails-app.service                     loaded active running   StackoverflowApp
    

    Logs can be viewed with journalctl:

    $ journalctl -f -u rails-app.service
    

    You can also try tailing syslog:

    $ tail -f /var/log/syslog
    

    BroadcastLogger is still broken, but seems to work without tagged logging:

    config.logger = ActiveSupport::BroadcastLogger.new(
      ActiveSupport::Logger.new($stdout,              formatter: Logger::Formatter.new),
      ActiveSupport::Logger.new("log/production.log", formatter: Logger::Formatter.new)
    )
    

    You could also just log to file only:

    config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new("log/production.log", formatter: Logger::Formatter.new))