whoop 1.0.2 → 1.0.3

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: c210634d7bf20b36da58aaaac74e882303f4336774eec34ecd7bf9759855827b
4
- data.tar.gz: d3bc51f178779de13a22e6e79b2e42cb3c80356657d7f5c11b4973e5dce3a809
3
+ metadata.gz: e87afc662b392a97fb44f20ec959efbd381c610d79632078feb9fca40f2591c9
4
+ data.tar.gz: 4710ad20b1dd68ee4215b964767ccb4ad82a863dfa720c6569735be37b03e89d
5
5
  SHA512:
6
- metadata.gz: c9d27fa6de48e6e61588d296dee684a6a87b22586541f8a10329c29b126fe2f0a59224c8174cc4a9514ab49155ff5f082457a656c4addb2be0db51f1f0dada19
7
- data.tar.gz: 714ad58469426ae297bb3733daff3bb62256db471b653ecbef069bc906caf32ff01e4fe7413fdb6a20536cdbdd0b5e50b1c649122ab231d33477aeaa0b9c96b5
6
+ metadata.gz: 636d9da4424c22bda6c608a69b2bf4e4685b04698a2c960dbafcd479cc4cfe4129512b59bda6055a90745b3be534be31e9f195481353ead1707b9bc468575e1c
7
+ data.tar.gz: af6eb8555456810f4d3c9a63a06b7f6337c6abd63ea004907618752465435c82f9c6857c7f95e7ff8a8d50646fc01e5c8709ed0091c1f8f8f793bf35dd75b668
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- whoop (1.0.2)
4
+ whoop (1.0.3)
5
5
  activerecord (>= 6.1.4)
6
6
  activesupport (>= 6.1.4)
7
7
  anbt-sql-formatter
@@ -25,12 +25,15 @@ GEM
25
25
  ast (2.4.2)
26
26
  bump (0.10.0)
27
27
  byebug (11.1.3)
28
+ codecov (0.6.0)
29
+ simplecov (>= 0.15, < 0.22)
28
30
  coderay (1.1.3)
29
31
  colorize (0.8.1)
30
32
  commander (4.6.0)
31
33
  highline (~> 2.0.0)
32
34
  concurrent-ruby (1.1.10)
33
35
  diff-lcs (1.5.0)
36
+ docile (1.4.0)
34
37
  highline (2.0.3)
35
38
  i18n (1.12.0)
36
39
  concurrent-ruby (~> 1.0)
@@ -86,6 +89,11 @@ GEM
86
89
  rubocop (>= 1.7.0, < 2.0)
87
90
  rubocop-ast (>= 0.4.0)
88
91
  ruby-progressbar (1.11.0)
92
+ simplecov (0.16.1)
93
+ docile (~> 1.1)
94
+ json (>= 1.8, < 3)
95
+ simplecov-html (~> 0.10.0)
96
+ simplecov-html (0.10.2)
89
97
  sorbet-runtime (0.5.10455)
90
98
  sord (4.0.0)
91
99
  commander (~> 4.5)
@@ -104,13 +112,16 @@ GEM
104
112
 
105
113
  PLATFORMS
106
114
  arm64-darwin-21
115
+ x86_64-linux
107
116
 
108
117
  DEPENDENCIES
109
118
  bump
119
+ codecov
110
120
  magic_frozen_string_literal
111
121
  pry-byebug
112
122
  rake
113
123
  rspec
124
+ simplecov
114
125
  sord
115
126
  standard
