Skip to content

Commit

Permalink
FIX: Add compatibility with Rails 7.1+
Browse files Browse the repository at this point in the history
Rails 7.1 introduced a new default logger
(`ActiveSupport::BroadcastLogger`) that responds to a new method named
`#broadcast_to`.

Our current logger isn’t compatible with it and we’re overwriting the
Rails logger. This means that a Rails 7.1 will call `#broadcast_to` on
the logger which will break.

This patch fixes the issue by using the new broadcast logger and adding
our Logster logger to it when inside a Rails 7.1+ app.

For Rails < 7.1, we continue to replace the Rails logger with the
Logster one.
  • Loading branch information
Flink committed Feb 28, 2024
1 parent 94d5143 commit a023d77
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 29 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -10,6 +10,8 @@ jobs:
backend:
runs-on: ubuntu-latest

name: "Ruby ${{ matrix.ruby }} - Rails ${{ matrix.rails }}"

services:
redis:
image: redis
Expand All @@ -18,7 +20,11 @@ jobs:

strategy:
matrix:
ruby: ['2.6', '2.7', '3.0', '3.1']
ruby: ['3.0', '3.1', '3.2', '3.3']
rails: ['6.1', '7.0', '7.1']

env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails }}.gemfile

steps:
- uses: actions/checkout@v3
Expand All @@ -42,9 +48,12 @@ jobs:
- name: Build JS app
run: bash build_client_app.sh

- name: Tests
- name: Tests (no Rails)
run: bundle exec rake test

- name: Tests (Railtie)
run: bundle exec rake test TEST=test/logster/test_railtie.rb

frontend:
runs-on: ubuntu-latest

Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
@@ -1,6 +1,10 @@
# CHANGELOG

- 2024002-19: 2.18.1
- 2024-02-28: 2.19.0

- FIX: Add compatibility with Rails 7.1+

- 2024-02-19: 2.18.1

- UX: backlink in dark mode has a dark background

Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -4,3 +4,7 @@ source "https://rubygems.org"

# Specify your gem's dependencies in rack-log-viewer.gemspec
gemspec

group :development, :test do
gem "rails", ">= 6.1"
end
7 changes: 5 additions & 2 deletions Rakefile
Expand Up @@ -4,15 +4,18 @@ require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |t|
t.pattern = "test/**/test_*.rb"
t.test_files = FileList["test/**/test_*"].exclude(%r{test/logster/test_railtie\.rb})
end

task(default: :test)

desc "Starts Sinatra and Ember servers"
task :client_dev do
begin
pid = spawn("cd website && LOGSTER_ENV=development BUNDLE_GEMFILE=Gemfile bundle exec rackup --host 0.0.0.0")
pid =
spawn(
"cd website && LOGSTER_ENV=development BUNDLE_GEMFILE=Gemfile bundle exec rackup --host 0.0.0.0",
)
pid2 = spawn("cd client-app && npx ember s --proxy http://localhost:9292")
Process.wait pid
Process.wait pid2
Expand Down
9 changes: 9 additions & 0 deletions gemfiles/rails_6.1.gemfile
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"

group :test do
gem "rails", "~> 6.1.0"
end

gemspec path: "../"
9 changes: 9 additions & 0 deletions gemfiles/rails_7.0.gemfile
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"

group :test do
gem "rails", "~> 7.0.0"
end

gemspec path: "../"
9 changes: 9 additions & 0 deletions gemfiles/rails_7.1.gemfile
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"

group :test do
gem "rails", "~> 7.1.0"
end

gemspec path: "../"
2 changes: 1 addition & 1 deletion lib/logster.rb
Expand Up @@ -57,4 +57,4 @@ def self.set_environments(envs)
# check logster/configuration.rb for config options
# Logster.config.environments << :staging

require "logster/rails/railtie" if defined?(::Rails) && ::Rails::VERSION::MAJOR.to_i >= 3
require "logster/rails/railtie" if defined?(::Rails::VERSION) && ::Rails::VERSION::MAJOR.to_i >= 3
4 changes: 1 addition & 3 deletions lib/logster/configuration.rb
Expand Up @@ -46,9 +46,7 @@ def initialize

@allow_grouping = false

if defined?(::Rails) && defined?(::Rails.env) && ::Rails.env.production?
@allow_grouping = true
end
@allow_grouping = true if defined?(::Rails.env) && ::Rails.env.production?
end

