markdown_logging_proxy 1.0.1 → 1.1.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: 338dd4aa3a43c7cda534f2b694a44b1347e1ad7434f417aa479dc8bb4daf7f95
4
- data.tar.gz: 0bcd6c0fd97df22c18495e5f68a9181a45cd4dc2997c3d2754ba99e4b9678104
3
+ metadata.gz: 7b7fa845a8bab9f3a70e7362c0910ff5b24c6bc95d5267e82e8d4f87f90fa2e3
4
+ data.tar.gz: 0c26ee702af858bc046077ebf4209f473c50e66ee14e03f45da161f1990b510a
5
5
  SHA512:
6
- metadata.gz: 689b425d69ab942947a53913d1ea6b2405d334d76e31bdb25050ed89fe3eb1bc5250f95801a6da1dd7b290b700c6ae62914f3d01d17d7f21ea782135920b6f8e
7
- data.tar.gz: 3c67856e0afc5c95b08a1606e0d6d1338e96ee41542e6608deca25c77fe1ea1e758f8ce479c075de5f1de769277716285c56cb0f527c4d1fc2139c2fc8115611
6
+ metadata.gz: 6ac27cf32075e6046405f21bec1d4bae709c9a3e91b40b461a53b8a5794ff25165efe4295183a9a0f58a1f8686a15d08b0424e79f87355887967338c85e00526
7
+ data.tar.gz: 927d9792258911a98117c6915288b13bb05a6596257a38504adf24d1439aa972e71868c8895ab1cd0d5afbb8e4910e463948bb0bd11cbf0e29544fc66b04685d
data/CHANGELOG.md CHANGED
@@ -14,3 +14,13 @@
14
14
 
15
15
  - Add missing CHANGELOG updates
16
16
  - Commit updates to compiled `dist/` version of library
17
+
18
+ ## [1.0.2]
19
+
20
+ - Improved log formatting
21
+ - Fix to proxy method overwrite behavior
22
+
23
+ ## [1.1.0]
24
+
25
+ - Don't show object IDs for args arrays. It's confusing.
26
+ - BREAKING: Proxies most `Object` methods by default now
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- markdown_logging_proxy (1.0.1)
4
+ markdown_logging_proxy (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,12 +1,17 @@
1
1
  module MarkdownLoggingProxy
2
2
  class MarkdownLogger
3
- def self.inspect_object(object)
4
- ['```ruby', object.pretty_inspect.chomp, '```'].join("\n")
3
+ def self.inspect_object(object, show_id = true)
4
+ [
5
+ '```ruby',
6
+ ("# #{id_object(object)}" if show_id),
7
+ object.pretty_inspect.chomp,
8
+ '```'
9
+ ].compact.join("\n")
5
10
  end
6
11
 
7
12
  def self.id_object(object)
8
13
  # #<Object:0x00007f5a0919e140>
9
- "##{object.class}:0x#{object.object_id.to_s(16)}>"
14
+ "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
10
15
  end
11
16
 
12
17
  def self.build(location, **options)
@@ -14,9 +19,10 @@ module MarkdownLoggingProxy
14
19
  new(location, **options)
15
20
  end
16
21
 
17
- attr_reader :std_logger, :backtrace, :heading_level
22
+ attr_reader :std_logger, :backtrace, :heading_level, :created_at
18
23
 
19
24
  def initialize(location, backtrace: true)
25
+ @created_at = Time.now
20
26
  @std_logger = create_logger(location)
21
27
  @heading_level = 1
22
28
  @backtrace = backtrace
@@ -62,7 +68,8 @@ module MarkdownLoggingProxy
62
68
 
63
69
  def markdown_formatter
64
70
  proc do |severity, time, __exec, msg|
65
- "#{'#' * heading_level} #{severity} in #{Process.pid} at #{time.iso8601} -- #{msg}\n\n"
71
+ elapsed = Time.now - created_at
72
+ "#{'#' * heading_level} #{severity} at +#{elapsed.round(5)} -- #{msg}\n\n"
66
73
  end
67
74
  end
68
75
  end
@@ -82,6 +89,13 @@ module MarkdownLoggingProxy
82
89
  require 'time'
83
90
 
84
91
  class Proxy
92
+ # Object methods that should be proxied but won't hit method_missing
93
+ DEFAULT_OVERWRITES = %i[
94
+ ! != !~ <=> == === =~
95
+ clone display dup enum_for eql? equal? freeze frozen? hash inspect
96
+ is_a? itself kind_of? nil? taint tainted? tap then to_enum to_s
97
+ trust untaint unstrust untrusted? yield_self
98
+ ]
85
99
 
86
100
  def initialize(
87
101
  to_proxy = nil,
@@ -90,7 +104,7 @@ module MarkdownLoggingProxy
90
104
  backtrace: true, # regex/true/false backtrace control
91
105
  ignore: [], # methods we shouldn't log/proxy
92
106
  proxy_response: [], # methods we should return a proxy for
93
- overwrite: [] # methods defined on Object we should overwrite
107
+ overwrite: DEFAULT_OVERWRITES
94
108
  )
95
109
  @target = to_proxy || target
96
110
  raise ArgumentError, "Missing required proxy target" unless @target
@@ -107,8 +121,8 @@ module MarkdownLoggingProxy
107
121
  }
