mr_loga_loga 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e845c29baf41dd825382341572e40afa84fcd65dac1373bd59ba5f973903b83
4
- data.tar.gz: 033c3b960404c5661fe1c0d2a198da5c4cee99b7aeeb1cacc47f6191594ff24d
3
+ metadata.gz: a3823df8fc816d6e6cc3bf7ce5a2457b06276a2e59fbb7985775bdd55c78b255
4
+ data.tar.gz: f6d410e9babfdf7d22197016c5d8f4e14f4ee2b4ba99df2e62e46a35e70f2f6d
5
5
  SHA512:
6
- metadata.gz: f69fb74275963243bb371c2ed23003a5cc708d684723e60bff89c92677165143ac0092e79d548f8832fd0b3d3b34cbf840dc0be97e3c3a4325f47c5780892619
7
- data.tar.gz: 10f59c45ff3b10909708444c46ec226bacbe560f7ded4e591983ca1f4f156a8bb5c7fd0aad85f51c68e7c07fcda0fb10566c0fcdec0c13cdd44c628a9e251c9c
6
+ metadata.gz: aa03cbe1efaf3bd72b2bedb93b8f759ac826e7350a3f62bbe238d5167cb16e50db855acd19341c6a77f32487ae953c5c9ade031997cadd1539f38d222b5f5c18
7
+ data.tar.gz: 3d3d98af06b08d3f0850e06ff1bb50f3573e30218746025989e4c71d3dfcf46f164f86724ad50edc9ce546ebe7a765059bc17ae2ac1cc0cb4d384a59c0f6314d
@@ -16,7 +16,7 @@ jobs:
16
16
  fail-fast: false
17
17
 
18
18
  steps:
19
- - uses: actions/checkout@v2
19
+ - uses: actions/checkout@v3
20
20
 
21
21
  - name: Set up Ruby ${{ matrix.ruby-version }}
22
22
  uses: ruby/setup-ruby@v1
@@ -41,7 +41,7 @@ jobs:
41
41
  lint:
42
42
  runs-on: ubuntu-latest
43
43
  steps:
44
- - uses: actions/checkout@v2
44
+ - uses: actions/checkout@v3
45
45
  with:
46
46
  fetch-depth: 0
47
47
 
@@ -17,7 +17,7 @@ jobs:
17
17
  version: ${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}.${{ steps.release.outputs.patch }}
18
18
 
19
19
  steps:
20
- - uses: actions/checkout@v2
20
+ - uses: actions/checkout@v3
21
21
  - name: Generate Release 🗒️
22
22
  uses: GoogleCloudPlatform/release-please-action@v3
23
23
  id: release
@@ -33,7 +33,7 @@ jobs:
33
33
  if: needs.prepare-release.outputs.release_created || github.event_name == 'workflow_dispatch'
34
34
 
35
35
  steps:
36
- - uses: actions/checkout@v2
36
+ - uses: actions/checkout@v3
37
37
 
38
38
  - name: Set up Ruby
39
39
  uses: ruby/setup-ruby@v1
@@ -73,7 +73,7 @@ jobs:
73
73
  RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
74
74
 
75
75
  - name: Commit Updated Files 📤
76
- uses: EndBug/add-and-commit@v8.0.1
76
+ uses: EndBug/add-and-commit@v9
77
77
  with:
78
78
  add: "['Gemfile.lock', 'lib/mr_loga_loga/version.rb']"
79
79
  message: "chore: post-release ${{ steps.release-version.outputs.version }}"
