console-adapter-rails 0.2.0 → 0.3.0

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: dd24ef25ea142ca007009e32bf72a8b15addbc51fcd68e0b8764f4aa01b715db
4
- data.tar.gz: 64cad4283b64c45dde83f84af277354cd184eacdb1f72ac6dd34e01d65e52009
3
+ metadata.gz: 4113b18d055a8163f32e2faf01bc4297c32c1ee36cc8fdbb74202b87b1532e92
4
+ data.tar.gz: 0f53176b8cf64ecbd393ada761e515d97c995da78ee093ec8c32e2b61d143838
5
5
  SHA512:
6
- metadata.gz: 06f40cdd266f29ff91da9a63b8ff6dc53ba09af52718b0c1cc2636dd73870ed006fcbf83f043183c9678bc39dfc6b0d2255675ce29da6c996c7ca59934d59e71
7
- data.tar.gz: 963a690fb77153a6509c4e8783b982ec02bf750f11dc8df63d7445ca5732746bbcc21ba5ba1c9e984680ac490f19a179fdac0e29a889b3a593a54a87124c15f6
6
+ metadata.gz: e2ffc06b392ca691d30c9d7ede74dbed6c56ab8a70ce794304a6d18c342b575c4b0479cc4f13e8d0508eb65889d482209856325f69c3a0145ec575d58b319b57
7
+ data.tar.gz: 46e0538d0ecc74f4bc03af56253804eb8178baa44bda5f84e5da96c29b862a47c17347ee3108bae8987d88ba398022348d6bd5aa70cea1e68d4fbfd6945a96cc
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2023, by Samuel Williams.
5
+
6
+ require 'console'
7
+
8
+ require 'action_controller/log_subscriber'
9
+
10
+ module Console
11
+ module Adapter
12
+ # A Rails adapter for the console logger.
13
+ module Rails
14
+ module ActionController
15
+ # A Rails log subscriber which is compatible with `Console::Logger`. It receives events from `ActiveSupport::Notifications` and logs them to the console.
16
+ class LogSubscriber < ::ActiveSupport::LogSubscriber
17
+ # Log an ActionController `process_action` event.
18
+ #
19
+ # Includes the following fields:
20
+ # - `subject`: "process_action.action_controller"
21
+ # - `controller`: The name of the controller.
22
+ # - `action`: The action performed.
23
+ # - `format`: The format of the response.
24
+ # - `method`: The HTTP method of the request.
25
+ # - `path`: The path of the request.
26
+ # - `status`: The HTTP status code of the response.
27
+ # - `view_runtime`: The time spent rendering views in milliseconds.
28
+ # - `db_runtime`: The time spent querying the database in milliseconds.
29
+ # - `location`: The redirect location if any.
30
+ # - `allocations`: The number of allocations performed.
31
+ # - `duration`: The total time spent processing the request in milliseconds.
32
+ def process_action(event)
33
+ payload = event.payload.dup
34
+
35
+ # This may contain sensitive information:
36
+ params = payload.delete(:params)
37
+
38
+ # These objects are not useful and may not serialize correctly:
39
+ headers = payload.delete(:headers)
40
+ request = payload.delete(:request)
41
+ response = payload.delete(:response)
42
+
43
+ if request and ip = request.remote_ip
44
+ payload[:source_address] = ip
45
+ end
46
+
47
+ if response and headers = response.headers
48
+ # Extract redirect location if any:
49
+ location = response.headers['Location'] || response.headers['location']
50
+ if location
51
+ payload[:location] = location
52
+ end
53
+ end
54
+
55
+ payload[:allocations] = event.allocations
56
+ payload[:duration] = event.duration
57
+
58
+ Console.logger.info(event.name, **payload)
59
+ end
60
+ end
61
+
62
+ def self.apply!(notifications: ActiveSupport::Notifications)
63
+ LogSubscriber.attach_to(:action_controller, LogSubscriber.new, notifications)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2023, by Samuel Williams.
5
+
6
+ require 'console'
7
+
8
+ require 'active_record/log_subscriber'
9
+
10
+ module Console
11
+ module Adapter
12
+ module Rails
13
+ module ActiveRecord
14
+ class LogSubscriber < ::ActiveSupport::LogSubscriber
15
+ IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN", "TRANSACTION"]
16
+
17
+ # Log an ActiveRecord sql event.
18
+ #
19
+ # Includes the following fields:
20
+ # - `subject`: "process_action.action_controller"
21
+ # - `sql`: The SQL query itself.
22
+ # - `name`: The name of the query.
23
+ # - `binds`: The bind parameters as an array of name-value pairs.
24
+ # - `allocations`: The number of allocations performed.
25
+ # - `duration`: The total time spent processing the request in milliseconds.
26
+ def sql(event)
27
+ return if IGNORE_PAYLOAD_NAMES.include?(event.payload[:name])
28
+
29
+ payload = event.payload.dup
30
+
31
+ # We don't want to dump the connection:
32
+ connection = payload.delete(:connection)
33
+
34
+ update_binds(payload)
35
+
36
+ payload[:allocations] = event.allocations
37
+ payload[:duration] = event.duration
38
+
39
+ Console.logger.info(event.name, **payload)
40
+ end
41
+
42
+ private
43
+
44
+ def update_binds(payload)
45
+ binds = payload.delete(:binds)
46
+ type_casted_binds = payload.delete(:type_casted_binds)
47
+
48
+ if binds&.any? and type_casted_binds
49
+ payload[:binds] = binds.map.with_index do |attribute, index|
50
+ [attribute.name, self.bind_value(attribute, type_casted_binds[index])]
51
+ end
52
+ end
53
+ end
54
+
55
+ def bind_value(attribute, value)
56
+ value = self.filter(attribute.name, value)
57
+
58
+ if attribute.type.binary?
59
+ "<#{value.bytesize} bytes of binary data>"
60
+ else
61
+ value
62
+ end
63
+ end
64
+
65
+ def filter(name, value)
66
+ ::ActiveRecord::Base.inspection_filter.filter_param(name, value)
67
+ end
68
+ end
69
+
70
+ def self.apply!(notifications: ActiveSupport::Notifications)
71
+ LogSubscriber.attach_to(:active_record, LogSubscriber.new, notifications)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2023, by Samuel Williams.
5
+
6
+ require 'console/compatible/logger'
7
+
8
+ require 'active_support/logger'
9
+ require 'active_support/tagged_logging'
10
+ require 'active_support/logger_silence'
11
+
12
+ if ActiveSupport::Logger.respond_to?(:logger_outputs_to?)
13
+ # https://github.com/rails/rails/issues/44800
14
+ class ActiveSupport::Logger
15
+ def self.logger_outputs_to?(*)
16
+ true
17
+ end
18
+ end
19
+ end
20
+
21
+ module Console
22
+ module Adapter
23
+ # A Rails adapter for the console logger.
24
+ module Rails
25
+ class Logger < ::Console::Compatible::Logger
26
+ include ::ActiveSupport::LoggerSilence
27
+
28
+ def self.apply!(configuration: ::Rails.configuration)
29
+ # Set the logger to a compatible logger to catch `Rails.logger` output:
30
+ configuration.logger = ActiveSupport::TaggedLogging.new(
31
+ Logger.new(::Rails)
32
+ )
33
+
34
+ # Delete `Rails::Rack::Logger` as it also doubles up on request logs:
35
+ configuration.middleware.delete ::Rails::Rack::Logger
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -6,7 +6,7 @@
6
6
  module Console