108
122
  )
109
123
  overwrite.each do |meth|
110
- define_method(meth) do |*args, &blk|
111
- @traceer.trace(meth, args, &blk)
124
+ self.class.define_method(meth) do |*args, &blk|
125
+ @tracer.trace(meth, args, &blk)
112
126
  end
113
127
  end
114
128
  end
@@ -121,7 +135,7 @@ end
121
135
  module MarkdownLoggingProxy
122
136
  class Tracer
123
137
  attr_reader :target, :logger, :ignore, :proxy
124
-
138
+
125
139
  def initialize(
126
140
  target:,
127
141
  proxy:,
@@ -132,7 +146,7 @@ module MarkdownLoggingProxy
132
146
  )
133
147
  @target = target
134
148
  @logger = logger
135
- @ignore = (ignore + proxy_response)
149
+ @ignore = ignore
136
150
  @proxy_response = proxy_response
137
151
  @proxy_options = proxy_options
138
152
  end
@@ -165,9 +179,9 @@ module MarkdownLoggingProxy
165
179
 
166
180
  Arguments:
167
181
 
168
- #{MarkdownLogger.inspect_object(args)}
182
+ #{MarkdownLogger.inspect_object(args, false)}
169
183
 
170
- Block? #{block_given? ? 'Yes' : 'No'}
184
+ Block given? #{block_given? ? 'Yes' : 'No'}
171
185
  #{logger.inspect_backtrace}
172
186
  MSG
173
187
  end
@@ -176,12 +190,14 @@ module MarkdownLoggingProxy
176
190
  response = target.public_send(meth, *args, &log_and_proxy_block(meth, blk))
177
191
  log_response(meth, response) unless ignore?(meth)
178
192
  return response unless proxy_response?(meth)
179
- logger.log :info, 3, <<~MSG
193
+ logger.log :info, 3, <<~MSG.chomp
180
194
  Returning proxied response to `#{meth}`
181
-
195
+
182
196
  Proxy from `#{meth}` on #{MarkdownLogger.id_object(target)}
183
-
184
- Proxy for: #{MarkdownLogger.id_object(response)}
197
+
198
+ Proxy for:
199
+
200
+ #{MarkdownLogger.inspect_object(response)}
185
201
  MSG
