markdown_logging_proxy 1.0.1 → 1.2.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: 38a597efa8e2dab27f1fe61005c367d1cfbabd2e1ea5a03e03ec1b29d2baab02
4
+ data.tar.gz: f76c90d59eb9c1a911c6eeb2c3e1e2bb8ec881847d9bf8d9cadb0a53ae3378fc
5
5
  SHA512:
6
- metadata.gz: 689b425d69ab942947a53913d1ea6b2405d334d76e31bdb25050ed89fe3eb1bc5250f95801a6da1dd7b290b700c6ae62914f3d01d17d7f21ea782135920b6f8e
7
- data.tar.gz: 3c67856e0afc5c95b08a1606e0d6d1338e96ee41542e6608deca25c77fe1ea1e758f8ce479c075de5f1de769277716285c56cb0f527c4d1fc2139c2fc8115611
6
+ metadata.gz: 108a3acc43c38e7a9fad4773a5d2f2b7b9c55e33b1f6280869316affe6d8b48d342ada65cb170865f672a6f1a77f184a171ccdc6720ebe9c41d5bae8af5ed590
7
+ data.tar.gz: 9a696b09265a340d4934c4874cbe0e0977cd6739768d282f145255b128126cdac879e921de452addffa1b94b980dce0d7039d397babd978a179e70b70c09d5aa
data/CHANGELOG.md CHANGED
@@ -14,3 +14,22 @@
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
27
+
28
+ ## [1.1.1]
29
+
30
+ - Allow proxying of `nil`
31
+ - Add require statements to copy+paste version
32
+
33
+ ## [1.2.0]
34
+
35
+ - Overwrite as many `Object` methods as we can by default
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.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -18,7 +18,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
18
 
19
19
  ### Copy+Paste
20
20
 
21
- Look at `dist/` for compiled versions of the gem which can safely (relatively) be copied into irb/pry sessions.
21
+ You can copy the contents of [`dist/markdown_logging_proxy.rb`](dist/markdown_logging_proxy.rb) into a live irb/pry session.
22
22
 
23
23
  ## Usage
24
24
 
@@ -1,12 +1,21 @@
1
+ require 'logger'
2
+ require 'securerandom'
3
+ require 'pp'
4
+ require 'time'
1
5
  module MarkdownLoggingProxy
2
6
  class MarkdownLogger
3
- def self.inspect_object(object)
4
- ['```ruby', object.pretty_inspect.chomp, '```'].join("\n")
7
+ def self.inspect_object(object, show_id = true)
8
+ [
9
+ '```ruby',
10
+ ("# #{id_object(object)}" if show_id),
11
+ object.pretty_inspect.chomp,
12
+ '```'
13
+ ].compact.join("\n")
5
14
  end
6
15
 
7
16
  def self.id_object(object)
8
17
  # #<Object:0x00007f5a0919e140>
9
- "##{object.class}:0x#{object.object_id.to_s(16)}>"
18
+ "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
10
19
  end
11
20
 
12
21
  def self.build(location, **options)
@@ -14,9 +23,10 @@ module MarkdownLoggingProxy
14
23
  new(location, **options)
15
24
  end
16
25
 
17
- attr_reader :std_logger, :backtrace, :heading_level
26
+ attr_reader :std_logger, :backtrace, :heading_level, :created_at
18
27
 
19
28
  def initialize(location, backtrace: true)
29
+ @created_at = Time.now
20
30
  @std_logger = create_logger(location)
21
31
  @heading_level = 1
22
32
  @backtrace = backtrace
@@ -62,7 +72,8 @@ module MarkdownLoggingProxy
62
72
 
63
73
  def markdown_formatter
64
74
  proc do |severity, time, __exec, msg|
65
- "#{'#' * heading_level} #{severity} in #{Process.pid} at #{time.iso8601} -- #{msg}\n\n"
75
+ elapsed = Time.now - created_at
76
+ "#{'#' * heading_level} #{severity} at +#{elapsed.round(5)} -- #{msg}\n\n"
66
77
  end
67
78
  end
68
79
  end
@@ -74,14 +85,9 @@ module MarkdownLoggingProxy
74
85
  end
75
86
  end
76
87
  module MarkdownLoggingProxy
77
- # frozen_string_literal: true
78
-
79
- require 'logger'
80
- require 'securerandom'
81
- require 'pp'
82
- require 'time'
83
-
84
88
  class Proxy
89
+ DO_NOT_OVERWRITE = %i[__binding__ __id__ __send__ class extend]
90
+ DEFAULT_OVERWRITES = Object.new.methods - DO_NOT_OVERWRITE
85
91
 
86
92
  def initialize(
87
93
  to_proxy = nil,
@@ -90,10 +96,9 @@ module MarkdownLoggingProxy
90
96
  backtrace: true, # regex/true/false backtrace control
91
97
  ignore: [], # methods we shouldn't log/proxy
92
98
  proxy_response: [], # methods we should return a proxy for
93
- overwrite: [] # methods defined on Object we should overwrite
99
+ overwrite: DEFAULT_OVERWRITES
94
100
  )
95
101
  @target = to_proxy || target
96
- raise ArgumentError, "Missing required proxy target" unless @target
97
102
  @logger = MarkdownLogger.build(location, backtrace: backtrace)
98
103
  @tracer = Tracer.new(
99
104
  target: @target,
@@ -107,8 +112,8 @@ module MarkdownLoggingProxy
107
112
  }
108
113
  )
109
114
  overwrite.each do |meth|
110
- define_method(meth) do |*args, &blk|
111
- @traceer.trace(meth, args, &blk)
115
+ self.class.define_method(meth) do |*args, &blk|
116
+ @tracer.trace(meth, args, &blk)
112
117
  end
113
118
  end
114
119
  end
@@ -121,7 +126,7 @@ end
121
126
  module MarkdownLoggingProxy
122
127
  class Tracer
123
128
  attr_reader :target, :logger, :ignore, :proxy
124
-
129
+
125
130
  def initialize(
126
131
  target:,
127
132
  proxy:,
@@ -132,7 +137,7 @@ module MarkdownLoggingProxy
132
137
  )
133
138
  @target = target
134
139
  @logger = logger
135
- @ignore = (ignore + proxy_response)
140
+ @ignore = ignore
136
141
  @proxy_response = proxy_response
137
142
  @proxy_options = proxy_options
138
143
  end
@@ -165,9 +170,9 @@ module MarkdownLoggingProxy
165
170
 
166
171
  Arguments:
167
172
 
168
- #{MarkdownLogger.inspect_object(args)}
173
+ #{MarkdownLogger.inspect_object(args, false)}
169
174
 
170
- Block? #{block_given? ? 'Yes' : 'No'}
175
+ Block given? #{block_given? ? 'Yes' : 'No'}
171
176
  #{logger.inspect_backtrace}
172
177
  MSG
173
178
  end
@@ -176,12 +181,14 @@ module MarkdownLoggingProxy
176
181
  response = target.public_send(meth, *args, &log_and_proxy_block(meth, blk))
177
182
  log_response(meth, response) unless ignore?(meth)
178
183
  return response unless proxy_response?(meth)
179
- logger.log :info, 3, <<~MSG
184
+ logger.log :info, 3, <<~MSG.chomp
180
185
  Returning proxied response to `#{meth}`
181
-
186
+
182
187
  Proxy from `#{meth}` on #{MarkdownLogger.id_object(target)}
183
-
184
- Proxy for: #{MarkdownLogger.id_object(response)}
188
+
189
+ Proxy for:
190
+
191
+ #{MarkdownLogger.inspect_object(response)}
185
192
  MSG
186
193
  Proxy.new(**@proxy_options.merge(
187
194
  target: response,
@@ -193,16 +200,18 @@ module MarkdownLoggingProxy
193
200
 
194
201
  def log_and_proxy_block(meth, blk)
195
202
  return if blk.nil?
203
+ logger_ref = self.logger
204
+ target_ref = self.target
196
205
  proc do |*args|
197
- logger.log :info, 3, <<~MSG
198
- Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
206
+ logger_ref.log :info, 2, <<~MSG.chomp
207
+ Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target_ref)}
199
208
 
200
209
  Arguments:
201
210
 
