From a8e430976a00dfa17238c60f543ce5bbb4b707f9 Mon Sep 17 00:00:00 2001
From: Daan Davidsz <daan@roqua.nl>
Date: Thu, 20 Sep 2018 09:51:21 +0000
Subject: [PATCH] Configure RoQua logger

---
 .gitlab-ci.yml                                | 15 ++++---
 Appraisals                                    | 14 +++----
 Gemfile                                       |  1 +
 Gemfile.lock                                  |  6 ++-
 gemfiles/rails42.gemfile                      |  4 +-
 gemfiles/rails50.gemfile                      |  4 +-
 gemfiles/rails51.gemfile                      |  4 +-
 gemfiles/{rails41.gemfile => rails52.gemfile} |  4 +-
 lib/roqua/logging/roqua_logging_railtie.rb    | 13 ++++++
 lib/roqua/support.rb                          |  1 +
 .../logging/roqua_logging_railtie_spec.rb     | 42 +++++++++++++++++++
 11 files changed, 90 insertions(+), 18 deletions(-)
 rename gemfiles/{rails41.gemfile => rails52.gemfile} (81%)
 create mode 100644 lib/roqua/logging/roqua_logging_railtie.rb
 create mode 100644 spec/roqua/logging/roqua_logging_railtie_spec.rb

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7d18e2c..b8c6e60 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -21,11 +21,6 @@ before_script:
     paths:
       - .gems
 
-rails_41_ruby_23:
-  <<: *ruby_23
-  script:
-    - bundle exec appraisal rails41 bundle exec rspec
-
 rails_42_ruby_23:
   <<: *ruby_23
   script:
@@ -50,3 +45,13 @@ rails_51_ruby_24:
   <<: *ruby_24
   script:
     - bundle exec appraisal rails51 bundle exec rspec
+
+rails_52_ruby_23:
+  <<: *ruby_23
+  script:
+    - bundle exec appraisal rails52 bundle exec rspec
+
+rails_52_ruby_24:
+  <<: *ruby_24
+  script:
+    - bundle exec appraisal rails52 bundle exec rspec
diff --git a/Appraisals b/Appraisals
index ccca3a0..b6382fa 100644
--- a/Appraisals
+++ b/Appraisals
@@ -1,15 +1,15 @@
-appraise "rails41" do
-  gem "activesupport", "4.1"
-end
-
 appraise "rails42" do
-  gem "activesupport", "4.2"
+  gem "activesupport", "~> 4.2.10"
 end
 
 appraise "rails50" do
-  gem "activesupport", "5.0"
+  gem "activesupport", "~> 5.0.0"
 end
 
 appraise "rails51" do
-  gem "activesupport", "5.1"
+  gem "activesupport", "~> 5.1.0"
+end
+
+appraise "rails52" do
+  gem "activesupport", "~> 5.2.0"
 end
diff --git a/Gemfile b/Gemfile
index 8544661..deb8331 100644
--- a/Gemfile
+++ b/Gemfile
@@ -10,6 +10,7 @@ group :test do
   gem 'actionpack', '>= 4.0'
   gem 'active_interaction', '~> 3.0'
   gem 'appsignal'
+  gem 'climate_control' # For ENV modification in specs
   gem 'combustion', '~> 0.5.2'
   gem 'fakefs', require: 'fakefs/safe'
   gem 'guard-rspec', '~> 4.2.6'
diff --git a/Gemfile.lock b/Gemfile.lock
index 25e647b..01e77c3 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -8,7 +8,7 @@ GIT
 PATH
   remote: .
   specs:
-    roqua-support (0.1.31)
+    roqua-support (0.1.32)
       active_interaction (~> 3.0)
       activesupport (>= 3.2, < 6)
       naught (~> 1.0)
@@ -52,6 +52,7 @@ GEM
     arel (7.1.4)
     ast (2.3.0)
     builder (3.2.3)
+    climate_control (0.2.0)
     coderay (1.1.1)
     combustion (0.5.5)
       activesupport (>= 3.0.0)
@@ -182,6 +183,7 @@ DEPENDENCIES
   appraisal
   appsignal
   bundler (~> 1.0)
+  climate_control
   combustion (~> 0.5.2)
   delayed_job_active_record
   fakefs
@@ -197,4 +199,4 @@ DEPENDENCIES
   timecop
 
 BUNDLED WITH
-   1.16.2
+   1.16.4
diff --git a/gemfiles/rails42.gemfile b/gemfiles/rails42.gemfile
index b010e36..45d357b 100644
--- a/gemfiles/rails42.gemfile
+++ b/gemfiles/rails42.gemfile
@@ -4,13 +4,15 @@ source "https://rubygems.org"
 
 gem "appraisal"
 gem "roqua_styleguide", :git => "https://gitlab.roqua.nl/roqua/styleguide.git"
-gem "activesupport", "4.2"
+gem "activesupport", "~> 4.2.10"
 
 group :test do
   gem "actionpack", ">= 4.0"
   gem "active_interaction", "~> 3.0"
   gem "appsignal"
+  gem "climate_control"
   gem "combustion", "~> 0.5.2"
+  gem "fakefs", :require => "fakefs/safe"
   gem "guard-rspec", "~> 4.2.6"
   gem "responders"
   gem "rspec-instrumentation-matcher"
diff --git a/gemfiles/rails50.gemfile b/gemfiles/rails50.gemfile
index c8d09da..ab46985 100644
--- a/gemfiles/rails50.gemfile
+++ b/gemfiles/rails50.gemfile
@@ -4,13 +4,15 @@ source "https://rubygems.org"
 
 gem "appraisal"
 gem "roqua_styleguide", :git => "https://gitlab.roqua.nl/roqua/styleguide.git"
