console-adapter-rails 0.2.1 → 0.3.0

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: 4113b18d055a8163f32e2faf01bc4297c32c1ee36cc8fdbb74202b87b1532e92
4
+ data.tar.gz: 0f53176b8cf64ecbd393ada761e515d97c995da78ee093ec8c32e2b61d143838
5
5
  SHA512:
6
- metadata.gz: b5c28d4ad22f2547b9a44d728404f90effffb4dc82928bdf5030d61105c63132a33671ead487d71c601e24ac881a49af81bb65d22524f7d6837759d42c86f912
7
- data.tar.gz: a75abdbe06bab6b33a45d0280eb1f58421a9fab370f29ef69fcf431faf67ac325f0aaafabf883305e3ba56184c3eec850a2deccd4fcb87b3f56414032cb819ab
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
@@ -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.0"
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.0
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