202
- #{MarkdownLogger.inspect_object(args)}
211
+ #{MarkdownLogger.inspect_object(args, false)}
203
212
  MSG
204
- blk.call(*args).tap do |response|
205
- logger.log :info, 4, <<~MSG
213
+ instance_exec(*args, &blk).tap do |response|
214
+ logger_ref.log :info, 3, <<~MSG.chomp
206
215
  Response from block in `#{meth}`
207
216
 
208
217
  #{MarkdownLogger.inspect_object(response)}
@@ -212,7 +221,7 @@ module MarkdownLoggingProxy
212
221
  end
213
222
 
214
223
  def log_and_reraise_error(meth, error)
215
- logger.log :error, 2, <<~MSG
224
+ logger.log :error, 2, <<~MSG.chomp
216
225
  Error in `#{meth}`
217
226
 
218
227
  Type: #{error.class}
@@ -232,3 +241,4 @@ module MarkdownLoggingProxy
232
241
  end
233
242
  end
234
243
  end
244
+
@@ -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
@@ -1,12 +1,7 @@
1
1
  module MarkdownLoggingProxy
2
- # frozen_string_literal: true
3
-
4
- require 'logger'
5
- require 'securerandom'
6
- require 'pp'
7
- require 'time'
8
-
9
2
  class Proxy
3
+ DO_NOT_OVERWRITE = %i[__binding__ __id__ __send__ class extend]
4
+ DEFAULT_OVERWRITES = Object.new.methods - DO_NOT_OVERWRITE
10
5
 
11
6
  def initialize(
12
7
  to_proxy = nil,
@@ -15,10 +10,9 @@ module MarkdownLoggingProxy
15
10
  backtrace: true, # regex/true/false backtrace control
16
11
  ignore: [], # methods we shouldn't log/proxy
17
12
  proxy_response: [], # methods we should return a proxy for
18
- overwrite: [] # methods defined on Object we should overwrite
13
+ overwrite: DEFAULT_OVERWRITES
19
14
  )
20
15
  @target = to_proxy || target
21
- raise ArgumentError, "Missing required proxy target" unless @target
22
16
  @logger = MarkdownLogger.build(location, backtrace: backtrace)
23
17
  @tracer = Tracer.new(
24
18
  target: @target,
@@ -32,8 +26,8 @@ module MarkdownLoggingProxy
32
26
  }
33
27
  )
34
28
  overwrite.each do |meth|
35
- define_method(meth) do |*args, &blk|
36
- @traceer.trace(meth, args, &blk)
29
+ self.class.define_method(meth) do |*args, &blk|
30
+ @tracer.trace(meth, args, &blk)
37
31
  end
38
32
  end
39
33
  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,
@@ -73,16 +75,18 @@ module MarkdownLoggingProxy
73
75
 
74
76
  def log_and_proxy_block(meth, blk)
75
77
  return if blk.nil?
78
+ logger_ref = self.logger
79
+ target_ref = self.target
76
80
  proc do |*args|
77
- logger.log :info, 3, <<~MSG
78
- Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
81
+ logger_ref.log :info, 2, <<~MSG.chomp
82
+ Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target_ref)}
79
83
 
80
84
  Arguments:
81
85
 
82
- #{MarkdownLogger.inspect_object(args)}
86
+ #{MarkdownLogger.inspect_object(args, false)}
83
87
  MSG
84
- blk.call(*args).tap do |response|
85
- logger.log :info, 4, <<~MSG
88
+ instance_exec(*args, &blk).tap do |response|
89
+ logger_ref.log :info, 3, <<~MSG.chomp
86
90
  Response from block in `#{meth}`
87
91
 
88
92
  #{MarkdownLogger.inspect_object(response)}
@@ -92,7 +96,7 @@ module MarkdownLoggingProxy
92
96
  end
93
97
 
94
98
  def log_and_reraise_error(meth, error)
95
- logger.log :error, 2, <<~MSG
99
+ logger.log :error, 2, <<~MSG.chomp
96
100
  Error in `#{meth}`
97
101
 
98
102
  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.2.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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  requirements: []
57
- rubygems_version: 3.3.7
57
+ rubygems_version: 3.3.20
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Proxy object for debugging