console-adapter-rails 0.2.1 → 0.3.1

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: f0964f1321e9dbcf587a9d6a6ab1a8b313eeb28110cda8b0e73fa96a7b1efcb3
4
- data.tar.gz: e7c124ea18335c487c986ffda35af7aa6bb5958b5f03e903f3cc4e11fd74c516
3
+ metadata.gz: bf1c2585df1d0f272caf432c85850eaf84012672b158245254da3ab64fb8bd99
4
+ data.tar.gz: bd9e3e238bf51e9c8a7bfb230ef91b38b96abe73f4c7bb2c52d62ebc9481a2dd
5
5
  SHA512:
6
- metadata.gz: b5c28d4ad22f2547b9a44d728404f90effffb4dc82928bdf5030d61105c63132a33671ead487d71c601e24ac881a49af81bb65d22524f7d6837759d42c86f912
7
- data.tar.gz: a75abdbe06bab6b33a45d0280eb1f58421a9fab370f29ef69fcf431faf67ac325f0aaafabf883305e3ba56184c3eec850a2deccd4fcb87b3f56414032cb819ab
6
+ metadata.gz: 701130eca76de4a6096a91c5d9fbb9d89c608c0f1c4ea16194e71d4c33713a61af8006d0113fc2fdd07a0dd3968fa3b61417caf59e3c91b916bd5d931c564a49
7
+ data.tar.gz: 7327c7c378eb29e3b925e450d3777aa8c1b6c96250471a0066c01f04ca78bf19933c56a02397bfb376e1ef13858f7c8e58ab5f569560ce6631991ea34de52332
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,70 @@
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
+ if attribute.came_from_user?
57
+ attribute.type.class.name
58
+ else
59
+ value
60
+ end
61
+ end
62
+ end
63
+
64
+ def self.apply!(notifications: ActiveSupport::Notifications)
65
+ LogSubscriber.attach_to(:active_record, LogSubscriber.new, notifications)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -5,6 +5,19 @@
5
5
 
6
6
  require 'console/compatible/logger'
7
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
+
8
21
  module Console
9
22
  module Adapter
10
23
  # A Rails adapter for the console logger.
@@ -6,7 +6,7 @@
6
6
  module Console
7
7
  module Adapter
8
8
  module Rails
9
- VERSION = "0.2.1"
9
+ VERSION = "0.3.1"
10
10
  end
11
11
  end
12
12
  end
@@ -3,83 +3,30 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2023, by Samuel Williams.
5
5
 
6
- require 'console'
7
-
8
- require 'active_support/log_subscriber'
9
- require 'active_support/tagged_logging'
10
- require 'action_controller/log_subscriber'
11
- require 'action_view/log_subscriber'
12
- require 'active_job/log_subscriber'
13
-
14
6
  require_relative 'rails/logger'
15
- require_relative 'rails/active_support'
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)
72
- if notifications
73
- # Clear out all the existing subscribers otherwise you'll get double the logs:
74
- notifications.notifier = ActiveSupport::Notifications::Fanout.new
75
- end
76
-
77
18
  if configuration
78
19
  Logger.apply!(configuration: configuration)
79
20
  end
80
21
 
81
- # Attach our log subscriber for action_controller events:
82
- LogSubscriber.attach_to(:action_controller, LogSubscriber.new, notifications)
22
+ if notifications
23
+ # Clear out all the existing subscribers otherwise you'll get double the logs:
24
+ notifications.notifier = ActiveSupport::Notifications::Fanout.new
25
+
26
+ # Add our own subscribers:
27
+ Rails::ActionController.apply!(notifications: notifications)
28
+ Rails::ActiveRecord.apply!(notifications: notifications)
29
+ end
83
30
  end
84
31
  end
85
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.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -74,7 +74,8 @@ 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
78
79
  - lib/console/adapter/rails/logger.rb
79
80
  - lib/console/adapter/rails/version.rb
80
81
  - license.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