any_logger 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +10 -2
- data/lib/any_logger/configuration/event.rb +85 -0
- data/lib/any_logger/configuration/log_subscriber.rb +69 -0
- data/lib/any_logger/configuration.rb +21 -44
- data/lib/any_logger/example/controller_subscriber.rb +1 -1
- data/lib/any_logger/version.rb +1 -1
- data/lib/any_logger.rb +4 -9
- metadata +8 -5
- data/lib/any_logger/initializer.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f41ec5169998e8de01c427d09baccaf59fa27099e5b1e760fafbefd1a0c9218
|
4
|
+
data.tar.gz: 8f10462f5a7fac93fa0b67cd6c1c66c23f73ac2298fa46d87a4a9d33dd5aeaa8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c43933d48c1a555c37d807ae775f5923fba403794fe74d3f8be6565930c07739de0fb0114cef395fd8c6463113ef0d40ae0d27d4e70237046174352c07fb9620
|
7
|
+
data.tar.gz: 19d911b00ce6b65bdaa45649312adf2ae92f692631fb2023f99947b40b50f63e072aa8e6256d4d909b9b48b294dbb29ff9bef01cf172d37b29938be8b5987129
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# AnyLogger
|
2
2
|
|
3
|
-
A DSL for simplifying modification of Rails
|
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
|
-
|
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,85 @@
|
|
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
|
+
def swap(organizer, event, subscriber = nil, &block)
|
10
|
+
@organizer = organizer
|
11
|
+
@event = event
|
12
|
+
@subscriber = subscriber
|
13
|
+
@block = block
|
14
|
+
|
15
|
+
detach(organizer, event)
|
16
|
+
attach(organizer, event, subscriber, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def detach(organizer, event)
|
20
|
+
@organizer ||= organizer
|
21
|
+
@event ||= event
|
22
|
+
|
23
|
+
push_detach_to_subscriptions
|
24
|
+
detach_from_event
|
25
|
+
end
|
26
|
+
|
27
|
+
def attach(organizer, event, subscriber = nil, &block)
|
28
|
+
@organizer ||= organizer
|
29
|
+
@event ||= event
|
30
|
+
@subscriber ||= subscriber
|
31
|
+
@block ||= block
|
32
|
+
|
33
|
+
validate_provided_block_or_subscriber
|
34
|
+
validate_unless_provided_block_and_subscriber
|
35
|
+
validate_subscriber_is_callable
|
36
|
+
|
37
|
+
push_attach_to_subscriptions
|
38
|
+
attach_to_event
|
39
|
+
end
|
40
|
+
|
41
|
+
private def event_name
|
42
|
+
"#{@event}.#{@organizer}"
|
43
|
+
end
|
44
|
+
|
45
|
+
private def push_detach_to_subscriptions
|
46
|
+
AnyLogger.config.subscriptions << Detach.new(@organizer, @event)
|
47
|
+
end
|
48
|
+
|
49
|
+
private def push_attach_to_subscriptions
|
50
|
+
AnyLogger.config.subscriptions << Attach.new(@organizer, @event, @subscriber || @block)
|
51
|
+
end
|
52
|
+
|
53
|
+
private def detach_from_event
|
54
|
+
ActiveSupport::Notifications.unsubscribe(event_name)
|
55
|
+
end
|
56
|
+
|
57
|
+
private def attach_to_event
|
58
|
+
# LogSubscriber#attach_toはmonotonic_subscribeなので、それに合わせる
|
59
|
+
if @subscriber
|
60
|
+
ActiveSupport::Notifications.monotonic_subscribe(event_name, @subscriber.new)
|
61
|
+
else
|
62
|
+
ActiveSupport::Notifications.monotonic_subscribe(event_name, &@block)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private def validate_provided_block_or_subscriber
|
67
|
+
return if @block || @subscriber
|
68
|
+
|
69
|
+
raise ArgumentError, "Block or subscriber must be provided"
|
70
|
+
end
|
71
|
+
|
72
|
+
private def validate_unless_provided_block_and_subscriber
|
73
|
+
return unless @block && @subscriber
|
74
|
+
|
75
|
+
raise ArgumentError, "Block and subscriber cannot be provided simultaneously"
|
76
|
+
end
|
77
|
+
|
78
|
+
private def validate_subscriber_is_callable
|
79
|
+
return unless @subscriber && !@subscriber.public_method_defined?(:call)
|
80
|
+
|
81
|
+
raise NoMethodError, "Subscriber must respond to #call"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,69 @@
|
|
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
|
+
end
|
47
|
+
|
48
|
+
def attach(organizer, subscriber)
|
49
|
+
@organizer ||= organizer
|
50
|
+
@subscriber ||= subscriber
|
51
|
+
|
52
|
+
push_attach_to_subscriptions
|
53
|
+
@subscriber.attach_to(@organizer)
|
54
|
+
end
|
55
|
+
|
56
|
+
private def push_detach_to_subscriptions
|
57
|
+
AnyLogger.config.subscriptions << Detach.new(@organizer, @detach_target)
|
58
|
+
end
|
59
|
+
|
60
|
+
private def push_attach_to_subscriptions
|
61
|
+
AnyLogger.config.subscriptions << Attach.new(@organizer, @subscriber)
|
62
|
+
end
|
63
|
+
|
64
|
+
private def validate_detach_target_is_set
|
65
|
+
raise KeyError, "#{@organizer}'s default subscriber not found" unless @detach_target
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
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
|
-
|
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
|
53
|
-
|
28
|
+
def swap_default_logger
|
29
|
+
Rails.application.config.middleware.swap(Rails::Rack::Logger, logger)
|
54
30
|
end
|
55
31
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
32
|
+
def subscriptions
|
33
|
+
@config[:subscriptions]
|
34
|
+
end
|
59
35
|
|
60
|
-
|
61
|
-
|
36
|
+
def subscriber
|
37
|
+
LogSubscriber.new
|
62
38
|
end
|
63
39
|
|
64
|
-
def
|
65
|
-
|
66
|
-
|
40
|
+
def event
|
41
|
+
Event.new
|
42
|
+
end
|
67
43
|
|
68
|
-
|
69
|
-
|
44
|
+
def swap(...)
|
45
|
+
subscriber.swap(...)
|
46
|
+
end
|
70
47
|
|
71
|
-
|
72
|
-
|
48
|
+
def detach(...)
|
49
|
+
subscriber.detach(...)
|
73
50
|
end
|
74
51
|
|
75
|
-
def attach(
|
76
|
-
|
52
|
+
def attach(...)
|
53
|
+
subscriber.attach(...)
|
77
54
|
end
|
78
55
|
end
|
79
56
|
end
|
@@ -57,7 +57,7 @@ module AnyLogger
|
|
57
57
|
duration: round_value(@event.duration),
|
58
58
|
view_runtime: round_value(@payload[:view_runtime]),
|
59
59
|
db_runtime: round_value(@payload[:db_runtime]),
|
60
|
-
params:
|
60
|
+
params: except_unnecessary_params || {},
|
61
61
|
redirect_to: formatted_redirect_location
|
62
62
|
}
|
63
63
|
end
|
data/lib/any_logger/version.rb
CHANGED
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(
|
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.
|
20
|
-
config.
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- milkeclair
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-01-
|
10
|
+
date: 2025-01-18 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
|
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
|
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
|