data/.tool-versions CHANGED
@@ -1 +1 @@
1
- ruby 3.0.0
1
+ ruby 3.1.1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0](https://github.com/hschne/mr-loga-loga/compare/v0.1.5...v0.2.0) (2022-05-06)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * improve compatibility by changing signature (#20)
9
+
10
+ ### Features
11
+
12
+ * improve compatibility by changing signature ([#20](https://github.com/hschne/mr-loga-loga/issues/20)) ([01e6f0f](https://github.com/hschne/mr-loga-loga/commit/01e6f0fd0482c141117a71b78ba008e97c63d808))
13
+
3
14
  ### [0.1.5](https://github.com/hschne/mr-loga-loga/compare/v0.1.4...v0.1.5) (2022-01-25)
4
15
 
5
16
 
data/Gemfile CHANGED
@@ -7,6 +7,6 @@ gemspec
7
7
 
8
8
  gem 'rake', '~> 13.0'
9
9
 
10
- gem 'rspec', '~> 3.0'
10
+ gem 'rspec', '~> 3.11'
11
11
 
12
- gem 'rubocop', '~> 1.25'
12
+ gem 'rubocop', '~> 1.28'
data/Gemfile.lock CHANGED
@@ -1,45 +1,45 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mr_loga_loga (0.1.5)
4
+ mr_loga_loga (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
- diff-lcs (1.4.4)
10
+ diff-lcs (1.5.0)
11
11
  docile (1.4.0)
12
- parallel (1.21.0)
13
- parser (3.1.0.0)
12
+ parallel (1.22.1)
13
+ parser (3.1.2.0)
14
14
  ast (~> 2.4.1)
15
15
  rainbow (3.1.1)
16
16
  rake (13.0.6)
17
- regexp_parser (2.2.0)
17
+ regexp_parser (2.3.1)
18
18
  rexml (3.2.5)
19
- rspec (3.10.0)
20
- rspec-core (~> 3.10.0)
21
- rspec-expectations (~> 3.10.0)
22
- rspec-mocks (~> 3.10.0)
23
- rspec-core (3.10.1)
24
- rspec-support (~> 3.10.0)
25
- rspec-expectations (3.10.1)
19
+ rspec (3.11.0)
20
+ rspec-core (~> 3.11.0)
21
+ rspec-expectations (~> 3.11.0)
22
+ rspec-mocks (~> 3.11.0)
23
+ rspec-core (3.11.0)
24
+ rspec-support (~> 3.11.0)
25
+ rspec-expectations (3.11.0)
26
26
  diff-lcs (>= 1.2.0, < 2.0)
27
- rspec-support (~> 3.10.0)
28
- rspec-mocks (3.10.2)
27
+ rspec-support (~> 3.11.0)
28
+ rspec-mocks (3.11.0)
29
29
  diff-lcs (>= 1.2.0, < 2.0)
30
- rspec-support (~> 3.10.0)
31
- rspec-support (3.10.3)
32
- rubocop (1.25.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-support (3.11.0)
32
+ rubocop (1.28.2)
33
33
  parallel (~> 1.10)
34
34
  parser (>= 3.1.0.0)
35
35
  rainbow (>= 2.2.2, < 4.0)
36
36
  regexp_parser (>= 1.8, < 3.0)
37
37
  rexml
38
- rubocop-ast (>= 1.15.1, < 2.0)
38
+ rubocop-ast (>= 1.17.0, < 2.0)
39
39
  ruby-progressbar (~> 1.7)
40
40
  unicode-display_width (>= 1.4.0, < 3.0)
41
- rubocop-ast (1.15.1)
42
- parser (>= 3.0.1.1)
41
+ rubocop-ast (1.17.0)
42
+ parser (>= 3.1.1.0)
43
43
  ruby-progressbar (1.11.0)
44
44
  simplecov (0.21.2)
45
45
  docile (~> 1.1)
@@ -59,11 +59,11 @@ PLATFORMS
59
59
  DEPENDENCIES
60
60
  mr_loga_loga!
61
61
  rake (~> 13.0)
62
- rspec (~> 3.0)
63
- rubocop (~> 1.25)
62
+ rspec (~> 3.11)
63
+ rubocop (~> 1.28)
64
64
  simplecov (~> 0.21)
65
65
  timecop (~> 0.9)
66
66
  yard (~> 0.9)
67
67
 
68
68
  BUNDLED WITH
69
- 2.2.26
69
+ 2.3.13
data/README.md CHANGED
@@ -138,28 +138,57 @@ end
138
138
  MrLogaLoga::Logger.new(STDOUT, formatter: MyFormatter.new)
139
139
  ```
140
140
 
141
+ ## Usage with Other Gems
142
+
143
+ This section describes how you can use MrLogaLoga together with other well-known gems.
144
+
141
145
  ### Rails
142
146
 
143
147
  Using MrLogaLoga in Ruby on Rails is straightforward. Set up MrLogaLoga as logger in your `application.rb` or environment files and you are off to the races:
144
148
 
145
149
  ```ruby
146
150
  # application.rb
147
- config.logger = MrLogaLoga::Logger.new(STDOUT)
148
- config.log_level = :info
151
+ config.log_formatter = MrLogaLoga::Formatters::KeyValue.new
152
+ config.logger = MrLogaLoga::Logger.new($stdout, formatter: config.log_formatter)
149
153
  ```
150
154
 
151
155
  Note that setting `config.log_formatter` does not work. You must set the formatter in the logger constructor as described in [Formatters](#formatters).
152
156
 
153
157
  ### Lograge
154
158
 
155
- [LogRage](https://github.com/roidrage/lograge) and MrLogaLoga work well together. When using both gems Lograge will be patched so that data will be available as `context` in MrLogaLoga. Make sure that MrLogaLoga is required **after** Lograge:
159
+ [LogRage](https://github.com/roidrage/lograge) and MrLogaLoga work well together. You must set lograge to use the raw formatter, however:
156
160
 
157
161
  ```ruby
158
- gem 'lograge'
159
- gem 'mr_loga_loga'
162
+ Rails.application.configure do
163
+ config.lograge.enabled = true
164
+ config.lograge.formatter = Lograge::Formatters::Raw.new
165
+ end
166
+ ```
167
+
168
+
169
+
170
+ ### Sidekiq
171
+
172
+ You can use MrLogaLoga with [Sidekiq](https://github.com/mperham/sidekiq) by configuring it like so:
173
+
174
+ ```ruby
175
+ Sidekiq.configure_server do |config|
176
+ config.logger = MrLogaLoga::Logger.new($stdout, formatter: MrLogaLoga::Formatters::Json.new)
177
+ config.logger.level = Logger::INFO
178
+
179
+ # Remove existing error handlers to avoid double logging
180
+ config.error_handlers.clear
181
+ config.error_handlers << proc { |ex, context| Sidekiq.logger.warn(ex, context[:job]) }
182
+ end
160
183
  ```
161
184
 
162
- Note that Lograge's formatters won't be used. Use MrLogaLoga's own [formatters](#formatters) instead.
185
+ If you want to use MrLogaLoga's helper methods in your workers you must include `MrLogaLoga` after including `Sidekiq`:
186
+ ```ruby
187
+ class MyWorker
188
+ include Sidekiq::Worker
189
+ include MrLogaLoga
190
+ end
191
+ ```
163
192
 
164
193
  ## Why MrLogaLoga?
165
194
 
@@ -175,7 +204,7 @@ MrLogaLoga addresses this by allowing you to attach contextual information to yo
175
204
 
176
205
  ## Credit
177
206
 
178
- This little library was inspired by [Lograge](https://github.com/roidrage/lograge) first and foremost. I would like to thank the amazing [@LenaSchnedlitz](https://twitter.com/LenaSchnedlitz) for the incredible logo! 🤩
207
+ This little library was inspired by [Lograge](https://github.com/roidrage/lograge) first and foremost. Some inspiration was taken from [ougai](https://github.com/tilfin/ougai). I would like to thank the amazing [@LenaSchnedlitz](https://twitter.com/LenaSchnedlitz) for the incredible logo! 🤩
179
208
 
180
209
  ## Development
181
210
 
@@ -28,26 +28,29 @@ module MrLogaLoga
28
28
  # @param context [Hash] the new context
29
29
  # @yield the new context
30
30
  # @return [Context] a new context object
31
- def add(severity, message = nil, **context, &block)
31
+ def add(severity, message = nil, context = nil, progname = nil, &block)
32
32
  severity ||= UNKNOWN
33
33
  return true unless @logger.log?(severity)
34
34
 
35
+ message, context = LoggerData.build(message, context, &block)
35
36
  context = merge_context(@context, context)
36
37
  context = context.call if context.is_a?(Proc)
37
38
 
38
- @logger.add(severity, message, **context, &block)
39
+ @logger.add(severity, LogMessage.new(message, context), progname, &block)
39
40
  end
40
41
 
41
42
  alias log add
42
43
 
43
44
  %i[debug info warn error fatal unknown].each do |symbol|
44
- define_method(symbol) do |message = nil, **context, &block|
45
+ define_method(symbol) do |message = nil, context = nil, progname = nil, &block|
45
46
  severity = Object.const_get("Logger::Severity::#{symbol.to_s.upcase}")
46
47
  return true unless @logger.log?(severity)
47
48
 
49
+ message, context = LoggerData.build(message, context, &block)
48
50
  context = merge_context(@context, context)
49
51
  context = context.call if context.is_a?(Proc)
50
- @logger.public_send(symbol, message, **context, &block)
52
+
53
+ @logger.add(severity, LogMessage.new(message, context), progname, &block)
51
54
  end
52
55
  end
53
56
 
@@ -22,9 +22,8 @@ module MrLogaLoga
22
22
  # @param progname [DateTime] The program name
23
23
  # @param message [Object] The log message, which may not be a string
24
24
  # @param context [Hash] The log message context
25
- #
26
25
  # @return [String] the formatted log message
27
- def call(severity, datetime, progname, message, **context)
26
+ def call(severity, datetime, progname, message, context)
28
27
  message = message.nil? ? '' : msg2str(message).strip
29
28
 
30
29
  message_hash = {
@@ -24,8 +24,8 @@ module MrLogaLoga
24
24
  # @param context [Hash] The log message context
25
25
  #
26
26
  # @return [String] the formatted log message
27
- def call(severity, datetime, progname, message, **context)
28
- message = msg2str(message).strip
27
+ def call(severity, datetime, progname, message, context = {})
28
+ message = message ? msg2str(message).strip : ''
29
29
  message = context.map { |key, value| "#{key}=#{value}" }
30
30
  .prepend(message)
31
31
  .compact
@@ -14,7 +14,7 @@ module MrLogaLoga
14
14
  # A shorthand method to use in your classes
15
15
  def logger
16
16
  if loga_loga.is_a?(MrLogaLoga::Logger)
17
- MrLogaLoga::LoggerProxy.new(loga_loga, -> { loga_context })
17
+ loga_loga.context { loga_context }
18
18
  else
19
19
  loga_loga
20
20
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MrLogaLoga
4
+ # A structured log message containing the actual message and context
5
+ #
6
+ # @private
7
+ LogMessage = Struct.new(:msg, :context) do
8
+ # Format the log message. This is a fallback for logger exctending MrLogaLoga who do not know how to format
9
+ # LogMessage themselves.
10
+ def inspect
11
+ # This is identical to ActiveRecord::SimpleFormatter
12
+ "#{msg.is_a?(String) ? msg : msg.inspect}\n"
13
+ end
14
+ end
15
+ end
@@ -21,44 +21,49 @@ module MrLogaLoga
21
21
  # logger.context(user: 1).debug('with context')
22
22
  #
23
23
  class Logger < ::Logger
24
+ alias super_add add
25
+
24
26
  def initialize(*args, **kwargs)
25
27
  super
26
28
  @default_formatter = MrLogaLoga::Formatters::KeyValue.new
27
29
  end
28
30
 
29
31
  # Generates a new context
30
- def context(**kwargs, &block)
31
- context = block ? -> { kwargs.merge(block.call) } : kwargs
32
- Context.new(self, context)
32
+ def context(context = {}, &block)
33
+ result = block ? -> { context.merge(block.call) } : context
34
+ Context.new(self, result)
33
35
  end
34
36
 
35
37
  # Adds a new log message with the given severity
36
- def add(severity, message = nil, progname = nil, *_args, **context, &block)
38
+ def add(severity, message = nil, context = nil, progname = nil, &block)
39
+ write(severity, message, context, progname, &block)
40
+ end
41
+
42
+ # Write the actual log data
43
+ #
44
+ # This method needs to be used rather than add as various gems (Rails, Sidekiq) patch loggers to overwrite add. The
45
+ # patches' signatures do not match our add method, so we use this method to do the actual logging in helper methods
46
+ # like debug, info etc.
47
+ #
48
+ def write(severity, *args, &block)
37
49
  severity ||= UNKNOWN
38
50
  return true unless log?(severity)
39
51
 
40
- progname = @progname if progname.nil?
52
+ message, context, progname = args
53
+ log_message = message.is_a?(LogMessage) ? message : LogMessage.new(*LoggerData.build(message, context, &block))
41
54
 
42
- if message.nil?
43
- if block
44
- message = block.call
45
- else
46
- message = progname
47
- progname = @progname
48
- end
49
- end
50
-
51
- @logdev.write(format(format_severity(severity), Time.now, progname, message, context))
52
- true
55
+ super_add(severity, log_message, progname)
53
56
  end
54
57
 
55
58
  alias log add
56
59
 
57
60
  %i[debug info warn error fatal unknown].each do |symbol|
58
- define_method(symbol) do |progname = nil, **context, &block|
61
+ define_method(symbol) do |message = nil, context = nil, progname = nil, &block|
59
62
  # Map the symbol (e.g. :debug) to the severity constant (e.g. DEBUG)
60
63
  severity = Object.const_get("Logger::Severity::#{symbol.to_s.upcase}")
61
- add(severity, nil, progname, **context, &block)
64
+ message, context = LoggerData.build(message, context, &block)
65
+
66
+ add(severity, LogMessage.new(message, context), progname, &block)
62
67
  end
63
68
  end
64
69
 
@@ -85,8 +90,8 @@ module MrLogaLoga
85
90
  end
86
91
  end
87
92
 
88
- def format(severity, datetime, progname, message, context)
89
- (@formatter || @default_formatter).call(severity, datetime, progname, message, **context)
93
+ def format_message(severity, datetime, progname, message)
94
+ (@formatter || @default_formatter).call(severity, datetime, progname, message.msg, message.context)
90
95
  end
91
96
  end
92
97
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module MrLogaLoga
6
+ # == Description
7
+ #
8
+ # This class separates message and contextual data from args
9
+ class LoggerData
10
+ class << self
11
+ def build(*args, &block)
12
+ args = block ? block.call : args
13
+ msg, context = args
14
+ if msg.nil?
15
+ [nil, as_hash(context)]
16
+ elsif context.nil?
17
+ msg.is_a?(Hash) ? [nil, msg] : [msg, {}]
18
+ else
19
+ [msg, as_hash(context)]
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def as_hash(data)
26
+ if data.is_a?(Hash) || data.respond_to?(:to_hash)
27
+ data
28
+ elsif !data.nil?
29
+ { context: data }
30
+ else
31
+ {}
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MrLogaLoga
4
- VERSION = '0.1.5'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/mr_loga_loga.rb CHANGED
@@ -2,16 +2,14 @@
2
2
 
3
3
  require_relative 'mr_loga_loga/version'
4
4
  require_relative 'mr_loga_loga/configuration'
5
- require_relative 'mr_loga_loga/logger_proxy'
6
5
  require_relative 'mr_loga_loga/instance_methods'
6
+ require_relative 'mr_loga_loga/logger_data'
7
+ require_relative 'mr_loga_loga/log_message'
7
8
  require_relative 'mr_loga_loga/context'
8
9
  require_relative 'mr_loga_loga/logger'
9
10
  require_relative 'mr_loga_loga/formatters/key_value'
10
11
  require_relative 'mr_loga_loga/formatters/json'
11
12
 
12
- require_relative 'mr_loga_loga/extensions/rails'
13
- require_relative 'mr_loga_loga/extensions/lograge'
14
-
15
13
  # ## Description
16
14
  #
17
15
  # The MrLogaLoga module provides additional logging functionality when included in your classes.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mr_loga_loga
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hschne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-26 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -128,13 +128,12 @@ files:
128
128
  - lib/mr_loga_loga.rb
129
129
  - lib/mr_loga_loga/configuration.rb
130
130
  - lib/mr_loga_loga/context.rb
131
- - lib/mr_loga_loga/extensions/lograge.rb
132
- - lib/mr_loga_loga/extensions/rails.rb
133
131
  - lib/mr_loga_loga/formatters/json.rb
134
132
  - lib/mr_loga_loga/formatters/key_value.rb
135
133
  - lib/mr_loga_loga/instance_methods.rb
134
+ - lib/mr_loga_loga/log_message.rb
136
135
  - lib/mr_loga_loga/logger.rb
137
- - lib/mr_loga_loga/logger_proxy.rb
136
+ - lib/mr_loga_loga/logger_data.rb
138
137
  - lib/mr_loga_loga/version.rb
139
138
  - logo.png
140
139
  - mr_loga_loga.gemspec
@@ -161,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
160
  - !ruby/object:Gem::Version
162
161
  version: '0'
163
162
  requirements: []
164
- rubygems_version: 3.2.3
163
+ rubygems_version: 3.3.7
165
164
  signing_key:
166
165
  specification_version: 4
167
166
  summary: A bombastic, fantastic logger for Ruby
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MrLogaLoga
4
- module Extensions
5
- # == Description
6
- #
7
- # This patches Lograge to forward data as context to MrLogaLoga.
8
- #
9
- # We want Lograge to forward requeest data not to it's own formatter and then to the logger, as this would make that
10
- # data part of the message. Rather, where Lograge normally sends the formatted message to the logger we send the
11
- # raw data.
12
- #
13
- # This effectively circumvents Lograge's formatters.
14
- #
15
- # == Patches
16
- #
17
- # - Lograge::LogSubscribers#process_main_event
18
- #
19
- module LogrageExtension
20
- class << self
21
- def apply
22
- return unless defined?(Lograge)
23
-
24
- patch_applies = defined?(Lograge::LogSubscribers::Base) &&
25
- Lograge::LogSubscribers::Base.private_method_defined?(:process_main_event)
26
- unless patch_applies
27
- puts "WARNING: Failed to patch Lograge. It looks like MrLogaLoga's patch no longer applies in "\
28
- "#{__FILE__}. Please contact MrLogaLoga maintainers."
29
- return
30
- end
31
-
32
- Lograge::LogSubscribers::Base.prepend(self)
33
- end
34
- end
35
-
36
- def process_main_event(event)
37
- return if Lograge.ignore?(event)
38
-
39
- payload = event.payload
40
- data = extract_request(event, payload)
41
- data = before_format(data, payload)
42
- # Instead of
43
- if logger.is_a?(MrLogaLoga::Logger)
44
- logger.send(Lograge.log_level, '', **data)
45
- else
46
- formatted_message = Lograge.formatter.call(data)
47
- logger.send(Lograge.log_level, formatted_message)
48
- end
49
- end
50
- end
51
- end
52
- end
53
-
54
- MrLogaLoga::Extensions::LogrageExtension.apply
@@ -1,154 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MrLogaLoga
4
- module Extensions
5
- # == Description
6
- #
7
- # This patches Rails to allow it to work with loggers that accept keyword arguments.
8
- #
9
- # Per default, Rails patches any logger during server startup to be able to broadcast any log messages to the
10
- # console. This patch assumes that loggers only take args and blocks, so we have to change that.
11
- #
12
- # During server startup Rails will always use the ActiveSupport::Logger to send data to the console.
13
- # This was modified o changed to always use the user-configured logger.
14
- #
15
- # == Patches
16
- #
17
- # - {ActiveSupport::Logger#self.broadcast}
18
- # - {Rails::Server#log_to_stdout}
19
- #
20
- module RailsExtension
21
- class << self
22
- def apply
23
- return unless defined?(Rails)
24
-
25
- patch_logger
26
- patch_server
27
- end
28
-
29
- private
30
-
31
- def patch_server
32
- server_defined = defined?(Rails::Server)
33
- return unless server_defined
34
-
35
- unless Rails::Server.private_method_defined?(:log_to_stdout)
36
- puts "WARNING: Failed to patch Rails::Server. It looks like MrLogaLoga's patch in #{__FILE__} no "\
37
- "longer applies. Please contact MrLogaLoga's maintainers."
38
- return
39
- end
40
-
41
- Rails::Server.prepend(ServerPatch)
42
- end
43
-
44
- def patch_logger
45
- logger_defined = defined?(ActiveSupport::Logger)
46
- return unless logger_defined
47
-
48
- unless broadcast_method
49
- puts "WARNING: Failed to patch ActiveSupport::Logger. It looks like MrLogaLoga's patch in #{__FILE__} no "\
50
- "longer applies. Please contact MrLogaLoga's maintainers."
51
- return
52
- end
53
-
54
- ActiveSupport::Logger.include(LoggerPatch)
55
- end
56
-
57
- def broadcast_method
58
- broadcast = ActiveSupport::Logger.method(:broadcast)
59
- broadcast && broadcast.arity == 1
60
- rescue NameError
61
- false
62
- end
63
-
64
- # This patches ActiveSupport::Logger to allow properly formatting keyword arguments
65
- module LoggerPatch
66
- # rubocop:disable all
67
- def included base
68
- base.instance_eval do
69
- def broadcast(logger)
70
- Module.new do
71
- # We need to patch this method as otherwise calling add with keyword arguments results in argument errors.
72
- # Rails calls `broadcast` for the logger configured in `Rails.logger`, which means that any invocations of
73
- # Rails.logger.add result in running through this method.
74
- #
75
- # TODO: Remove once patched in Rails
76
- define_method(:add) do |*args, **kwargs, &block|
77
- logger.add(*args, **kwargs, &block)
78
- super(*args, **kwargs, &block)
79
- end
80
-
81
- define_method(:<<) do |x|
82
- logger << x
83
- super(x)
84
- end
85
-
86
- define_method(:close) do
87
- logger.close
88
- super()
89
- end
90
-
91
- define_method(:progname=) do |name|
92
- logger.progname = name
93
- super(name)
94
- end
95
-
96
- define_method(:formatter=) do |formatter|
97
- logger.formatter = formatter
98
- super(formatter)
99
- end
100
-
101
- define_method(:level=) do |level|
102
- logger.level = level
103
- super(level)
104
- end
105
-
106
- define_method(:local_level=) do |level|
107
- logger.local_level = level if logger.respond_to?(:local_level=)
108
- super(level) if respond_to?(:local_level=)
109
- end
110
-
111
- define_method(:silence) do |level = Logger::ERROR, &block|
112
- if logger.respond_to?(:silence)
113
- logger.silence(level) do
114
- if defined?(super)
115
- super(level, &block)
116
- else
117
- block.call(self)
118
- end
119
- end
120
- else
121
- if defined?(super)
122
- super(level, &block)
123
- else
124
- block.call(self)
125
- end
126
- end
127
- end
128
- end
129
- end
130
- end
131
- end
132
- # rubocpo:enable all
133
- end
134
-
135
- # This patches the server command so that console output will use the same logger you previously configured
136
- module ServerPatch
137
- def log_to_stdout
138
- wrapped_app # touch the app so the logger is set up
139
-
140
- console = Rails.logger.class.new(STDOUT)
141
- console.formatter = Rails.logger.formatter
142
- console.level = Rails.logger.level
143
-
144
- unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDOUT)
145
- Rails.logger.extend(ActiveSupport::Logger.broadcast(console))
146
- end
147
- end
148
- end
149
- end
150
- end
151
- end
152
- end
153
-
154
- MrLogaLoga::Extensions::RailsExtension.apply
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'logger'
4
- require 'forwardable'
5
-
6
- module MrLogaLoga
7
- # == Description
8
- #
9
- # A proxy that attaches contextual information to the underlying logger when called.
10
- #
11
- # @api private
12
- class LoggerProxy
13
- extend Forwardable
14
-
15
- def initialize(logger, context_proc)
16
- @logger = logger
17
- @context_proc = context_proc
18
- end
19
-
20
- def add(severity, message = nil, progname = nil, **context, &block)
21
- severity ||= UNKNOWN
22
- return true unless @logger.log?(severity)
23
-
24
- context = @context_proc.call.merge(context)
25
-
26
- @logger.add(severity, message, progname, **context, &block)
27
- end
28
-
29
- alias log add
30
-
31
- %i[debug info warn error fatal unknown].each do |symbol|
32
- def_delegator :@logger, "#{symbol}?".to_sym, "#{symbol}?".to_sym
33
-
34
- define_method(symbol) do |progname = nil, **context, &block|
35
- severity = Object.const_get("Logger::Severity::#{symbol.to_s.upcase}")
36
- add(severity, nil, progname, **context, &block)
37
- end
38
- end
39
- end
40
- end