any_logger 0.1.5 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 165c98ee85c05ca25d605f1f183afe9378bb7f14c1d0b9a8179bbd2a5000accc
4
- data.tar.gz: a3904fb46fe860748e01546c434988ec99666189dea7ef1b35e98e768936e0a2
3
+ metadata.gz: 3ce22730112cf7d85f3f6bbbd9121953414ddf2340dec1c1345f1fa029f00cf2
4
+ data.tar.gz: 369a2346c8b527a95f8c12a5c0bb28b0775e9dc3b161760e07fae48bdc554a63
5
5
  SHA512:
6
- metadata.gz: fa55349de040b2afe0abc46c16de65204ad43d9f15377788aac3719367b7ac2a7638f653214e9cc2c3adaa471f899cd436dad644f935eb7732cf15139d2310f1
7
- data.tar.gz: d3c6a6b60ae6b57d08077bba36612323a3425e74d1c7ac2f8c491383395c6661b812d650e388d51780232accf54cf11b9a1660d0fb931bfa771536efa11964a8
6
+ metadata.gz: f16b68080e058b6fd5b57e9439b887a5961f6dcaa17376d03e07fdd77cce0f8f11baefd7dc583dd1f229c571b5d59f79f581e57973637d0dec7ccc9aa4d7d892
7
+ data.tar.gz: 03f93b241f571c798b576278916536d0d518ae25c89fe3851fdd8f26d9d7796f3469636505a796cefe145eca256773e9b1cfc9bc68a78ed0f36bb1575b974f15
data/CHANGELOG.md CHANGED
@@ -19,3 +19,13 @@
19
19
  ## [0.1.5] - 2025-01-15
20
20
 
21
21
  - except_unnecessary_paramsをexpectに誤字していたのを修正
22
+
23
+ ## [0.2.0] - 2025-01-19
24
+
25
+ - process_action.action_controllerのような個別のイベントに対応
26
+
27
+ ## [0.2.1] - 2025-01-20
28
+
29
+ - Example::ControllerSubscriberのparamsに関するログが、paramsが空の場合にも出力される問題を修正
30
+ - config.subscriber.detach :action_viewをしたときに、
31
+ ActionView::LogSubscriberがデフォルトでsubscribeするイベントをunsubscribeするように変更
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # AnyLogger
2
2
 
3
- A DSL for simplifying modification of Rails LogSubscribers
3
+ A DSL for simplifying modification of Rails ActiveSupport Instrumentation API
4
4
 
5
5
  ### Example
6
6
 
@@ -11,8 +11,16 @@ require "any_logger/example/controller_subscriber"
11
11
 
12
12
  AnyLogger.configure do |config|
13
13
  config.logger = Rails::Rack::Logger # default: AnyLogger::Example::RackLogger
14
+
15
+ config.subscriber.attach :action_mailer, MyLogger::ModelSubscriber
16
+ # If you call swap, detach, or attach directly in config, the subscriber will be set
14
17
  config.swap :action_controller, AnyLogger::Example::ControllerSubscriber
15
18
  config.detach :action_view
16
- config.attach :active_record, MyLogger::ModelSubscriber
19
+
20
+ # Event can be attached using either a class or a block
21
+ config.event.swap :active_record, :sql, MyLogger::ActiveRecord::Sql
22
+ config.event.attach :active_job, :discard do |event|
23
+ MyErrorReporter.notify(event)
24
+ end
17
25
  end