def subdirectory
Expand Down
56 changes: 37 additions & 19 deletions lib/logster/rails/railtie.rb
Expand Up @@ -5,32 +5,39 @@ module Logster::Rails
class Engine < Rails::Engine
end

def self.set_logger(config)
return unless Logster.config.environments.include?(Rails.env.to_sym)
class << self
def set_logger(config)
return unless Logster.config.environments.include?(Rails.env.to_sym)

require "logster/middleware/debug_exceptions"
require "logster/middleware/reporter"
require "logster/middleware/debug_exceptions"
require "logster/middleware/reporter"

store = Logster.store ||= Logster::RedisStore.new
store.level = Logger::Severity::WARN if Rails.env.production?
store = Logster.store ||= Logster::RedisStore.new
store.level = Logger::Severity::WARN if Rails.env.production?

if Rails.env.development?
require "logster/defer_logger"
logger = Logster::DeferLogger.new(store)
else
logger = Logster::Logger.new(store)
end
if Rails.env.development?
require "logster/defer_logger"
logger = Logster::DeferLogger.new(store)
else
logger = Logster::Logger.new(store)
end

logger.chain(::Rails.logger)
logger.level = ::Rails.logger.level
logger.level = ::Rails.logger.level

Logster.logger = ::Rails.logger = config.logger = logger
end
Logster.logger = config.logger = logger

def self.initialize!(app)
return unless Logster.config.environments.include?(Rails.env.to_sym)
if rails_71?
::Rails.logger.broadcast_to(logger)
else
logger.chain(::Rails.logger)
::Rails.logger = logger
end
end

def initialize!(app)
return unless Logster.config.environments.include?(Rails.env.to_sym)
return unless logster_enabled?

if Logster::Logger === Rails.logger
if Logster.config.enable_js_error_reporting
app.middleware.insert_before ActionDispatch::ShowExceptions, Logster::Middleware::Reporter
end
Expand All @@ -52,6 +59,17 @@ def self.initialize!(app)
Logster.config.application_version = git_version.strip if git_version.present?
end
end

private

def logster_enabled?
return ::Rails.logger == Logster.logger unless rails_71?
::Rails.logger.broadcasts.include?(Logster.logger)
end

def rails_71?
::Rails.version >= "7.1"
end
end

class Railtie < ::Rails::Railtie
Expand Down
2 changes: 1 addition & 1 deletion lib/logster/version.rb
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Logster
VERSION = "2.18.1"
VERSION = "2.19.0"
end
1 change: 1 addition & 0 deletions logster.gemspec
Expand Up @@ -39,4 +39,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "byebug", "~> 11.1.0"
spec.add_development_dependency "rubocop-discourse", "~> 2.4.1"
spec.add_development_dependency "syntax_tree"
spec.add_development_dependency "sqlite3"
end
31 changes: 31 additions & 0 deletions test/dummy/config/application.rb
@@ -0,0 +1,31 @@
# frozen_string_literal: true
require_relative "boot"

require "active_record/railtie"
require "action_controller/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

require "logster"

module Dummy
class Application < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f

# For compatibility with applications that use this config
config.action_controller.include_all_helpers = false

config.eager_load = false

# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
Logster.set_environments([Rails.env.to_sym])
end
end
6 changes: 6 additions & 0 deletions test/dummy/config/boot.rb
@@ -0,0 +1,6 @@
# frozen_string_literal: true
# Set up gems listed in the Gemfile.
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__)

require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__)
6 changes: 6 additions & 0 deletions test/dummy/config/environment.rb
@@ -0,0 +1,6 @@
# frozen_string_literal: true
# Load the Rails application.
require_relative "application"

# Initialize the Rails application.
Rails.application.initialize!
21 changes: 21 additions & 0 deletions test/logster/test_railtie.rb
@@ -0,0 +1,21 @@
# frozen_string_literal: true

ENV["RAILS_ENV"] = "test"

require "redis"
require_relative "../dummy/config/environment"
ActiveRecord::Migrator.migrations_paths = [File.expand_path("dummy/db/migrate", __dir__)]

require_relative "../test_helper"

class TestRailtie < Minitest::Test
def test_sets_logger
refute_nil Logster.logger

if Rails.version >= "7.1"
assert_includes Rails.logger.broadcasts, Logster.logger
else
assert_equal Rails.logger, Logster.logger
end
end
end

0 comments on commit a023d77

Please sign in to comment.