action_tracer 0.2.4 → 0.2.5

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: 0061b0039b327ef7923b1ccb60a49d3ecc299f93fbf13061087b90f0ae511865
4
- data.tar.gz: bba51412925a6b3d23aeabc2e68c58ce6c572323a7ac4ba749f0de077dcc42af
3
+ metadata.gz: 7214356083ad06c96fe92939aa010ea7ad918d2273431eedecd9edfe3d358f3d
4
+ data.tar.gz: 13e9c9b139723297a2a92407786761a7f1df0bfc58a793e980daaa7232d665f8
5
5
  SHA512:
6
- metadata.gz: 25be298bf88d787985c8b056fec084b225623de7d904b1872a541fcd3da338214ed31169492e4fceae38ca7568c2592280ce3b7483f608a3c8f6fba7cbfd717d
7
- data.tar.gz: 4f90d3dfef39cdf116bff177057a74d8f889a33a23e66a6e68685057acef8cc4e15ae8d432d413848621d6009309f65c14718d2738c2551a02a0ff234e2d4a2f
6
+ metadata.gz: da46b25d92b0c5fba47b21cda6ccd555a6bf89dd9bcb40be436bbc55b7dc4c7cc4cce1d662927d26f492bc0fda5eba4f2ff1cddc0347b2f7f3450978dd0c7beb
7
+ data.tar.gz: 074af54035733e26735e927a84e0e1bc2717a28cddb583af30035deae31f29d3bd9391eed8830b4a684eb40355f75b67b6fbd031fd48d0775dfdd4ad2112613f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.5
2
+
3
+ * Be able to inject logger.
4
+ * Fix around_action ordering bug.
5
+ * Be able to omit source_location path.
6
+
1
7
  ## 0.2.4
2
8
 
3
9
  * Fix bug for Rails7.x support.
data/README.md CHANGED
@@ -60,12 +60,12 @@ it will put logs for `log/action_tracer.log` like this:
60
60
  ```log
61
61
  I, [2020-09-27T03:25:43.018298 #1] INFO -- : ["APPLIED", :set_turbolinks_location_header_from_session, "/usr/local/bundle/gems/turbolinks-5.2.1/lib/turbolinks/redirection.rb", 43]
62
62
  I, [2020-09-27T03:25:43.019410 #1] INFO -- : ["APPLIED", :verify_authenticity_token, "/usr/local/bundle/gems/actionpack-5.1.7/lib/action_controller/metal/request_forgery_protection.rb", 211]
63
- I, [2020-09-27T03:25:43.021131 #1] INFO -- : ["APPLIED", :require_login, "/myapp/app/controllers/awesome_controller.rb", 17]
64
- I, [2020-09-27T03:25:43.022063 #1] INFO -- : ["NO_APPLIED", :set_awesome, "/myapp/app/controllers/awesome_controller.rb", 25]
65
- I, [2020-09-27T03:25:43.023716 #1] INFO -- : ["APPLIED", :with_readonly, "/myapp/app/controllers/awesome_controller.rb", 21]
66
- I, [2020-09-27T03:25:43.025547 #1] INFO -- : ["ACTION", :index, "/myapp/app/controllers/awesome_controller.rb", 7]
67
- I, [2020-09-27T03:25:43.026297 #1] INFO -- : ["APPLIED", :with_readonly, "/myapp/app/controllers/awesome_controller.rb", 21]
68
- I, [2020-09-27T03:25:43.027203 #1] INFO -- : ["APPLIED", :store_location, "/myapp/app/controllers/awesome_controller.rb", 27]
63
+ I, [2020-09-27T03:25:43.021131 #1] INFO -- : ["APPLIED", :require_login, "app/controllers/awesome_controller.rb", 17]
64
+ I, [2020-09-27T03:25:43.022063 #1] INFO -- : ["NO_APPLIED", :set_awesome, "app/controllers/awesome_controller.rb", 25]
65
+ I, [2020-09-27T03:25:43.023716 #1] INFO -- : ["APPLIED", :with_readonly, "app/controllers/awesome_controller.rb", 21]
66
+ I, [2020-09-27T03:25:43.025547 #1] INFO -- : ["ACTION", :index, "app/controllers/awesome_controller.rb", 7]
67
+ I, [2020-09-27T03:25:43.026297 #1] INFO -- : ["APPLIED", :with_readonly, "app/controllers/awesome_controller.rb", 21]
68
+ I, [2020-09-27T03:25:43.027203 #1] INFO -- : ["APPLIED", :store_location, "app/controllers/awesome_controller.rb", 27]
69
69
  I, [2020-09-27T03:25:43.030074 #1] INFO -- : ["APPLIED", :verify_same_origin_request, "/usr/local/bundle/gems/actionpack-5.1.7/lib/action_controller/metal/request_forgery_protection.rb", 240]
70
70
  I, [2020-09-27T03:25:43.030776 #1] INFO -- :
71
71
  ```