18
26
  ```
@@ -0,0 +1,97 @@
1
+ require "active_support/notifications"
2
+
3
+ module AnyLogger
4
+ class Configuration
5
+ class Event
6
+ Detach = Struct.new(:organizer, :event)
7
+ Attach = Struct.new(:organizer, :event, :subscriber)
8
+
9
+ DEFAULT_ATTACHED_EVENTS = {
10
+ action_view: [:render_template, :render_layout]
11
+ }
12
+
13
+ private_constant :DEFAULT_ATTACHED_EVENTS
14
+
15
+ def swap(organizer, event, subscriber = nil, &block)
16
+ @organizer = organizer
17
+ @event = event
18
+ @subscriber = subscriber
19
+ @block = block
20
+
21
+ detach(organizer, event)
22
+ attach(organizer, event, subscriber, &block)
23
+ end
24
+
25
+ def detach(organizer, event)
26
+ @organizer ||= organizer
27
+ @event ||= event
28
+
29
+ push_detach_to_subscriptions
30
+ detach_from_event
31
+ end
32
+
33
+ def detaches_default_attached_for(organizer)
34
+ return unless DEFAULT_ATTACHED_EVENTS[organizer]
35
+
36
+ DEFAULT_ATTACHED_EVENTS[organizer].each { |event| detach(organizer, event) }
37
+ end
38
+
39
+ def attach(organizer, event, subscriber = nil, &block)
40
+ @organizer ||= organizer
41
+ @event ||= event
42
+ @subscriber ||= subscriber
43
+ @block ||= block
44
+
45
+ validate_provided_block_or_subscriber
46
+ validate_unless_provided_block_and_subscriber
47
+ validate_subscriber_is_callable
48
+
49
+ push_attach_to_subscriptions
50
+ attach_to_event
51
+ end
52
+
53
+ private def event_name
54
+ "#{@event}.#{@organizer}"
55
+ end
56
+
57
+ private def push_detach_to_subscriptions
58
+ AnyLogger.config.subscriptions << Detach.new(@organizer, @event)
59
+ end
60
+
61
+ private def push_attach_to_subscriptions
62
+ AnyLogger.config.subscriptions << Attach.new(@organizer, @event, @subscriber || @block)
63
+ end
64
+
65
+ private def detach_from_event
66
+ ActiveSupport::Notifications.unsubscribe(event_name)
67
+ end
68
+
69
+ private def attach_to_event
70
+ # LogSubscriber#attach_toはmonotonic_subscribeなので、それに合わせる
71
+ if @subscriber
72
+ ActiveSupport::Notifications.monotonic_subscribe(event_name, @subscriber.new)
73
+ else
74
+ ActiveSupport::Notifications.monotonic_subscribe(event_name, &@block)
75
+ end
76
+ end
77
+
78
+ private def validate_provided_block_or_subscriber
79
+ return if @block || @subscriber
80
+
81
+ raise ArgumentError, "Block or subscriber must be provided"
82
+ end
83
+
84
+ private def validate_unless_provided_block_and_subscriber
85
+ return unless @block && @subscriber
86
+
87
+ raise ArgumentError, "Block and subscriber cannot be provided simultaneously"
88
+ end
89
+
90
+ private def validate_subscriber_is_callable
91
+ return unless @subscriber && !@subscriber.public_method_defined?(:call)
92
+
93
+ raise NoMethodError, "Subscriber must respond to #call"
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,74 @@
1
+ require "active_support"
2
+ require "active_support/core_ext/string/inflections"
3
+ require "action_controller/log_subscriber"
4
+ require "active_record/log_subscriber"
5
+ require "action_view/log_subscriber"
6
+ require "action_mailer/log_subscriber"
7
+ require "action_dispatch/log_subscriber"
8
+ require "active_job/log_subscriber"
9
+ require "active_storage/log_subscriber"
10
+
11
+ module AnyLogger
12
+ class Configuration
13
+ class LogSubscriber
14
+ Detach = Struct.new(:organizer, :subscriber)
15
+ Attach = Struct.new(:organizer, :subscriber)
16
+
17
+ DEFAULT_SUBSCRIBERS = {
18
+ action_controller: ActionController::LogSubscriber,
19
+ active_record: ActiveRecord::LogSubscriber,
20
+ action_view: ActionView::LogSubscriber,
21
+ action_mailer: ActionMailer::LogSubscriber,
22
+ action_dispatch: ActionDispatch::LogSubscriber,
23
+ active_job: ActiveJob::LogSubscriber,
24
+ active_storage: ActiveStorage::LogSubscriber
25
+ }
26
+
27
+ private_constant :DEFAULT_SUBSCRIBERS
28
+
29
+ def swap(organizer, subscriber, detach_target = nil)
30
+ @organizer = organizer
31
+ @subscriber = subscriber
32
+ @detach_target = detach_target || DEFAULT_SUBSCRIBERS[organizer]
33
+
34
+ detach(organizer, detach_target)
35
+ attach(organizer, subscriber)
36
+ end
37
+
38
+ def detach(organizer, detach_target = nil)
39
+ @organizer ||= organizer
40
+ @detach_target ||= detach_target || DEFAULT_SUBSCRIBERS[organizer]
41
+
42
+ validate_detach_target_is_set
43
+
44
+ push_detach_to_subscriptions
45
+ @detach_target.detach_from(@organizer)
46
+ event.detaches_default_attached_for(@organizer)
47
+ end
48
+
49
+ def attach(organizer, subscriber)
50
+ @organizer ||= organizer
51
+ @subscriber ||= subscriber
52
+
53
+ push_attach_to_subscriptions
54
+ @subscriber.attach_to(@organizer)
55
+ end
56
+
57
+ private def push_detach_to_subscriptions
58
+ AnyLogger.config.subscriptions << Detach.new(@organizer, @detach_target)
59
+ end
60
+
61
+ private def push_attach_to_subscriptions
62
+ AnyLogger.config.subscriptions << Attach.new(@organizer, @subscriber)
63
+ end
64
+
65
+ private def validate_detach_target_is_set
66
+ raise KeyError, "#{@organizer}'s default subscriber not found" unless @detach_target
67
+ end
68
+
69
+ private def event
70
+ AnyLogger.config.event
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,43 +1,18 @@
1
1
  require "singleton"
2
- require "active_support"
3
- require "active_support/core_ext/string/inflections"
4
- require "action_controller/log_subscriber"
5
- require "active_record/log_subscriber"
6
- require "action_view/log_subscriber"
7
- require "action_mailer/log_subscriber"
8
- require "action_dispatch/log_subscriber"
9
- require "active_job/log_subscriber"
10
- require "active_storage/log_subscriber"
11
2
  require_relative "example/rack_logger"
3
+ require_relative "configuration/log_subscriber"
4
+ require_relative "configuration/event"
12
5
 
13
6
  module AnyLogger
14
7
  class Configuration
15
8
  include Singleton
16
9
 
17
- Subscriber = Struct.new(:subscription, :klass, :detachable, :attachable) do
18
- def initialize(subscription, klass, detachable: false, attachable: false)
19
- super(subscription, klass, detachable, attachable)
20
- end
21
- end
22
-
23
- DEFAULT_SUBSCRIBERS = {
24
- action_controller: ActionController::LogSubscriber,
25
- active_record: ActiveRecord::LogSubscriber,
26
- action_view: ActionView::LogSubscriber,
27
- action_mailer: ActionMailer::LogSubscriber,
28
- action_dispatch: ActionDispatch::LogSubscriber,
29
- active_job: ActiveJob::LogSubscriber,
30
- active_storage: ActiveStorage::LogSubscriber
31
- }
32
-
33
- private_constant :DEFAULT_SUBSCRIBERS
34
-
35
10
  attr_reader :config
36
11
 
37
12
  def initialize
38
13
  @config = {
39
14
  logger: ::AnyLogger::Example::RackLogger,
40
- subscribers: DEFAULT_SUBSCRIBERS.map { |key, klass| Subscriber.new(key, klass) }
15
+ subscriptions: []
41
16
  }
42
17
  end
43
18
 
@@ -46,34 +21,36 @@ module AnyLogger
46
21
  end
47
22
 
48
23
  def logger=(klass)
24
+ Rails.application.config.middleware.swap(logger, klass)
49
25
  @config[:logger] = klass
50
26
  end
51
27
 
52
- def subscribers
53
- @config[:subscribers]
28
+ def swap_default_logger
29
+ Rails.application.config.middleware.swap(Rails::Rack::Logger, logger)
54
30
  end
55
31
 
56
- def swap(key, klass, target_klass = nil)
57
- target_klass ||= DEFAULT_SUBSCRIBERS[key]
58
- raise KeyError, "Default subscriber not found" unless target_klass
32
+ def subscriptions
33
+ @config[:subscriptions]
34
+ end
59
35
 
60
- detach(key, target_klass)
61
- attach(key, klass)
36
+ def subscriber
37
+ LogSubscriber.new
62
38
  end
63
39
 
64
- def detach(key, klass = nil)
65
- klass ||= DEFAULT_SUBSCRIBERS[key]
66
- raise KeyError, "Default subscriber not found" unless klass
40
+ def event
41
+ Event.new
42
+ end
67
43
 
68
- target_subscriber =
69
- @config[:subscribers].find { it.subscription == key && it.klass == klass }
44
+ def swap(...)
45
+ subscriber.swap(...)
46
+ end
70
47
 
71
- target_subscriber.detachable = true
72
- target_subscriber.attachable = false
48
+ def detach(...)
49
+ subscriber.detach(...)
73
50
  end
74
51
 
75
- def attach(key, klass)
76
- @config[:subscribers] << Subscriber.new(key, klass, attachable: true)
52
+ def attach(...)
53
+ subscriber.attach(...)
77
54
  end
78
55
  end
79
56
  end
@@ -36,7 +36,7 @@ module AnyLogger
36
36
  lines << " 🔍 path: \"#{e[:path]}\""
37
37
  lines << " 🧱 status: #{e[:status]} in #{e[:duration]}ms " \
38
38
  "(view: #{e[:view_runtime]}ms | db: #{e[:db_runtime]}ms)"
39
- lines << " 📝 params: #{e[:params]}" if @payload[:params].present?
39
+ lines << " 📝 params: #{e[:params]}" if e[:params].present?
40
40
  lines << " 🚀 redirect: \"#{e[:redirect_to]}\"" if @headers[:location].present?
41
41
  lines.join("\n")
42
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AnyLogger
4
- VERSION = "0.1.5"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/any_logger.rb CHANGED
@@ -1,22 +1,17 @@
1
1
  require_relative "any_logger/version"
2
2
  require_relative "any_logger/configuration"
3
- require_relative "any_logger/initializer"
4
3
 
5
4
  module AnyLogger
6
- def self.configure(auto_startable: true, &block)
5
+ def self.configure(&block)
6
+ config.swap_default_logger
7
7
  block.call(config)
8
- start if auto_startable
9
- end
10
-
11
- def self.start
12
- Initializer.run
13
8
  end
14
9
 
15
10
  def self.config
16
11
  Configuration.instance
17
12
  end
18
13
 
19
- def self.subscribers
20
- config.subscribers
14
+ def self.subscriptions
15
+ config.subscriptions
21
16
  end
22
17
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: any_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - milkeclair
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-01-14 00:00:00.000000000 Z
10
+ date: 2025-01-20 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: actionmailer
@@ -93,7 +93,8 @@ dependencies:
93
93
  - - ">="
94
94
  - !ruby/object:Gem::Version
95
95
  version: '0'
96
- description: A DSL for simplifying modification of Rails LogSubscribers
96
+ description: A DSL for simplifying modification of Rails ActiveSupport Instrumentation
97
+ API
97
98
  email:
98
99
  - milkeclair.black@gmail.com
99
100
  executables: []
@@ -109,9 +110,10 @@ files:
109
110
  - Rakefile
110
111
  - lib/any_logger.rb
111
112
  - lib/any_logger/configuration.rb
113
+ - lib/any_logger/configuration/event.rb
114
+ - lib/any_logger/configuration/log_subscriber.rb
112
115
  - lib/any_logger/example/controller_subscriber.rb
113
116
  - lib/any_logger/example/rack_logger.rb
114
- - lib/any_logger/initializer.rb
115
117
  - lib/any_logger/version.rb
116
118
  - sig/any_logger.rbs
117
119
  homepage: https://github.com/milkeclair/any_logger
@@ -135,5 +137,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
137
  requirements: []
136
138
  rubygems_version: 3.6.2
137
139
  specification_version: 4
138
- summary: A DSL for simplifying modification of Rails LogSubscribers
140
+ summary: A DSL for simplifying modification of Rails ActiveSupport Instrumentation
141
+ API
139
142
  test_files: []
@@ -1,27 +0,0 @@
1
- module AnyLogger
2
- class Initializer
3
- def self.run
4
- swap_default_logger
5
- change_subscribers
6
- end
7
-
8
- private_class_method def self.swap_default_logger
9
- Rails.application.config.middleware.swap(Rails::Rack::Logger, AnyLogger.config.logger)
10
- end
11
-
12
- private_class_method def self.change_subscribers
13
- AnyLogger.config.subscribers.each do
14
- detach_subscriber(it) if it.detachable
15
- attach_subscriber(it) if it.attachable
16
- end
17
- end
18
-
19
- private_class_method def self.detach_subscriber(subscriber)
20
- subscriber.klass.detach_from(subscriber.subscription)
21
- end
22
-
23
- private_class_method def self.attach_subscriber(subscriber)
24
- subscriber.klass.attach_to(subscriber.subscription)
25
- end
26
- end
27
- end