Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dev initializer logs not going to stdout #46170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,10 @@ class Railtie < Rails::Railtie # :nodoc:

# When loading console, force ActiveRecord::Base to be loaded
# to avoid cross references when loading a constant for the
# first time. Also, make it output to STDERR.
# first time.
console do |app|
require "active_record/railties/console_sandbox" if app.sandbox?
require "active_record/base"
unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDERR, STDOUT)
console = ActiveSupport::Logger.new(STDERR)
console.level = Rails.logger.level
Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger, console)
end
ActiveRecord.verbose_query_logs = false
end

Expand Down
8 changes: 8 additions & 0 deletions railties/lib/rails/application/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ module Bootstrap
unless config.consider_all_requests_local
Rails.error.logger = Rails.logger
end

if config.log_to_stdout? && !ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDOUT, STDERR)
console = ActiveSupport::Logger.new(STDOUT)
console.formatter = Rails.logger.formatter
console.level = Rails.logger.level

Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger, console)
end
end

# Initialize cache early in the stack so railties can make use of it.
Expand Down
9 changes: 9 additions & 0 deletions railties/lib/rails/application/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def initialize(*)
@rake_eager_load = false
@server_timing = false
@dom_testing_default_html_version = :html4
@log_to_stdout = false
end

# Loads default configuration values for a target version. This includes
Expand Down Expand Up @@ -553,6 +554,14 @@ def permissions_policy(&block)
end
end

def log_to_stdout! # :nodoc:
@log_to_stdout = true
end

def log_to_stdout? # :nodoc:
@log_to_stdout
end

def default_log_file
path = paths["log"].first
unless File.exist? File.dirname path
Expand Down
4 changes: 3 additions & 1 deletion railties/lib/rails/commands/console/console_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def initialize(args = [], local_options = {}, config = {})

desc "console", "Start the Rails console"
def perform
boot_application!
require_application!
Rails.configuration.log_to_stdout!
Rails.application.require_environment! if defined?(APP_PATH)
Rails::Console.start(Rails.application, options)
end
end
Expand Down
14 changes: 1 addition & 13 deletions railties/lib/rails/commands/server/server_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def start(after_stop_callback = nil)
trap(:INT) { exit }
create_tmp_directories
setup_dev_caching
log_to_stdout if options[:log_stdout]
Rails.configuration.log_to_stdout! if options[:log_stdout]

super()
ensure
Expand Down Expand Up @@ -72,18 +72,6 @@ def create_tmp_directories
end
end

def log_to_stdout
wrapped_app # touch the app so the logger is set up
Copy link
Member

@p8 p8 Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure where this method is defined and why it's no longer necessary. Any idea? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question!

wrapped_app comes from Rack::Server here: https://github.com/rack/rack/blob/abca7d59c566320f1b60d1f5224beac9d201fa3b/lib/rack/server.rb#L421

Rack calls wrapped_app on its own here: https://github.com/rack/rack/blob/abca7d59c566320f1b60d1f5224beac9d201fa3b/lib/rack/server.rb#L327

However, if you want to modify the application (like making the logger broadcast), then you have to "touch the app". All this does is load the app a bit earlier than Rack would normally so that things like initializers have a change to run first.


console = ActiveSupport::Logger.new(STDOUT)
console.formatter = Rails.logger.formatter
console.level = Rails.logger.level

unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDERR, STDOUT)
Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger, console)
end
end

def use_puma?
server.to_s.end_with?("Handler::Puma")
end
Expand Down