186
202
  Proxy.new(**@proxy_options.merge(
187
203
  target: response,
@@ -194,15 +210,15 @@ module MarkdownLoggingProxy
194
210
  def log_and_proxy_block(meth, blk)
195
211
  return if blk.nil?
196
212
  proc do |*args|
197
- logger.log :info, 3, <<~MSG
213
+ logger.log :info, 2, <<~MSG.chomp
198
214
  Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
199
215
 
200
216
  Arguments:
201
217
 
202
- #{MarkdownLogger.inspect_object(args)}
218
+ #{MarkdownLogger.inspect_object(args, false)}
203
219
  MSG
204
220
  blk.call(*args).tap do |response|
205
- logger.log :info, 4, <<~MSG
221
+ logger.log :info, 3, <<~MSG.chomp
206
222
  Response from block in `#{meth}`
207
223
 
208
224
  #{MarkdownLogger.inspect_object(response)}
@@ -212,7 +228,7 @@ module MarkdownLoggingProxy
212
228
  end
213
229
 
214
230
  def log_and_reraise_error(meth, error)
215
- logger.log :error, 2, <<~MSG
231
+ logger.log :error, 2, <<~MSG.chomp
216
232
  Error in `#{meth}`
217
233
 
218
234
  Type: #{error.class}
@@ -1,12 +1,17 @@
1
1
  module MarkdownLoggingProxy
2
2
  class MarkdownLogger
3
- def self.inspect_object(object)
4
- ['```ruby', object.pretty_inspect.chomp, '```'].join("\n")
3
+ def self.inspect_object(object, show_id = true)
4
+ [
5
+ '```ruby',
6
+ ("# #{id_object(object)}" if show_id),
7
+ object.pretty_inspect.chomp,
8
+ '```'
9
+ ].compact.join("\n")
5
10
  end
6
11
 
7
12
  def self.id_object(object)
8
13
  # #<Object:0x00007f5a0919e140>
9
- "##{object.class}:0x#{object.object_id.to_s(16)}>"
14
+ "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
10
15
  end
11
16
 
12
17
  def self.build(location, **options)
@@ -14,9 +19,10 @@ module MarkdownLoggingProxy
14
19
  new(location, **options)
15
20
  end
16
21
 
17
- attr_reader :std_logger, :backtrace, :heading_level
22
+ attr_reader :std_logger, :backtrace, :heading_level, :created_at
18
23
 
19
24
  def initialize(location, backtrace: true)
25
+ @created_at = Time.now
20
26
  @std_logger = create_logger(location)
21
27
  @heading_level = 1
22
28
  @backtrace = backtrace
@@ -62,7 +68,8 @@ module MarkdownLoggingProxy
62
68
 
63
69
  def markdown_formatter
64
70
  proc do |severity, time, __exec, msg|
65
- "#{'#' * heading_level} #{severity} in #{Process.pid} at #{time.iso8601} -- #{msg}\n\n"
71
+ elapsed = Time.now - created_at
72
+ "#{'#' * heading_level} #{severity} at +#{elapsed.round(5)} -- #{msg}\n\n"
66
73
  end
67
74
  end
68
75
  end
@@ -7,6 +7,13 @@ module MarkdownLoggingProxy
7
7
  require 'time'
8
8
 
9
9
  class Proxy
10
+ # Object methods that should be proxied but won't hit method_missing
11
+ DEFAULT_OVERWRITES = %i[
12
+ ! != !~ <=> == === =~
13
+ clone display dup enum_for eql? equal? freeze frozen? hash inspect
14
+ is_a? itself kind_of? nil? taint tainted? tap then to_enum to_s
15
+ trust untaint unstrust untrusted? yield_self
16
+ ]
10
17
 
11
18
  def initialize(
12
19
  to_proxy = nil,
@@ -15,7 +22,7 @@ module MarkdownLoggingProxy
15
22
  backtrace: true, # regex/true/false backtrace control
16
23
  ignore: [], # methods we shouldn't log/proxy
17
24
  proxy_response: [], # methods we should return a proxy for
18
- overwrite: [] # methods defined on Object we should overwrite
25
+ overwrite: DEFAULT_OVERWRITES
19
26
  )
20
27
  @target = to_proxy || target
21
28
  raise ArgumentError, "Missing required proxy target" unless @target
@@ -32,8 +39,8 @@ module MarkdownLoggingProxy
32
39
  }
33
40
  )