7
7
  module Adapter
8
8
  module Rails
9
- VERSION = "0.2.0"
9
+ VERSION = "0.3.0"
10
10
  end
11
11
  end
12
12
  end
@@ -3,89 +3,30 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2023, by Samuel Williams.
5
5
 
6
- require 'console'
7
- require 'console/compatible/logger'
8
-
9
- require 'active_support/log_subscriber'
10
- require 'active_support/tagged_logging'
11
- require 'action_controller/log_subscriber'
12
- require 'action_view/log_subscriber'
13
- require 'active_job/log_subscriber'
14
-
15
- require_relative 'rails/active_support'
6
+ require_relative 'rails/logger'
7
+ require_relative 'rails/action_controller'
8
+ require_relative 'rails/active_record'
16
9
 
17
10
  module Console
18
11
  module Adapter
19
12
  # A Rails adapter for the console logger.
20
13
  module Rails
21
- # A Rails log subscriber which is compatible with `Console::Logger`. It receives events from `ActiveSupport::Notifications` and logs them to the console.
22
- class LogSubscriber < ActiveSupport::LogSubscriber
23
- # Log controller action events.
24
- #
25
- # Includes the following fields:
26
- # - `subject`: "process_action.action_controller"
27
- # - `controller`: The name of the controller.
28
- # - `action`: The action performed.
29
- # - `format`: The format of the response.
30
- # - `method`: The HTTP method of the request.
31
- # - `path`: The path of the request.
32
- # - `status`: The HTTP status code of the response.
33
- # - `view_runtime`: The time spent rendering views in milliseconds.
34
- # - `db_runtime`: The time spent querying the database in milliseconds.
35
- # - `location`: The redirect location if any.
36
- # - `allocations`: The number of allocations performed.
37
- # - `duration`: The total time spent processing the request in milliseconds.
38
- def process_action(event)
39
- payload = event.payload.dup
40
-
41
- # This may contain sensitive information:
42
- params = payload.delete(:params)
43
-
44
- # These objects are not useful and may not serialize correctly:
45
- headers = payload.delete(:headers)
46
- request = payload.delete(:request)
47
- response = payload.delete(:response)
48
-
49
- if request and ip = request.remote_ip
50
- payload[:source_address] = ip
51
- end
52
-
53
- if response and headers = response.headers
54
- # Extract redirect location if any:
55
- location = response.headers['Location'] || response.headers['location']
56
- if location
57
- payload[:location] = location
58
- end
59
- end
60
-
61
- payload[:allocations] = event.allocations
62
- payload[:duration] = event.duration
63
-
64
- Console.logger.info(event.name, **payload)
65
- end
66
- end
67
-
68
14
  # Apply the Rails adapter to the current process and Rails application.
69
15
  # @parameter notifications [ActiveSupport::Notifications] The notifications object to use.
70
16
  # @parameter configuration [Rails::Configuration] The configuration object to use.
71
17
  def self.apply!(notifications: ActiveSupport::Notifications, configuration: ::Rails.configuration)
18
+ if configuration
19
+ Logger.apply!(configuration: configuration)
20
+ end
21
+
72
22
  if notifications
73
23
  # Clear out all the existing subscribers otherwise you'll get double the logs:
74
24
  notifications.notifier = ActiveSupport::Notifications::Fanout.new
75
- end
76
-
77
- if configuration
78
- # Set the logger to a compatible logger to catch `Rails.logger` output:
79
- configuration.logger = ActiveSupport::TaggedLogging.new(
80
- Console::Compatible::Logger.new(::Rails)
81
- )
82
25
 
83
- # Delete `Rails::Rack::Logger` as it also doubles up on request logs:
84
- configuration.middleware.delete ::Rails::Rack::Logger
26
+ # Add our own subscribers:
27
+ Rails::ActionController.apply!(notifications: notifications)
28
+ Rails::ActiveRecord.apply!(notifications: notifications)
85
29
  end
86
-
87
- # Attach our log subscriber for action_controller events:
88
- LogSubscriber.attach_to(:action_controller, LogSubscriber.new, notifications)
89
30
  end
90
31
  end
91
32
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console-adapter-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -37,7 +37,7 @@ cert_chain:
37
37
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
38
38
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
39
39
  -----END CERTIFICATE-----
40
- date: 2023-08-11 00:00:00.000000000 Z
40
+ date: 2023-08-18 00:00:00.000000000 Z
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: console
@@ -74,7 +74,9 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - lib/console/adapter/rails.rb
77
- - lib/console/adapter/rails/active_support.rb
77
+ - lib/console/adapter/rails/action_controller.rb
78
+ - lib/console/adapter/rails/active_record.rb
79
+ - lib/console/adapter/rails/logger.rb
78
80
  - lib/console/adapter/rails/version.rb
79
81
  - license.md
80
82
  - readme.md
metadata.gz.sig CHANGED
Binary file
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2023, by Samuel Williams.
5
-
6
- require 'active_support/logger'
7
-
8
- if ActiveSupport::Logger.respond_to?(:logger_outputs_to?)
9
- # https://github.com/rails/rails/issues/44800
10
- class ActiveSupport::Logger
11
- def self.logger_outputs_to?(*)
12
- true
13
- end
14
- end
15
- end