-gem "activesupport", "5.0"
+gem "activesupport", "~> 5.0.0"
 
 group :test do
   gem "actionpack", ">= 4.0"
   gem "active_interaction", "~> 3.0"
   gem "appsignal"
+  gem "climate_control"
   gem "combustion", "~> 0.5.2"
+  gem "fakefs", :require => "fakefs/safe"
   gem "guard-rspec", "~> 4.2.6"
   gem "responders"
   gem "rspec-instrumentation-matcher"
diff --git a/gemfiles/rails51.gemfile b/gemfiles/rails51.gemfile
index c4e6b8a..585d87f 100644
--- a/gemfiles/rails51.gemfile
+++ b/gemfiles/rails51.gemfile
@@ -4,13 +4,15 @@ source "https://rubygems.org"
 
 gem "appraisal"
 gem "roqua_styleguide", :git => "https://gitlab.roqua.nl/roqua/styleguide.git"
-gem "activesupport", "5.1"
+gem "activesupport", "~> 5.1.0"
 
 group :test do
   gem "actionpack", ">= 4.0"
   gem "active_interaction", "~> 3.0"
   gem "appsignal"
+  gem "climate_control"
   gem "combustion", "~> 0.5.2"
+  gem "fakefs", :require => "fakefs/safe"
   gem "guard-rspec", "~> 4.2.6"
   gem "responders"
   gem "rspec-instrumentation-matcher"
diff --git a/gemfiles/rails41.gemfile b/gemfiles/rails52.gemfile
similarity index 81%
rename from gemfiles/rails41.gemfile
rename to gemfiles/rails52.gemfile
index dfe8d29..64c8fa7 100644
--- a/gemfiles/rails41.gemfile
+++ b/gemfiles/rails52.gemfile
@@ -4,13 +4,15 @@ source "https://rubygems.org"
 
 gem "appraisal"
 gem "roqua_styleguide", :git => "https://gitlab.roqua.nl/roqua/styleguide.git"
-gem "activesupport", "4.1"
+gem "activesupport", "~> 5.2.0"
 
 group :test do
   gem "actionpack", ">= 4.0"
   gem "active_interaction", "~> 3.0"
   gem "appsignal"
+  gem "climate_control"
   gem "combustion", "~> 0.5.2"
+  gem "fakefs", :require => "fakefs/safe"
   gem "guard-rspec", "~> 4.2.6"
   gem "responders"
   gem "rspec-instrumentation-matcher"
diff --git a/lib/roqua/logging/roqua_logging_railtie.rb b/lib/roqua/logging/roqua_logging_railtie.rb
new file mode 100644
index 0000000..15752d9
--- /dev/null
+++ b/lib/roqua/logging/roqua_logging_railtie.rb
@@ -0,0 +1,13 @@
+class RoquaLoggingRailtie < Rails::Railtie
+  initializer 'roqua_logging_railtie.configure_roqua_logging' do
+    RoquaLoggingRailtie.configure if ENV['RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER'].present?
+  end
+
+  def self.configure
+    Roqua.logger = ActiveSupport::Logger.new(STDOUT)
+    Roqua.logger.logger.formatter = Logger::Formatter.new
+
+    require 'roqua/support/request_logger'
+    Roqua::Support::RequestLogger.attach_to :action_controller
+  end
+end
diff --git a/lib/roqua/support.rb b/lib/roqua/support.rb
index 879cab8..65c9204 100644
--- a/lib/roqua/support.rb
+++ b/lib/roqua/support.rb
@@ -1,4 +1,5 @@
 require 'logger'
+require 'roqua/logging/roqua_logging_railtie'
 require 'roqua/support/instrumentation'
 require 'roqua/support/log_wrapper'
 require 'roqua/support/errors'
diff --git a/spec/roqua/logging/roqua_logging_railtie_spec.rb b/spec/roqua/logging/roqua_logging_railtie_spec.rb
new file mode 100644
index 0000000..bb0923a
--- /dev/null
+++ b/spec/roqua/logging/roqua_logging_railtie_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+require 'roqua/support/request_logger'
+require 'roqua/logging/roqua_logging_railtie'
+
+describe RoquaLoggingRailtie do
+  let(:initializer_key) { 'roqua_logging_railtie.configure_roqua_logging' }
+
+  subject(:initializer) do
+    Rails.application.initializers.select { |i| i.name == initializer_key }.first
+  end
+
+  it 'loads the initializer' do
+    expect(Rails.application.initializers.map(&:name)).to include(initializer_key)
+  end
+
+  it 'attaches Roqua::Support::RequestLogger to action_controller' do
+    expect(Roqua::Support::RequestLogger).to receive(:attach_to).with(:action_controller)
+    RoquaLoggingRailtie.configure
+  end
+
+  it 'logs to STDOUT' do
+    RoquaLoggingRailtie.configure
+    expect(ActiveSupport::Logger.logger_outputs_to?(Roqua.logger.logger, STDOUT)).to be_truthy
+  end
+
+  it 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is blank' do
+    expect(RoquaLoggingRailtie).to_not receive(:configure)
+
+    ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: '' do
+      initializer.block.call
+    end
+  end
+
+  it 'when RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER is present' do
+    expect(RoquaLoggingRailtie).to receive(:configure)
+
+    ClimateControl.modify RAILS_LOG_TO_STDOUT_USING_ROQUA_LOGGER: 'true' do
+      initializer.block.call
+    end
+  end
+end
-- 
GitLab