@@ -78,7 +78,7 @@ Notice `around_action` is put 2 times around action though called 1 time.
78
78
  Normally log is put in this format:
79
79
 
80
80
  ```ruby
81
- ["APPLIED", :require_login, "/myapp/app/controllers/awesome_controller.rb", 17]
81
+ ["APPLIED", :require_login, "app/controllers/awesome_controller.rb", 17]
82
82
  ```
83
83
 
84
84
  1. State. One of `APPLIED`, `NO_APPLIED` and `ACTION`.
@@ -86,7 +86,7 @@ Normally log is put in this format:
86
86
  `NO_APPLIED`: Filter is registered but not executed.
87
87
  `ACTION`: Called action.
88
88
  2. Method name. When filter is a Proc, just put `:Proc`.
89
- 3. File path. It's absolute path.
89
+ 3. File path. In your application directory, it's omitted path. See Configuration section for details.
90
90
  4. Method line no.
91
91
 
92
92
  When filter is an object, log is put in this format:
@@ -95,7 +95,31 @@ When filter is an object, log is put in this format:
95
95
  ["UNRECOGNIZED", #<Awesome::Object:0x00007f95c35768f8>]
96
96
  ```
97
97
 
98
- We can't recgnize the filter is actually executed or not.
98
+ We can't recognize the filter is actually executed or not.
99
+
100
+ ### Configuration
101
+
102
+ To customize how to output log, add `config/initializers/action_tracer.rb`:
103
+
104
+ ```ruby
105
+ ActionTracer.configure do |config|
106
+ # when you want to omit source_location path
107
+ config.omitted_source_location_paths = ["#{Dir.pwd}/"]
108
+ # e.g.
109
+ # config.omitted_source_location_paths = []
110
+ # ["APPLIED", :require_login, "/Users/makicamel/works/github.com/makicamel/myapp/app/controllers/awesome_controller.rb", 17]
111
+ # config.omitted_source_location_paths = ["#{Dir.pwd}/"]
112
+ # ["APPLIED", :require_login, "app/controllers/awesome_controller.rb", 17]
113
+
114
+ # when you want to output log to stdout
115
+ config.logger = Rails.logger
116
+ end
117
+ ```
118
+
119
+ - `omitted_source_location_paths`: Omitted source_location path. In default `["#{Dir.pwd}/"]`.
120
+ - `logger`: Injectable logger. For example, to specify to output log to stdout.
121
+ - `directory`: The directory to put log. In default `/log/`.
122
+ - `file_name`: The log file name. In default `action_tracer.log`.
99
123
 
100
124
  ## CommingFeatures
101
125
 
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionTracer
4
+ class Configration
5
+ attr_writer :app_path, :directory, :file_name, :logger, :omitted_source_location_paths
6
+
7
+ def app_path
8
+ @app_path ||= Dir.pwd
9
+ end
10
+
11
+ def directory
12
+ @directory ||= '/log/'
13
+ end
14
+
15
+ def file_name
16
+ @file_name ||= 'action_tracer.log'
17
+ end
18
+
19
+ def logger
20
+ @logger ||= Logger.new(app_path + directory + file_name)
21
+ end
22
+
23
+ def omitted_source_location_paths
24
+ @omitted_source_location_paths ||= ["#{Dir.pwd}/"]
25
+ end
26
+
27
+ def omitted_source_location_path
28
+ @omitted_source_location_path ||= %r{#{omitted_source_location_paths.join('|')}}
29
+ end
30
+ end
31
+
32
+ class << self
33
+ def configure
34
+ yield config
35
+ end
36
+
37
+ def config
38
+ @config ||= Configration.new
39
+ end
40
+
41
+ def logger
42
+ @logger ||= @config.logger
43
+ end
44
+ end
45
+ end
@@ -7,19 +7,30 @@ module ActionTracer
7
7
  PROC = :Proc
8
8
  attr_reader :applied
9
9
 
10
- def initialize(filter, method:)
10
+ def initialize(filter, kind:, method:)
11
11
  @filter = filter.is_a?(Symbol) ? filter : PROC
12
+ @kind = kind
12
13
  @method = method
13
14
  @applied = ActionTracer.applied_filters.include? filter
14
15
  end
15
16
 
16
17
  def to_a
17
18
  if @method.respond_to? :source_location
18
- [APPLIED[@applied], @filter, *@method.source_location]
19
+ source_location, line_number = *@method.source_location
20
+ source_location = source_location.sub(::ActionTracer.config.omitted_source_location_path, '')
21
+ [APPLIED[@applied], @filter, source_location, line_number].compact
19
22
  else
20
23
  [APPLIED[:unrecognized], @method]
21
24
  end
22
25
  end
26
+
27
+ def before?
28
+ @kind == :before || @kind == :around
29
+ end
30
+
31
+ def after?
32
+ @kind == :after || @kind == :around
33
+ end
23
34
  end
24
35
 
25
36
  class Action
@@ -34,7 +45,9 @@ module ActionTracer
34
45
  end
35
46
 
36
47
  def to_a
37
- [APPLIED[:action], @name, *@method.source_location]
48
+ source_location, line_number = *@method.source_location
49
+ source_location = source_location.sub(::ActionTracer.config.omitted_source_location_path, '')
50
+ [APPLIED[:action], @name, source_location, line_number].compact
38
51
  end
39
52
 
40
53
  def self.nil_method
@@ -44,23 +57,23 @@ module ActionTracer
44
57
  end
45
58
 
46
59
  class Filters
47
- def initialize(before = [], after = [], around = [], action:)
48
- @before = before
49
- @after = after
50
- @around = around
60
+ def initialize(filters:, action:)
61
+ @filters = filters
51
62
  @action = action
52
63
  end
53
64
 
54
65
  class << self
55
66
  def build(controller)
56
- filters = { before: [], after: [], around: [] }
57
- raw_filters = controller.__callbacks[:process_action].send(:chain).group_by(&:kind)
58
- raw_filters.each do |kind, filter|
59
- filters[kind] = filter.map { |f| f.__send__(filter_method) }.map do |f|
60
- Filter.new(f, method: f.is_a?(Symbol) ? controller.method(f) : f)
61
- end
67
+ raw_filters = controller.__callbacks[:process_action].__send__(:chain)
68
+ filters = raw_filters.map do |raw_filter|
69
+ filter = raw_filter.__send__(filter_method)
70
+ Filter.new(
71
+ filter,
72
+ kind: raw_filter.kind,
73
+ method: filter.is_a?(Symbol) ? controller.method(filter) : filter
74
+ )
62
75
  end
63
- new(filters[:before], filters[:after], filters[:around], action: Action.build(controller))
76
+ new(filters: filters, action: Action.build(controller))
64
77
  end
65
78
 
66
79
  private
@@ -73,17 +86,17 @@ module ActionTracer
73
86
  def print
74
87
  invoked_before.map(&:to_a).each { |filter| ActionTracer.logger.info filter }
75
88
  ActionTracer.logger.info @action.to_a
76
- invoked_after.map(&:to_a).reverse_each { |filter| ActionTracer.logger.info filter }
89
+ invoked_after.map(&:to_a).each { |filter| ActionTracer.logger.info filter }
77
90
  end
78
91
 
79
92
  private
80
93
 
81
94
  def invoked_before
82
- @before + @around
95
+ @filters.select(&:before?)
83
96
  end
84
97
 
85
98
  def invoked_after
86
- @after + @around
99
+ @filters.select(&:after?).reverse
87
100
  end
88
101
  end
89
102
  end
@@ -3,22 +3,6 @@
3
3
  require "forwardable"
4
4
 
5
5
  module ActionTracer
6
- class Config
7
- attr_reader :path
8
-
9
- def initialize(directory:, file:)
10
- @path = directory + file
11
- end
12
- end
13
-
14
- def self.app_path
15
- @app_path ||= Dir.pwd
16
- end
17
-
18
- def self.config(directory: "/log/", file: "action_tracer.log")
19
- @config ||= Config.new(directory: app_path + directory, file: file)
20
- end
21
-
22
6
  class Railtie < ::Rails::Railtie
23
7
  initializer "action_tracer" do
24
8
  ActiveSupport.on_load(:action_controller) do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionTracer
4
- VERSION = "0.2.4"
4
+ VERSION = '0.2.5'
5
5
  end
data/lib/action_tracer.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "action_tracer/version"
4
+ require "action_tracer/configuration"
4
5
  require "action_tracer/railtie"
5
6
  require "action_tracer/filters"
6
- require "action_tracer/logger"
7
7
  require "action_tracer/action_tracer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - makicamel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-11 00:00:00.000000000 Z
11
+ date: 2023-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -89,8 +89,8 @@ files:
89
89
  - bin/setup
90
90
  - lib/action_tracer.rb
91
91
  - lib/action_tracer/action_tracer.rb
92
+ - lib/action_tracer/configuration.rb
92
93
  - lib/action_tracer/filters.rb
93
- - lib/action_tracer/logger.rb
94
94
  - lib/action_tracer/monkey_patches/abstract_controller/callbacks.rb
95
95
  - lib/action_tracer/monkey_patches/active_support/callbacks.rb
96
96
  - lib/action_tracer/railtie.rb
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "forwardable"
4
-
5
- module ActionTracer
6
- class Logger < ActiveSupport::LogSubscriber
7
- extend Forwardable
8
- def_delegators :@logger, :info
9
-
10
- def initialize
11
- @logger = ::Logger.new(ActionTracer.config.path)
12
- end
13
- end
14
-
15
- def self.logger
16
- @logger ||= Logger.new
17
- end
18
- end