Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

Rails 6 logging stopped working in deployed VM

I have a rails 6.0.3 application that is working perfectly fine and deployed on both heroku (as staging) and on a normal digital ocean VM for production (used capistrano deploy for the same).

But the logs for some reason are not working only on the VM (please note that this used to work before). On dev and heroku, they work just fine

When I go to the shared logs, this is what I see:

~/app_name/current/log$ ll
total 28
-rw-rw-r-- 1 deploy deploy     0 Dec  1 15:36 "'production'.log"
-rw-rw-r-- 1 deploy deploy 17204 Dec  1 15:36 "'production'.log.1"
drwxrwxr-x 2 deploy deploy  4096 Jan  6 17:34  ./
drwxrwxr-x 9 deploy deploy  4096 Nov 28 07:04  ../
-rw-rw-r-- 1 deploy deploy     0 Jan  6 17:33  production.log

For some reason the earlier logs used to be in a file called "'production'.log", so I manually created production.log as an attempted solution to no avail. Note that: The "'production'.log.1" was logrotated with the last lines as:

Started GET "/users/sign_in" for 49.207.209.194 at 2020-12-01 02:35:28 +0000
Processing by Users::SessionsController#new as HTML
  Rendering users/sessions/new.html.slim within layouts/application
  Rendered users/sessions/new.html.slim within layouts/application (Duration: 0.6ms | Allocations: 75)
  Rendered shared/_header.html.slim (Duration: 0.2ms | Allocations: 41)
Completed 200 OK in 6ms (Views: 3.5ms | Allocations: 1120)
Started GET "/users/sign_in" for 49.207.209.194 at 2020-12-01 02:35:29 +0000
Processing by Users::SessionsController#new as HTML
  Rendering users/sessions/new.html.slim within layouts/application
  Rendered users/sessions/new.html.slim within layouts/application (Duration: 0.4ms | Allocations: 75)
  Rendered shared/_header.html.slim (Duration: 0.2ms | Allocations: 41)
Completed 200 OK in 4ms (Views: 2.3ms | Allocations: 1120)

So it was working until then

From rails console i tried:

$ bundle exec rails c
Loading production environment (Rails 6.0.3.4)
irb(main):001:0> Rails.logger
=> #<ActiveSupport::Logger:0x000055d6cbd84cb0 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x000055d6cbd84dc8 @datetime_format=nil>, @formatter=#<Logger::Formatter:0x000055d6cbd84c60 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x000055d6cbd84d78 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @binmode=false, @mon_data=#<Monitor:0x000055d6cbd84d28>, @mon_data_owner_object_id=15740>>
irb(main):002:0> Rails.logger.instance_variable_get("@logdev")
=> #<Logger::LogDevice:0x000055d6cbd84d78 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @binmode=false, @mon_data=#<Monitor:0x000055d6cbd84d28>, @mon_data_owner_object_id=15740>
irb(main):003:0> Rails.logger.instance_variable_get("@logdev").dev
=> #<IO:<STDOUT>>

On /home/deploy/app_name/.rbenv-vars I have the following set:

RAILS_LOG_TO_STDOUT=true
RAILS_SERVE_STATIC_FILES=true
RACK_ENV=production
RAILS_ENV=production

in my production.rb file, inside the application configuration I have the following lines related to logging:

  config.log_level = :debug

  config.log_tags = [ :request_id ]

  config.log_formatter = ::Logger::Formatter.new

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

This has remained virtually unchanged since my first commit, and the logging used to work before

Later I had added the lograge gem (after I realized that logs were not being output) Had added the following inside a new file in config/initializers

require 'lograge/sql/extension'
Rails.application.configure do
  config.lograge.enabled = true
  config.lograge.custom_options = lambda do |event|
    exceptions = %w[controller action]
    {
        params: event.payload[:params].except(*exceptions)
    }
  end
end

Couldnt figure out what was causing empty log files

Update: I even tried deleting the application directory and redeployed using capistrano. It creates a new log file, but writes nothing on it, same as before:

~/app_name/current/log$ ll
total 8
drwxrwxr-x 2 deploy deploy 4096 Jan  7 19:24 ./
drwxrwxr-x 9 deploy deploy 4096 Jan  7 19:07 ../
-rw-rw-r-- 1 deploy deploy    0 Jan  7 19:24 production.log

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...