116
127
  whoop!
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Whoop
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/whoop.svg)](https://badge.fury.io/rb/whoop)
4
+ [![Contribute](https://img.shields.io/badge/Contribute%20with-Gitpod-908a85?logo=gitpod)](https://gitpod.io/#https://github.com/coderberry/whoop)
4
5
  [![Maintainability](https://api.codeclimate.com/v1/badges/1ffd27fe59383a4ff52b/maintainability)](https://codeclimate.com/github/coderberry/whoop/maintainability)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/1ffd27fe59383a4ff52b/test_coverage)](https://codeclimate.com/github/coderberry/whoop/test_coverage)
6
+ [![codecov](https://codecov.io/gh/coderberry/whoop/branch/main/graph/badge.svg?token=E906B6SEKD)](https://codecov.io/gh/coderberry/whoop)
6
7
  [![Tests](https://github.com/coderberry/whoop/actions/workflows/tests.yml/badge.svg)](https://github.com/coderberry/whoop/actions/workflows/tests.yml)
7
8
  [![CodeQL](https://github.com/coderberry/whoop/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/coderberry/whoop/actions/workflows/codeql-analysis.yml)
8
9
  [![StandardRB](https://github.com/coderberry/whoop/actions/workflows/standardrb.yml/badge.svg)](https://github.com/coderberry/whoop/actions/workflows/standardrb.yml)
@@ -13,6 +14,8 @@
13
14
 
14
15
  Whoop is a Ruby logging library with built-in formatting and colorization.
15
16
 
17
+ To try **whoop** within your browser, visit the [whoop playground](https://replit.com/@coderberry/whoop-playground).
18
+
16
19
  ## Installation
17
20
 
18
21
  Install the gem and add to the application's Gemfile by executing:
@@ -34,7 +37,7 @@ Whoop.setup do |config|
34
37
  # config.logger = ActiveSupport::Logger.new("log/#{Rails.env}.log")
35
38
  # config.logger = ActiveSupport::Logger.new($stdout)
36
39
  # config.logger = nil # uses `puts`
37
-
40
+
38
41
  config.level = :debug
39
42
  # config.level = :info
40
43
  # config.level = :warn
@@ -50,11 +53,189 @@ The `whoop` method is accessible from any object. See specs for more examples.
50
53
  whoop "Hello, World!"
51
54
  ```
52
55
 
53
- ### Formatters
54
- TODO
56
+ You can pass any options into the `whoop` method to change the output.
57
+
58
+ - `label` (first argument) - Either the object or title if a block is passed (see below)
59
+ - `pattern` - String character to use for the line (default is `-`)
60
+ - `count` - the number of times to repeat the pattern per line (e.g. 80)
61
+ - `color` - the color to use for the line (e.g. :red)
62
+ - `format` - the format to use for the message (one of `:json`, `:sql`, `:plain`)
63
+ - `caller_depth` - the depth of the caller to use for the source (default: 0)
64
+ - `explain` - whether to run `EXPLAIN` on the SQL query (default: false)
65
+ - `context` - a hash of key/value pairs to include in the output
66
+
67
+ ## Examples
68
+
69
+ ```ruby
70
+ whoop "Hello"
71
+
72
+ # log/debug.log
73
+ # ┏--------------------------------------------------------------------------------
74
+ # ┆ timestamp: 2022-09-26 14:28:06 -0600
75
+ # ┆ source: /spec/whoop_spec.rb:12
76
+ #
77
+ # Hello
78
+ #
79
+ # ┗--------------------------------------------------------------------------------
80
+ ```
81
+
82
+ ```ruby
83
+ whoop("My Label", color: :green) { "Hello" }
84
+
85
+ # log/debug.log (the colors don't appear in markdown)
86
+ # ┏------------------------------------ My Label ------------------------------------
87
+ # ┆ timestamp: 2022-09-26 14:28:06 -0600
88
+ # ┆ source: /spec/whoop_spec.rb:26
89
+ #
90
+ # Hello
91
+ #
92
+ # ┗--------------------------------------------------------------------------------
93
+ ```
94
+
95
+ ```ruby
96
+ whoop({hello: "world"}, format: :json, color: false)
97
+
98
+ # ┏--------------------------------------------------------------------------------
99
+ # ┆ timestamp: 2022-09-26 14:28:06 -0600
100
+ # ┆ source: /spec/whoop_spec.rb:39
101
+ #
102
+ # {
103
+ # "hello": "world"
104
+ # }
105
+ #
106
+ # ┗--------------------------------------------------------------------------------
107
+ ```
108
+
109
+ ```ruby
110
+ whoop("This message includes context", color: false, context: {user: "Eric", ip_address: "127.0.0.1"})
111
+
112
+ # ┏--------------------------------------------------------------------------------
113
+ # ┆ timestamp: 2022-09-26 14:28:06 -0600
114
+ # ┆ source: /spec/whoop_spec.rb:39
115
+ # ┆ user: Eric
116
+ # ┆ ip_address: 127.0.0.1
117
+ #
118
+ # This message includes context
119
+ #
120
+ # ┗--------------------------------------------------------------------------------
121
+ ```
122
+
123
+ ```ruby
124
+ sql = 'SELECT emp_id, first_name,last_name,dept_id,mgr_id, ' +
125
+ 'WIDTH_BUCKET(department_id,20,40,10) "Exists in Dept" ' +
126
+ 'FROM emp WHERE mgr_id < 300 ORDER BY "Exists in Dept"'
127
+
128
+ whoop(sql, format: :sql)
129
+
130
+ # ┏--------------------------------------------------------------------------------
131
+ # ┆ timestamp: 2022-09-26 14:28:06 -0600
132
+ # ┆ source: /spec/whoop_spec.rb:52
133
+ #
134
+ # SELECT
135
+ # emp_id
136
+ # ,first_name
137
+ # ,last_name
138
+ # ,dept_id
139
+ # ,mgr_id
140
+ # ,WIDTH_BUCKET (
141
+ # department_id
142
+ # ,20
143
+ # ,40
144
+ # ,10
145
+ # ) "Exists in Dept"
146
+ # FROM
147
+ # emp
148
+ # WHERE
149
+ # mgr_id < 300
150
+ # ORDER BY
151
+ # "Exists in Dept"
152
+ #
153
+ # ┗--------------------------------------------------------------------------------
154
+ ```
155
+
156
+ #### Auto-explain SQL queries
157
+
158
+ In addition to formatting the SQL query, you can also ask whoop to perform an
159
+ `explain` on the query by using the `explain: true` argument.
160
+
161
+ Example (using the Example 1 sample plan from explain.dalibo.com):
162
+
163
+ ```ruby
164
+ sql = <<~SQL
165
+ SELECT rel_users_exams.user_username AS rel_users_exams_user_username,
166
+ rel_users_exams.exam_id AS rel_users_exams_exam_id,
167
+ rel_users_exams.started_at AS rel_users_exams_started_at,
168
+ rel_users_exams.finished_at AS rel_users_exams_finished_at,
169
+ exam_1.id AS exam_1_id,
170
+ exam_1.title AS exam_1_title,
171
+ exam_1.date_from AS exam_1_date_from,
172
+ exam_1.date_to AS exam_1_date_to,
173
+ exam_1.created AS exam_1_created,
174
+ exam_1.created_by_ AS exam_1_created_by_,
175
+ exam_1.duration AS exam_1_duration,
176
+ exam_1.success_threshold AS exam_1_success_threshold,
177
+ exam_1.published AS exam_1_published
178
+ FROM rel_users_exams LEFT OUTER
179
+ JOIN exam AS exam_1
180
+ ON exam_1.id = rel_users_exams.exam_id
181
+ WHERE 1 = rel_users_exams.exam_id;
182
+ SQL
183
+
184
+ whoop("SQL with Explain", format: :sql, explain: true) { sql }
185
+
186
+ # ┏-------------------------------- SQL with Explain --------------------------------
187
+ # ┆ timestamp: 2022-09-26 14:50:11 -0600
188
+ # ┆ source: (irb):23:in `<top (required)>'
189
+ #
190
+ # sql:
191
+ #
192
+ # SELECT
193
+ # rel_users_exams.user_username AS rel_users_exams_user_username
194
+ # ,rel_users_exams.exam_id AS rel_users_exams_exam_id
195
+ # ,rel_users_exams.started_at AS rel_users_exams_started_at
196
+ # ,rel_users_exams.finished_at AS rel_users_exams_finished_at
197
+ # ,exam_1.id AS exam_1_id
198
+ # ,exam_1.title AS exam_1_title
199
+ # ,exam_1.date_from AS exam_1_date_from
200
+ # ,exam_1.date_to AS exam_1_date_to
201
+ # ,exam_1.created AS exam_1_created
202
+ # ,exam_1.created_by_ AS exam_1_created_by_
203
+ # ,exam_1.duration AS exam_1_duration
204
+ # ,exam_1.success_threshold AS exam_1_success_threshold
205
+ # ,exam_1.published AS exam_1_published
206
+ # FROM
207
+ # rel_users_exams LEFT OUTER JOIN exam AS exam_1
208
+ # ON exam_1.id = rel_users_exams.exam_id
209
+ # WHERE
210
+ # 1 = rel_users_exams.exam_id
211
+ # ;
212
+ #
213
+ # query plan:
214
+ #
215
+ # Nested Loop Left Join (cost=11.95..28.52 rows=5 width=157) (actual time=0.010..0.010 rows=0 loops=1)
216
+ # Output: rel_users_exams.user_username, rel_users_exams.exam_id, rel_users_exams.started_at, rel_users_exams.finished_at, exam_1.id, exam_1.title, exam_1.date_from, exam_1.date_to, exam_1.created, exam_1.created_by_, exam_1.duration, exam_1.success_threshold, exam_1.published
217
+ # Inner Unique: true
218
+ # Join Filter: (exam_1.id = rel_users_exams.exam_id)
219
+ # Buffers: shared hit=1
220
+ # -> Bitmap Heap Scan on public.rel_users_exams (cost=11.80..20.27 rows=5 width=52) (actual time=0.009..0.009 rows=0 loops=1)
221
+ # Output: rel_users_exams.user_username, rel_users_exams.exam_id, rel_users_exams.started_at, rel_users_exams.finished_at
222
+ # Recheck Cond: (1 = rel_users_exams.exam_id)
223
+ # Buffers: shared hit=1
224
+ # -> Bitmap Index Scan on rel_users_exams_pkey (cost=0.00..11.80 rows=5 width=0) (actual time=0.005..0.005 rows=0 loops=1)
225
+ # Index Cond: (1 = rel_users_exams.exam_id)
226
+ # Buffers: shared hit=1
227
+ # -> Materialize (cost=0.15..8.17 rows=1 width=105) (never executed)
228
+ # Output: exam_1.id, exam_1.title, exam_1.date_from, exam_1.date_to, exam_1.created, exam_1.created_by_, exam_1.duration, exam_1.success_threshold, exam_1.published
229
+ # -> Index Scan using exam_pkey on public.exam exam_1 (cost=0.15..8.17 rows=1 width=105) (never executed)
230
+ # Output: exam_1.id, exam_1.title, exam_1.date_from, exam_1.date_to, exam_1.created, exam_1.created_by_, exam_1.duration,
231
+ # exam_1.success_threshold, exam_1.published
232
+ # Index Cond: (exam_1.id = 1)
233
+ # Planning Time: 1.110 ms
234
+ # Execution Time: 0.170 ms
235
+ #
236
+ # ┗--------------------------------------------------------------------------------
237
+ ```
55
238
 
56
- ### Colors
57
- TODO
58
239
 
59
240
  ## Development
60
241
 
data/Rakefile CHANGED
@@ -22,6 +22,7 @@ end
22
22
 
23
23
  task publish: :build do
24
24
  system "gem push #{GEM_NAME}-#{GEM_VERSION}.gem"
25
+ system "gem push --key github --host https://rubygems.pkg.github.com/coderberry #{GEM_NAME}-#{GEM_VERSION}.gem"
25
26
  end
26
27
 
27
28
  task :clean do
data/lib/whoop/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Whoop
4
- VERSION = "1.0.2"
4
+ VERSION = "1.0.3"
5
5
  end
data/lib/whoop.rb CHANGED
@@ -30,6 +30,9 @@ module Whoop
30
30
  FORMATS = %i[plain json sql].freeze
31
31
  PATTERN = "-"
32
32
  COUNT = 80
33
+ INDENT = "┆"
34
+ TOP_LINE_CHAR = "┏"
35
+ BOTTOM_LINE_CHAR = "┗"
33
36
 
34
37
  # Log the message to the logger
35
38
  # @param [String] label (optional) - the label or object to log
@@ -39,15 +42,34 @@ module Whoop
39
42
  # @param [Symbol] format - the format to use for the message (one of :json, :sql, :plain)
40
43
  # @param [Integer] caller_depth - the depth of the caller to use for the source (default: 0)
41
44
  # @param [Boolean] explain - whether to explain the SQL query (default: false)
42
- def whoop(label = nil, pattern: PATTERN, count: COUNT, color: :default, format: :plain, caller_depth: 0, explain: false)
45
+ # @param [Hash] context - Any additional context you'd like to include in the log
46
+ def whoop(
47
+ label = nil,
48
+ pattern: PATTERN,
49
+ count: COUNT,
50
+ color: :default,
51
+ format: :plain,
52
+ caller_depth: 0,
53
+ explain: false,
54
+ context: nil
55
+ )
43
56
  logger_method = detect_logger_method
44
57
  color_method = detect_color_method(color)
45
58
  formatter_method = detect_formatter_method(format, colorize: color.present?, explain: explain)
46
59
 
47
60
  line = pattern * count
48
61
  caller_path = clean_caller_path(caller[caller_depth])
49
- caller_path_line = ["source:".colorize(:light_black).underline, caller_path].join(" ")
50
- timestamp_line = ["timestamp:".colorize(:light_black).underline, Time.now].join(" ")
62
+ caller_path_line = [color_method.call(INDENT), "source:".colorize(:light_black).underline, caller_path].join(" ")
63
+ timestamp_line = [color_method.call(INDENT), "timestamp:".colorize(:light_black).underline, Time.now].join(" ")
64
+
65
+ context_lines =
66
+ if context.is_a?(Hash) && context.keys.length > 0
67
+ context.map do |k, v|
68
+ [color_method.call(INDENT), "#{k}:".colorize(:light_black).underline, v].join(" ")
69
+ end
70
+ else
71
+ []
72
+ end
51
73
 
52
74
  if block_given?
53
75
  result = yield
@@ -58,21 +80,25 @@ module Whoop
58
80
  pattern * count
59
81
  end
60
82
  result.tap do
61
- logger_method.call color_method.call "\n\n#{top_line}"
83
+ logger_method.call color_method.call "\n\n#{TOP_LINE_CHAR}#{top_line}"
62
84
  logger_method.call timestamp_line
63
85
  logger_method.call caller_path_line
86
+ context_lines.each { |l| logger_method.call l }
64
87
  logger_method.call ""
65
88
  logger_method.call formatter_method.call(result)
66
- logger_method.call color_method.call "#{line}\n\n"
89
+ logger_method.call ""
90
+ logger_method.call color_method.call "#{BOTTOM_LINE_CHAR}#{line}\n\n"
67
91
  end
68
92
  else
69
93
  tap do
70
- logger_method.call color_method.call "\n\n#{line}"
94
+ logger_method.call color_method.call "\n\n#{TOP_LINE_CHAR}#{line}"
71
95
  logger_method.call timestamp_line
72
96
  logger_method.call caller_path_line
97
+ context_lines.each { |l| logger_method.call l }
73
98
  logger_method.call ""
74
99
  logger_method.call formatter_method.call(label)
75
- logger_method.call color_method.call "#{line}\n\n"
100
+ logger_method.call ""
101
+ logger_method.call color_method.call "#{BOTTOM_LINE_CHAR}#{line}\n\n"
76
102
  end
77
103
  end
78
104
  end
data/sig/whoop.rbs CHANGED
@@ -14,6 +14,9 @@ module Whoop
14
14
  FORMATS: untyped
15
15
  PATTERN: untyped
16
16
  COUNT: untyped
17
+ INDENT: untyped
18
+ TOP_LINE_CHAR: untyped
19
+ BOTTOM_LINE_CHAR: untyped
17
20
 
18
21
  # sord omit - no YARD return type given, using untyped
19
22
  # Log the message to the logger
@@ -31,6 +34,8 @@ module Whoop
31
34
  # _@param_ `caller_depth` — - the depth of the caller to use for the source (default: 0)
32
35
  #
33
36
  # _@param_ `explain` — - whether to explain the SQL query (default: false)
37
+ #
38
+ # _@param_ `context` — - Any additional context you'd like to include in the log
34
39
  def whoop: (
35
40
  ?String? label,
36
41
  ?pattern: String,
@@ -38,7 +43,8 @@ module Whoop
38
43
  ?color: Symbol,
39
44
  ?format: Symbol,
40
45
  ?caller_depth: Integer,
41
- ?explain: bool
46
+ ?explain: bool,
47
+ ?context: ::Hash[untyped, untyped]?
42
48
  ) -> untyped
43
49
 
44
50
  # Remove the Rails.root from the caller path
data/whoop.gemspec CHANGED
@@ -14,8 +14,6 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 2.6.0"
16
16
 
17
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
-
19
17
  spec.metadata["homepage_uri"] = spec.homepage
20
18
  spec.metadata["source_code_uri"] = spec.homepage
21
19
  spec.metadata["changelog_uri"] = spec.homepage
@@ -44,4 +42,6 @@ Gem::Specification.new do |spec|
44
42
  spec.add_development_dependency "standard"
45
43
  spec.add_development_dependency "bump"
46
44
  spec.add_development_dependency "sord"
45
+ spec.add_development_dependency "simplecov"
46
+ spec.add_development_dependency "codecov"
47
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whoop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Berry
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-23 00:00:00.000000000 Z
11
+ date: 2022-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -178,6 +178,34 @@ dependencies:
178
178
  - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: codecov
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
181
209
  description: A simple gem to help you whoop your logs into shape.
182
210
  email:
183
211
  - eric@berry.sh
@@ -194,7 +222,6 @@ files:
194
222
  - LICENSE.txt
195
223
  - README.md
196
224
  - Rakefile
197
- - cc-test-reporter
198
225
  - lib/whoop.rb
199
226
  - lib/whoop/formatters/json_formatter.rb
200
227
  - lib/whoop/formatters/sql_formatter.rb
@@ -205,7 +232,6 @@ homepage: https://github.com/coderberry/whoop
205
232
  licenses:
206
233
  - MIT
207
234
  metadata:
208
- allowed_push_host: https://rubygems.org
209
235
  homepage_uri: https://github.com/coderberry/whoop
210
236
  source_code_uri: https://github.com/coderberry/whoop
211
237
  changelog_uri: https://github.com/coderberry/whoop
data/cc-test-reporter DELETED
Binary file