From 3f9a85a828c690bf5b143ebe82b21a658a09cca5 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 10 Nov 2021 22:45:52 -0600 Subject: [PATCH] Rails: send logs to stderr by default, not stdout. Send all logs to stderr by default instead of stdout. Fixes a problem where parsing the output of sandboxed commands could fail, because they could contain Rails log messages in their stdout. When we run a command in a sandbox, we call fork+exec to run the command in the background so we can capture its output. If Rails prints anything to stdout between the fork and exec calls, then it will be inadvertently captured along with the command's output. This will break parsing of the command's output. This can happen if warning messages are printed by Rails while setting up the sandbox between the fork and exec calls. Writing to stderr is also more correct, since stdout is buffered by default, which means logs could potentially be lost if the process dies unexpectedly before the buffers are flushed. Stderr is unbuffered by default, which means logs will always be output immediately. --- app/logical/danbooru/http/logger.rb | 2 +- config/environments/development.rb | 2 +- config/environments/production.rb | 2 +- config/initializers/delayed_jobs.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/logical/danbooru/http/logger.rb b/app/logical/danbooru/http/logger.rb index b4bb453b8..427e92806 100644 --- a/app/logical/danbooru/http/logger.rb +++ b/app/logical/danbooru/http/logger.rb @@ -5,7 +5,7 @@ module Danbooru attr_reader :logger - def initialize(logger: ::Logger.new(STDOUT)) + def initialize(logger: ::Logger.new(STDERR)) @logger = logger end diff --git a/config/environments/development.rb b/config/environments/development.rb index d0aeaea66..226eaf286 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -64,7 +64,7 @@ Rails.application.configure do # Uncomment if you wish to allow Action Cable access from any origin. # config.action_cable.disable_request_forgery_protection = true - logger = ActiveSupport::Logger.new(STDOUT) + logger = ActiveSupport::Logger.new(STDERR) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) diff --git a/config/environments/production.rb b/config/environments/production.rb index d25c0bb32..dbabb916f 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -76,7 +76,7 @@ Rails.application.configure do # require "syslog/logger" # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') - logger = ActiveSupport::Logger.new(STDOUT) + logger = ActiveSupport::Logger.new(STDERR) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) diff --git a/config/initializers/delayed_jobs.rb b/config/initializers/delayed_jobs.rb index 9137de1d6..7cad7da0c 100644 --- a/config/initializers/delayed_jobs.rb +++ b/config/initializers/delayed_jobs.rb @@ -8,7 +8,7 @@ class DelayedJobTimeoutPlugin < ::Delayed::Plugin end end -Delayed::Worker.logger = Logger.new(STDOUT, level: :debug) +Delayed::Worker.logger = Logger.new(STDERR, level: :debug) Delayed::Worker.default_queue_name = "default" Delayed::Worker.destroy_failed_jobs = false Delayed::Worker.plugins << DelayedJobTimeoutPlugin