any_logger 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +29 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +16 -0
- data/Rakefile +12 -0
- data/lib/any_logger/configuration.rb +72 -0
- data/lib/any_logger/example/controller_subscriber.rb +109 -0
- data/lib/any_logger/example/rack_logger.rb +17 -0
- data/lib/any_logger/initializer.rb +23 -0
- data/lib/any_logger/version.rb +5 -0
- data/lib/any_logger.rb +21 -0
- data/sig/any_logger.rbs +4 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2358954386fe713d2a0ecd983defb8d9490cb0e343c19fb7837984d031a1bd2
|
4
|
+
data.tar.gz: 7885ea10527db0410b819c16e91b9baf7f503ceb380f69a5098cf948e72536ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 32f35f4c25058f116c5503bdd750a198e3ad6e14924d9075882226b15357aa3b6b565ce8b3d6a10bcfcaf3a0d11f09d161186759a107470d66fef8669d64f21d
|
7
|
+
data.tar.gz: acccf7756be61b8171373dcd9e96087951c41954a67eb69a77ed282204b862298ddc6badb9b98698b11ea28e8defb38b5a784c30bf1bbad050ddb25627771e6e
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.4.0
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
EnforcedStyle: double_quotes
|
6
|
+
|
7
|
+
Style/StringLiteralsInInterpolation:
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
|
10
|
+
Style/FrozenStringLiteralComment:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/Documentation:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Style/MutableConstant:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/SymbolArray:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/AccessModifierDeclarations:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/RegexpLiteral:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Bundler/OrderedGems:
|
29
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.4.1
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 milkeclair
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# AnyLogger
|
2
|
+
|
3
|
+
Easier to change LogSubscribers in rails.
|
4
|
+
|
5
|
+
### Example
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
AnyLogger.configure do |config|
|
9
|
+
config.logger = Rails::Rack::Logger # default: AnyLogger::Example::RackLogger
|
10
|
+
config.swap :action_controller, AnyLogger::Example::ControllerSubscriber
|
11
|
+
config.detach :action_view
|
12
|
+
config.attach :active_record, MyLogger::ModelSubscriber
|
13
|
+
end
|
14
|
+
|
15
|
+
AnyLogger.start
|
16
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,72 @@
|
|
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
|
+
require_relative "example/rack_logger"
|
12
|
+
|
13
|
+
module AnyLogger
|
14
|
+
class Configuration
|
15
|
+
include Singleton
|
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
|
+
DEFAULT_SUBSCRIBER_OPTIONS = { klass: nil, detachable: false, attachable: false }
|
28
|
+
|
29
|
+
private_constant :DEFAULT_SUBSCRIBER_OPTIONS
|
30
|
+
|
31
|
+
attr_reader :config
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
@config = {
|
35
|
+
logger: ::AnyLogger::Example::RackLogger,
|
36
|
+
subscribers: DEFAULT_SUBSCRIBERS.dup.transform_values { |k| DEFAULT_SUBSCRIBER_OPTIONS.dup.merge(klass: k) }
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def logger
|
41
|
+
@config[:logger]
|
42
|
+
end
|
43
|
+
|
44
|
+
def logger=(klass)
|
45
|
+
@config[:logger] = klass
|
46
|
+
end
|
47
|
+
|
48
|
+
def subscribers
|
49
|
+
@config[:subscribers]
|
50
|
+
end
|
51
|
+
|
52
|
+
def swap(key, klass)
|
53
|
+
return unless @config[:subscribers].key?(key)
|
54
|
+
|
55
|
+
@config[:subscribers][key] = { klass: klass, detachable: true, attachable: true }
|
56
|
+
end
|
57
|
+
|
58
|
+
def detach(key, klass = nil)
|
59
|
+
return unless @config[:subscribers].key?(key)
|
60
|
+
|
61
|
+
@config[:subscribers][key][:detachable] = true
|
62
|
+
@config[:subscribers][key][:klass] = klass if klass
|
63
|
+
end
|
64
|
+
|
65
|
+
def attach(key, klass)
|
66
|
+
return unless @config[:subscribers].key?(key)
|
67
|
+
|
68
|
+
@config[:subscribers][key][:attachable] = true
|
69
|
+
@config[:subscribers][key][:klass] = klass
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require "active_support/log_subscriber"
|
2
|
+
require "rack/utils"
|
3
|
+
|
4
|
+
module AnyLogger
|
5
|
+
module Example
|
6
|
+
class ControllerSubscriber < ActiveSupport::LogSubscriber
|
7
|
+
UNNECESSARY_PATHS = ["/favicon.ico"]
|
8
|
+
|
9
|
+
REDIRECT_CODES = [:moved_permanently, :found, :see_other, :temporary_redirect, :permanent_redirect]
|
10
|
+
|
11
|
+
private_constant :UNNECESSARY_PATHS, :REDIRECT_CODES
|
12
|
+
|
13
|
+
def process_action(event)
|
14
|
+
@event = event
|
15
|
+
@payload = event.payload
|
16
|
+
@headers = @payload[:response].headers.symbolize_keys if @payload[:response]&.headers
|
17
|
+
|
18
|
+
return if match_unnecessary_path?
|
19
|
+
|
20
|
+
info { formatted_message }
|
21
|
+
end
|
22
|
+
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# [GET] HogeController#index for: 127.0.0.1 at: 2025-01-01 00:00:00 +0900
|
26
|
+
# path: "/hoge?foo=bar"
|
27
|
+
# status: 200 in 100ms (view: 50ms | db: 50ms)
|
28
|
+
# params: {"foo" => "bar"}
|
29
|
+
# redirect: "/fuga"
|
30
|
+
private def formatted_message
|
31
|
+
<<~MESSAGE
|
32
|
+
|
33
|
+
#{l_method} #{l_action} #{l_for} #{l_at}
|
34
|
+
#{l_path}
|
35
|
+
#{l_status} #{l_duration} (#{l_view_runtime} | #{l_db_runtime})
|
36
|
+
#{"#{l_params}\n" if @payload[:params].present?}#{l_redirect if @headers[:location].present?}
|
37
|
+
MESSAGE
|
38
|
+
end
|
39
|
+
|
40
|
+
private def match_unnecessary_path?
|
41
|
+
UNNECESSARY_PATHS.include?(@payload[:path])
|
42
|
+
end
|
43
|
+
|
44
|
+
private def l_method
|
45
|
+
"[#{@payload[:method].upcase}]"
|
46
|
+
end
|
47
|
+
|
48
|
+
private def l_action
|
49
|
+
"#{@payload[:controller]}##{@payload[:action]}"
|
50
|
+
end
|
51
|
+
|
52
|
+
private def l_for
|
53
|
+
"for: #{@payload[:request].remote_ip}"
|
54
|
+
end
|
55
|
+
|
56
|
+
private def l_at
|
57
|
+
"at: #{conversion_event_time_to_datetime}"
|
58
|
+
end
|
59
|
+
|
60
|
+
private def l_path
|
61
|
+
"🔍 path: \"#{@payload[:path]}\""
|
62
|
+
end
|
63
|
+
|
64
|
+
private def l_status
|
65
|
+
"🧱 status: #{@payload[:status]}"
|
66
|
+
end
|
67
|
+
|
68
|
+
private def l_duration
|
69
|
+
"in #{@event.duration&.round(2) || 0}ms"
|
70
|
+
end
|
71
|
+
|
72
|
+
private def l_view_runtime
|
73
|
+
"view: #{@payload[:view_runtime]&.round(2) || 0}ms"
|
74
|
+
end
|
75
|
+
|
76
|
+
private def l_db_runtime
|
77
|
+
"db: #{@payload[:db_runtime]&.round(2) || 0}ms"
|
78
|
+
end
|
79
|
+
|
80
|
+
private def l_params
|
81
|
+
@payload[:params] = except_unnecessary_params
|
82
|
+
"📝 params: #{@payload[:params]&.inspect || {}}"
|
83
|
+
end
|
84
|
+
|
85
|
+
private def l_redirect
|
86
|
+
return unless REDIRECT_CODES.any? { @payload[:status] == Rack::Utils.status_code(it) }
|
87
|
+
|
88
|
+
ellipsised_url = ellipsis_scheme_and_authority(@headers[:location])
|
89
|
+
"🚀 redirect: \"#{ellipsised_url}\""
|
90
|
+
end
|
91
|
+
|
92
|
+
private def conversion_event_time_to_datetime
|
93
|
+
now_monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
94
|
+
elapsed = now_monotonic - @event.time
|
95
|
+
now = Time.now.to_f
|
96
|
+
|
97
|
+
Time.at((now - elapsed).to_i)
|
98
|
+
end
|
99
|
+
|
100
|
+
private def except_unnecessary_params
|
101
|
+
@payload[:params].except("controller", "action")
|
102
|
+
end
|
103
|
+
|
104
|
+
private def ellipsis_scheme_and_authority(url)
|
105
|
+
url.gsub(/\Ahttps?:\/\/[^\/]+/, "")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "active_support/log_subscriber"
|
2
|
+
|
3
|
+
module AnyLogger
|
4
|
+
module Example
|
5
|
+
class RackLogger
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
@app.call(env)
|
12
|
+
ensure
|
13
|
+
ActiveSupport::LogSubscriber.flush_all!
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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
|
+
Configuration::DEFAULT_SUBSCRIBERS.each do |key, default_subscriber|
|
14
|
+
subscribers = AnyLogger.config.subscribers
|
15
|
+
detachable = subscribers[key][:detachable]
|
16
|
+
attachable = subscribers[key][:attachable]
|
17
|
+
|
18
|
+
default_subscriber.detach_from(key) if detachable
|
19
|
+
subscribers[key][:klass].attach_to(key) if attachable
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/any_logger.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative "any_logger/version"
|
2
|
+
require_relative "any_logger/configuration"
|
3
|
+
require_relative "any_logger/initializer"
|
4
|
+
|
5
|
+
module AnyLogger
|
6
|
+
def self.configure(&block)
|
7
|
+
block.call(config)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.start
|
11
|
+
Initializer.run
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.config
|
15
|
+
Configuration.instance
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.subscribers
|
19
|
+
config.config[:subscribers]
|
20
|
+
end
|
21
|
+
end
|
data/sig/any_logger.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: any_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- milkeclair
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-01-13 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: actionpack
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activejob
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: activerecord
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: activestorage
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
type: :runtime
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: activesupport
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
type: :runtime
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
description: easy swap for LogSubscribers in rails
|
83
|
+
email:
|
84
|
+
- milkeclair.black@gmail.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".rspec"
|
90
|
+
- ".rubocop.yml"
|
91
|
+
- ".ruby-version"
|
92
|
+
- CHANGELOG.md
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/any_logger.rb
|
97
|
+
- lib/any_logger/configuration.rb
|
98
|
+
- lib/any_logger/example/controller_subscriber.rb
|
99
|
+
- lib/any_logger/example/rack_logger.rb
|
100
|
+
- lib/any_logger/initializer.rb
|
101
|
+
- lib/any_logger/version.rb
|
102
|
+
- sig/any_logger.rbs
|
103
|
+
homepage: https://github.com/milkeclair/any_logger
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata:
|
107
|
+
homepage_uri: https://github.com/milkeclair/any_logger
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: 3.4.0
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubygems_version: 3.6.2
|
123
|
+
specification_version: 4
|
124
|
+
summary: easy swap for LogSubscribers in rails
|
125
|
+
test_files: []
|