34
41
  overwrite.each do |meth|
35
- define_method(meth) do |*args, &blk|
36
- @traceer.trace(meth, args, &blk)
42
+ self.class.define_method(meth) do |*args, &blk|
43
+ @tracer.trace(meth, args, &blk)
37
44
  end
38
45
  end
39
46
  end
@@ -1,7 +1,7 @@
1
1
  module MarkdownLoggingProxy
2
2
  class Tracer
3
3
  attr_reader :target, :logger, :ignore, :proxy
4
-
4
+
5
5
  def initialize(
6
6
  target:,
7
7
  proxy:,
@@ -12,7 +12,7 @@ module MarkdownLoggingProxy
12
12
  )
13
13
  @target = target
14
14
  @logger = logger
15
- @ignore = (ignore + proxy_response)
15
+ @ignore = ignore
16
16
  @proxy_response = proxy_response
17
17
  @proxy_options = proxy_options
18
18
  end
@@ -45,9 +45,9 @@ module MarkdownLoggingProxy
45
45
 
46
46
  Arguments:
47
47
 
48
- #{MarkdownLogger.inspect_object(args)}
48
+ #{MarkdownLogger.inspect_object(args, false)}
49
49
 
50
- Block? #{block_given? ? 'Yes' : 'No'}
50
+ Block given? #{block_given? ? 'Yes' : 'No'}
51
51
  #{logger.inspect_backtrace}
52
52
  MSG
53
53
  end
@@ -56,12 +56,14 @@ module MarkdownLoggingProxy
56
56
  response = target.public_send(meth, *args, &log_and_proxy_block(meth, blk))
57
57
  log_response(meth, response) unless ignore?(meth)
58
58
  return response unless proxy_response?(meth)
59
- logger.log :info, 3, <<~MSG
59
+ logger.log :info, 3, <<~MSG.chomp
60
60
  Returning proxied response to `#{meth}`
61
-
61
+
62
62
  Proxy from `#{meth}` on #{MarkdownLogger.id_object(target)}
63
-
64
- Proxy for: #{MarkdownLogger.id_object(response)}
63
+
64
+ Proxy for:
65
+
66
+ #{MarkdownLogger.inspect_object(response)}
65
67
  MSG
66
68
  Proxy.new(**@proxy_options.merge(
67
69
  target: response,
@@ -74,15 +76,15 @@ module MarkdownLoggingProxy
74
76
  def log_and_proxy_block(meth, blk)
75
77
  return if blk.nil?
76
78
  proc do |*args|
77
- logger.log :info, 3, <<~MSG
79
+ logger.log :info, 2, <<~MSG.chomp
78
80
  Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
79
81
 
80
82
  Arguments:
81
83
 
82
- #{MarkdownLogger.inspect_object(args)}
84
+ #{MarkdownLogger.inspect_object(args, false)}
83
85
  MSG
84
86
  blk.call(*args).tap do |response|
85
- logger.log :info, 4, <<~MSG
87
+ logger.log :info, 3, <<~MSG.chomp
86
88
  Response from block in `#{meth}`
87
89
 
88
90
  #{MarkdownLogger.inspect_object(response)}
@@ -92,7 +94,7 @@ module MarkdownLoggingProxy
92
94
  end
93
95
 
94
96
  def log_and_reraise_error(meth, error)
95
- logger.log :error, 2, <<~MSG
97
+ logger.log :error, 2, <<~MSG.chomp
96
98
  Error in `#{meth}`
97
99
 
98
100
  Type: #{error.class}
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "markdown_logging_proxy"
3
- spec.version = "1.0.1"
3
+ spec.version = "1.1.0"
4
4
  spec.authors = ["Carl Zulauf"]
5
5
  spec.email = ["carl@linkleaf.com"]
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_logging_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf