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

Story 18706 update to new ci standard #77

Merged
merged 5 commits into from
Jul 10, 2024
Merged
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
9 changes: 5 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [2.7, '3.0', 3.1, 3.2]
ruby: [3.1, 3.2, 3.3]
gemfile:
- Gemfile
- gemfiles/rails_6.gemfile
- gemfiles/rails_7_0.gemfile
# - gemfiles/rails_7_1.gemfile
- gemfiles/activesupport_6_0.gemfile
- gemfiles/activesupport_6_1.gemfile
- gemfiles/activesupport_7_0.gemfile
- gemfiles/activesupport_7_1.gemfile
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.5
3.1.6
8 changes: 2 additions & 6 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

appraise 'rails-6' do
gem 'activesupport', '~> 6.0'
end
require 'appraisal/matrix'

appraise 'rails-7' do
gem 'activesupport', '~> 7.0'
end
appraisal_matrix(activesupport: "6.0")
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.0] - 2024-07-10
### Added
- Added support for `activesupport` 7.1 by providing a mixin for extending Broadcast loggers.

## [1.3.0] 2023-12-08
### Added
- `ContextualLogger::LoggerMixin#redact` method to expose the existing redaction logic outside of just logs.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ gemspec

gem 'coveralls', require: false

gem 'activesupport'
gem 'appraisal'
gem 'appraisal-matrix'
gem 'bump', '~> 0.6.1'
gem 'pry'
gem 'rake'
Expand Down
30 changes: 21 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
PATH
remote: .
specs:
contextual_logger (1.3.0)
activesupport (< 7.1)
contextual_logger (1.4.0)
activesupport (>= 6.0)
json

GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.7.2)
activesupport (7.1.3.4)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
appraisal (2.5.0)
bundler
rake
thor (>= 0.14.0)
appraisal-matrix (0.2.0)
appraisal (~> 2.2)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
bump (0.6.1)
coderay (1.1.3)
concurrent-ruby (1.2.2)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
coveralls (0.8.23)
json (>= 1.8, < 3)
simplecov (~> 0.16.1)
Expand All @@ -29,11 +39,13 @@ GEM
tins (~> 1.6)
diff-lcs (1.5.0)
docile (1.4.0)
i18n (1.14.1)
drb (2.2.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
json (2.6.3)
method_source (1.0.0)
minitest (5.19.0)
minitest (5.24.1)
mutex_m (0.2.0)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand All @@ -44,7 +56,7 @@ GEM
method_source (~> 1.0)
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
rake (13.2.1)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand Down Expand Up @@ -81,7 +93,7 @@ GEM
sync (0.5.0)
term-ansicolor (1.7.1)
tins (~> 1.0)
thor (1.2.2)
thor (1.3.1)
tins (1.32.1)
sync
tzinfo (2.0.6)
Expand All @@ -92,8 +104,8 @@ PLATFORMS
ruby

DEPENDENCIES
activesupport
appraisal
appraisal-matrix
bump (~> 0.6.1)
contextual_logger!
coveralls
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ end

contextual_logger = ApplicationLogger.new(STDOUT)
```
### Initialization of an ActiveSupport::BroadcastLogger
ActiveSupport 7.1 added a BroadcastLogger class. In order to broadcast logs including context, you need to `extend` the BroadcastLogger instance with a separate mixin:
```ruby
logger = Logger.new(STDOUT).extend(ContextualLogger::LoggerMixin)
other_logger = Logger.new(other_log_stream).extend(ContextualLogger::LoggerMixin)
broadcast_logger = ActiveSupport::BroadcastLogger.new(logger, console_logger).extend(ContextualLogger::BroadcastLoggerMixin)
```
All loggers being passed to the BroadcastLogger must also be contextual loggers.

## Ways to Set Context
Context may be provided any of 3 ways. Typically, all 3 will be used together.

Expand Down
2 changes: 1 addition & 1 deletion contextual_logger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
}

spec.add_dependency 'json'
spec.add_dependency 'activesupport', '< 7.1'
spec.add_dependency 'activesupport', ">= 6.0"
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This file was generated by Appraisal

source "http://rubygems.org"
source "https://rubygems.org"

gem "coveralls", require: false
gem "activesupport", "~> 6.0"
gem "appraisal"
gem "appraisal-matrix", "0.1.0"
gem "bump", "~> 0.6.1"
gem "pry"
gem "rake"
Expand All @@ -14,5 +14,6 @@ gem "ruby-prof"
gem "ruby-prof-flamegraph"
gem "rspec"
gem "rspec_junit_formatter"
gem "activesupport", "~> 6.0.0"

gemspec path: "../"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This file was generated by Appraisal

source "http://rubygems.org"
source "https://rubygems.org"

gem "coveralls", require: false
gem "activesupport", "~> 7.1"
gem "appraisal"
gem "appraisal-matrix", "0.1.0"
gem "bump", "~> 0.6.1"
gem "pry"
gem "rake"
Expand All @@ -14,5 +14,6 @@ gem "ruby-prof"
gem "ruby-prof-flamegraph"
gem "rspec"
gem "rspec_junit_formatter"
gem "activesupport", "~> 6.1.0"

gemspec path: "../"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This file was generated by Appraisal

source "http://rubygems.org"
source "https://rubygems.org"

gem "coveralls", require: false
gem "activesupport", "~> 7.0.0"
gem "appraisal"
gem "appraisal-matrix", "0.1.0"
gem "bump", "~> 0.6.1"
gem "pry"
gem "rake"
Expand All @@ -14,5 +14,6 @@ gem "ruby-prof"
gem "ruby-prof-flamegraph"
gem "rspec"
gem "rspec_junit_formatter"
gem "activesupport", "~> 7.0.0"

gemspec path: "../"
19 changes: 19 additions & 0 deletions gemfiles/activesupport_7_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "coveralls", require: false
gem "appraisal"
gem "appraisal-matrix", "0.1.0"
gem "bump", "~> 0.6.1"
gem "pry"
gem "rake"
gem "rubocop", "0.54.0"
gem "rubocop-git"
gem "ruby-prof"
gem "ruby-prof-flamegraph"
gem "rspec"
gem "rspec_junit_formatter"
gem "activesupport", "~> 7.1.0"

gemspec path: "../"
12 changes: 12 additions & 0 deletions lib/contextual_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,16 @@ def deep_merge_with_current_context(stacked_context)
end
end
end

module BroadcastLoggerMixin
method_names = LOG_LEVEL_NAMES_TO_SEVERITY.keys << :add

method_names.each do |method_name|
class_eval(<<~EOS, __FILE__, __LINE__ + 1)
def #{method_name}(...)
dispatch { |logger| logger.#{method_name}(...) }
end
EOS
end
end
end
2 changes: 1 addition & 1 deletion lib/contextual_logger/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ContextualLogger
VERSION = '1.3.0'
VERSION = '1.4.0'
end
42 changes: 31 additions & 11 deletions spec/lib/contextual_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,54 @@ def expect_log_line_to_be_written(log_line)
describe 'when used with ActiveSupport::Logger.broadcast' do
let(:log_level) { Logger::Severity::ERROR }
let(:console_log_stream) { StringIO.new }
let(:console_logger) { Logger.new(console_log_stream) }
let(:console_logger) do
if ::ActiveSupport::VERSION::STRING < "7.1"
Logger.new(console_log_stream)
else
Logger.new(console_log_stream).extend(ContextualLogger::LoggerMixin)
end
end
let(:broadcast_logger) do
if ::ActiveSupport::VERSION::STRING < "7.1"
logger.extend(::ActiveSupport::Logger.broadcast(console_logger))
else
ActiveSupport::BroadcastLogger.new(logger, console_logger)
ActiveSupport::BroadcastLogger.new(logger, console_logger).extend(ContextualLogger::BroadcastLoggerMixin)
end
end

it 'properly broadcasts to both logs when level-named method is called' do
log_at_every_level(broadcast_logger, service: 'test_service')
expect(log_message_levels).to eq(["error", "fatal", "unknown"])
# note: context lands in `progname` arg
expect(console_log_stream.string.gsub(/\[[^\]]+\]/, '[]')).to eq(<<~EOS)
D, [] DEBUG -- {:service=>\"test_service\"}: debug message
I, [] INFO -- {:service=>\"test_service\"}: info message
W, [] WARN -- {:service=>\"test_service\"}: warn message
E, [] ERROR -- {:service=>\"test_service\"}: error message
F, [] FATAL -- {:service=>\"test_service\"}: fatal message
A, [] ANY -- {:service=>\"test_service\"}: unknown message
EOS
if ::ActiveSupport::VERSION::STRING < "7.1"
expect(console_log_stream.string.gsub(/\[[^\]]+\]/, '[]')).to eq(<<~EOS)
D, [] DEBUG -- {:service=>\"test_service\"}: debug message
I, [] INFO -- {:service=>\"test_service\"}: info message
W, [] WARN -- {:service=>\"test_service\"}: warn message
E, [] ERROR -- {:service=>\"test_service\"}: error message
F, [] FATAL -- {:service=>\"test_service\"}: fatal message
A, [] ANY -- {:service=>\"test_service\"}: unknown message
EOS
else
expect(log_stream.string.gsub(/"timestamp":"[^"]+"/, '<time>')).to eq(<<~EOS)
{"message":"error message","severity":"ERROR",<time>,"service":"test_service"}
{"message":"fatal message","severity":"FATAL",<time>,"service":"test_service"}
{"message":"unknown message","severity":"ANY",<time>,"service":"test_service"}
EOS
end
end

it 'properly broadcasts to both logs when add is called' do
broadcast_logger.add(Logger::Severity::ERROR, "error message", service: 'test_service')
expect(log_message_levels).to eq(["error"])
# note: context lands in `progname` arg
expect(console_log_stream.string.gsub(/\[[^\]]+\]/, '[]')).to eq("E, [] ERROR -- {:service=>\"test_service\"}: error message\n")
if ::ActiveSupport::VERSION::STRING < "7.1"
expect(console_log_stream.string.gsub(/\[[^\]]+\]/, '[]')).to eq("E, [] ERROR -- {:service=>\"test_service\"}: error message\n")
else
expect(console_log_stream.string.gsub(/"timestamp":"[^"]+"/, '<time>')).to eq(<<~EOS)
{"message":"error message","severity":"ERROR",<time>,"service":"test_service"}
EOS
end
end
end

Expand Down
Loading