tty-logger 0.4.0 → 0.5.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: 1e4b31874b5d1ca9d091b11c0a5d6ca04c3b44a41f1d92c1f9d9b40fe2573c5c
4
- data.tar.gz: 7f078cf57986ea70885db59ecab0bbd1da6afa93c272ea764208d3c88251b4f9
3
+ metadata.gz: '0594c7ef471a07571e5621e8535469fcb50edeeb10d7ffa58b194ef8fe4f8350'
4
+ data.tar.gz: 42a3b6c20b756754723c42c5b0ce85490b403978ffe566cb267dc6f18811f1c9
5
5
  SHA512:
6
- metadata.gz: 1c255546632ba09a0aeac9b5e0395dd668576eb30a882342c18b9cf6a5fa525b0c6143e17a6bae3406fdbcb0306c17916c1a5016c62f2e5df816b5b70afeb040
7
- data.tar.gz: ca12ab5f19b1037d50cc818a940b4e909521ebe7d4338fc552cb0901600eb61d713213f04d576713bfa146e971d050af9599d48d2eecea87013109aa190a39f8
6
+ metadata.gz: 5fb46bcc0515d4f4762dc9c601b65131a493153ed85e48e83ac64ae70b0b2060d0780d867560bfc8e50ed4c461ea62e1aeba5b4e1b6b55572656196e9958840b
7
+ data.tar.gz: 235421d689aa362a858a813fd369cef67673b0c6b13ccf50f6733a603746c3dfc00bde16fdd73fb1dd4358e9457c70d61c033c60d409e3f721eb0b34cc5c40bd
@@ -1,9 +1,19 @@
1
1
  # Change log
2
2
 
3
- ## [v0.4.0] - 2020-07-29
3
+ ## [v0.5.0] - 2020-09-27
4
4
 
5
5
  ### Added
6
+ * Add :message_format option to customize how messages are displayed in the console
7
+ by Josh Greenwood (@JoshTGreenwood)
8
+
9
+ ### Fixed
10
+ * Fix to select event name from valid log types or current level
11
+ by Ryan Schlesinger (@ryansch)
12
+ * Fix duplicate filters attribute definition in TTY::Logger::Config
6
13
 
14
+ ## [v0.4.0] - 2020-07-29
15
+
16
+ ### Added
7
17
  * Allow editing logger configuration at runtime ([#10](https://github.com/piotrmurach/tty-logger/pull/10))
8
18
  * Support for the `<<` streaming operator ([#9](https://github.com/piotrmurach/tty-logger/pull/9)))
9
19
 
@@ -40,6 +50,7 @@
40
50
 
41
51
  * Initial implementation and release
42
52
 
53
+ [v0.5.0]: https://github.com/piotrmurach/tty-logger/compare/v0.4.0..v0.5.0
43
54
  [v0.4.0]: https://github.com/piotrmurach/tty-logger/compare/v0.3.0..v0.4.0
44
55
  [v0.3.0]: https://github.com/piotrmurach/tty-logger/compare/v0.2.0..v0.3.0
45
56
  [v0.2.0]: https://github.com/piotrmurach/tty-logger/compare/v0.1.0..v0.2.0
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <div align="center">
2
- <a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="tty logo" /></a>
2
+ <a href="https://ttytoolkit.org" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="tty logo" /></a>
3
3
  </div>
4
4
 
5
5
  # TTY::Logger [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
@@ -42,7 +42,7 @@
42
42
  Add this line to your application's Gemfile:
43
43
 
44
44
  ```ruby
45
- gem 'tty-logger'
45
+ gem "tty-logger"
46
46
  ```
47
47
 
48
48
  And then execute:
@@ -62,7 +62,7 @@ Or install it yourself as:
62
62
  * [2.1.1 Exceptions](#211-exceptions)
63
63
  * [2.1.2 Types](#212-types)
64
64
  * [2.2 Levels](#22-levels)
65
- * [2.2.1 Scoped Level](#22-scoped-levels)
65
+ * [2.2.1 Scoped Level](#22-scoped-level)
66
66
  * [2.3 Structured Data](#23-structured-data)
67
67
  * [2.4 Configuration](#24-configuration)
68
68
  * [2.4.1 Metadata](#241-metadata)
@@ -294,7 +294,7 @@ end
294
294
  # • debug logged
295
295
  ```
296
296
 
297
- Or elevating level to error with a constant `ERROR_LEVEL`:
297
+ Or elevate a level to an error with a constant `ERROR_LEVEL`:
298
298
 
299
299
  ```ruby
300
300
  logger.log_at TTY::Logger::ERROR_LEVEL do
@@ -346,6 +346,8 @@ All the configuration options can be changed globally via `configure` or per log
346
346
  * `:max_depth` - the maximum depth for nested structured data. Defaults to `3`.
347
347
  * `:metadata` - the meta info to display before the message, can be `:pid`, `:date`, `:time` or `:file`. Defaults to empty array `[]`, no metadata. Setting this to `:all` will print all the metadata.
348
348
  * `:types` - the new custom log types. Defaults to `{}`.
349
+ * `:date_format` - uses `strftime` format to display dates. Defaults to `"%F"`.
350
+ * `:time_format` - uses `strftime` format to display times. Defaults to `"%T.%3N"`.
349
351
 
350
352
  For example, to configure `:max_bytes`, `:level` and `:metadata` for all logger instances do:
351
353
 
@@ -549,6 +551,7 @@ The console handler prints log messages to the console. It supports the followin
549
551
  * `:styles` - a hash of styling options.
550
552
  * `:formatter` - the formatter for log messages. Defaults to `:text`
551
553
  * `:output` - the device to log error messages to. Defaults to `$stderr`
554
+ * `:message_format` - uses `sprintf` format to display messages. Defaults to `"%-25s"`.
552
555
 
553
556
  The supported options in the `:styles` are:
554
557
 
@@ -604,6 +607,15 @@ styled_logger.error("Custom error")
604
607
  # ! Dooh Custom error
605
608
  ```
606
609
 
610
+ To increase message padding to a percentage of terminal width (depends on [tty-screen](https://github.com/piotrmurach/tty-screen/)):
611
+
612
+ ```ruby
613
+ TTY::Logger.new do |config|
614
+ padding = (TTY::Screen.columns * 0.4).to_i
615
+ config.handlers = [[:console, { message_format: "%-#{padding}s" }]]
616
+ end
617
+ ```
618
+
607
619
  #### 2.6.2 Stream handler
608
620
 
609
621
  To send log event data outside of console to another service or `IO` stream, you can use `:stream` handler.
@@ -104,6 +104,7 @@ module TTY
104
104
  @data_filter = DataFilter.new(@config.filters.data,
105
105
  mask: @config.filters.mask)
106
106
 
107
+ @types = LOG_TYPES.dup
107
108
  @config.types.each do |name, log_level|
108
109
  add_type(name, log_level)
109
110
  end
@@ -120,9 +121,11 @@ module TTY
120
121
  #
121
122
  # @api private
122
123
  def add_type(name, log_level)
123
- if respond_to?(name)
124
+ if @types.include?(name)
124
125
  raise Error, "Already defined log type #{name.inspect}"
125
126
  end
127
+
128
+ @types[name.to_sym] = log_level
126
129
  self.class.define_level(name, log_level)
127
130
  end
128
131
 
@@ -226,8 +229,7 @@ module TTY
226
229
 
227
230
  self
228
231
  end
229
-
230
- alias_method :<<, :write
232
+ alias << write
231
233
 
232
234
  # Log a message given the severtiy level
233
235
  #
@@ -254,7 +256,7 @@ module TTY
254
256
  level: current_level,
255
257
  time: Time.now,
256
258
  pid: Process.pid,
257
- name: /<top\s+\(required\)>|<main>|<</ =~ label ? current_level : label,
259
+ name: @types.include?(label.to_sym) ? label : current_level,
258
260
  path: loc.path,
259
261
  lineno: loc.lineno,
260
262
  method: loc.base_label
@@ -7,15 +7,12 @@ module TTY
7
7
  class Config
8
8
  FILTERED = "[FILTERED]"
9
9
 
10
- # The format used for date display
10
+ # The format used for date display. uses strftime format
11
11
  attr_accessor :date_format
12
12
 
13
- # The format used for time display
13
+ # The format used for time display. uses strftime format
14
14
  attr_accessor :time_format
15
15
 
16
- # The filters to hide sensitive data from the messages and data.
17
- attr_accessor :filters
18
-
19
16
  # The format used for displaying structured data
20
17
  attr_accessor :formatter
21
18
 
@@ -28,7 +25,7 @@ module TTY
28
25
  # The maximum message size to be logged in bytes. Defaults to 8192
29
26
  attr_accessor :max_bytes
30
27
 
31
- # The maximum depth for formattin array and hash objects. Defaults to 3
28
+ # The maximum depth for formatting array and hash objects. Defaults to 3
32
29
  attr_accessor :max_depth
33
30
 
34
31
  # The meta info to display, can be :date, :time, :file, :pid. Defaults to []
@@ -66,7 +63,7 @@ module TTY
66
63
  end
67
64
 
68
65
  def to_h
69
- {message: @message, data: @data, mask: @mask}
66
+ { message: @message, data: @data, mask: @mask }
70
67
  end
71
68
 
72
69
  def to_s
@@ -92,6 +89,7 @@ module TTY
92
89
  def to_proc
93
90
  -> (config) {
94
91
  config.date_format = @date_format.dup
92
+ config.time_format = @time_format.dup
95
93
  config.filters = @filters.dup
96
94
  config.formatter = @formatter
97
95
  config.handlers = @handlers.dup
@@ -100,7 +98,6 @@ module TTY
100
98
  config.max_depth = @max_depth
101
99
  config.metadata = @metadata.dup
102
100
  config.output = @output.dup
103
- config.time_format = @time_format.dup
104
101
  config.types = @types.dup
105
102
  config
106
103
  }
@@ -37,11 +37,9 @@ module TTY
37
37
  def filter(obj)
38
38
  return obj if filters.empty?
39
39
 
40
- hash = obj.reduce({}) do |acc, (k, v)|
40
+ obj.each_with_object({}) do |(k, v), acc|
41
41
  acc[k] = filter_val(k, v)
42
- acc
43
42
  end
44
- hash
45
43
  end
46
44
 
47
45
  private
@@ -50,7 +48,7 @@ module TTY
50
48
  compiled = {
51
49
  regexps: [],
52
50
  nested_regexps: [],
53
- blocks: [],
51
+ blocks: []
54
52
  }
55
53
  strings = []
56
54
  nested_strings = []
@@ -105,9 +103,8 @@ module TTY
105
103
  end
106
104
 
107
105
  def filter_obj(_key, obj, composite)
108
- obj.reduce({}) do |acc, (k, v)|
106
+ obj.each_with_object({}) do |(k, v), acc|
109
107
  acc[k] = filter_val(k, v, composite)
110
- acc
111
108
  end
112
109
  end
113
110
 
@@ -20,7 +20,7 @@ module TTY
20
20
  def dump(obj, max_bytes: 2**12, max_depth: 3)
21
21
  bytesize = 0
22
22
 
23
- hash = obj.reduce({}) do |acc, (k, v)|
23
+ hash = obj.each_with_object({}) do |(k, v), acc|
24
24
  str = (k.to_json + v.to_json)
25
25
  items = acc.keys.size - 1
26
26
 
@@ -31,7 +31,6 @@ module TTY
31
31
  bytesize += str.bytesize
32
32
  acc[k] = dump_val(v, max_depth)
33
33
  end
34
- acc
35
34
  end
36
35
  ::JSON.generate(hash)
37
36
  end
@@ -50,13 +49,13 @@ module TTY
50
49
  def enc_obj(obj, depth)
51
50
  return ELLIPSIS if depth.zero?
52
51
 
53
- obj.reduce({}) { |acc, (k, v)| acc[k] = dump_val(v, depth); acc }
52
+ obj.each_with_object({}) { |(k, v), acc| acc[k] = dump_val(v, depth) }
54
53
  end
55
54
 
56
55
  def enc_arr(obj, depth)
57
56
  return ELLIPSIS if depth.zero?
58
57
 
59
- obj.reduce([]) { |acc, v| acc << dump_val(v, depth); acc }
58
+ obj.each_with_object([]) { |v, acc| acc << dump_val(v, depth) }
60
59
  end
61
60
  end # JSON
62
61
  end # Formatters
@@ -32,12 +32,13 @@ module TTY
32
32
  def dump(obj, max_bytes: 2**12, max_depth: 3)
33
33
  bytesize = 0
34
34
 
35
- line = obj.reduce([]) do |acc, (k, v)|
35
+ line = obj.each_with_object([]) do |(k, v), acc|
36
36
  str = "#{dump_key(k)}=#{dump_val(v, max_depth)}"
37
37
  items = acc.size - 1
38
38
 
39
39
  if bytesize + str.bytesize + items > max_bytes
40
- if bytesize + items + (acc[-1].bytesize - ELLIPSIS.bytesize) > max_bytes
40
+ if bytesize + items +
41
+ (acc[-1].bytesize - ELLIPSIS.bytesize) > max_bytes
41
42
  acc.pop
42
43
  end
43
44
  acc << ELLIPSIS
@@ -46,7 +47,6 @@ module TTY
46
47
  bytesize += str.bytesize
47
48
  acc << str
48
49
  end
49
- acc
50
50
  end
51
51
  line.join(SPACE)
52
52
  end
@@ -86,8 +86,8 @@ module TTY
86
86
  return LBRACE + ELLIPSIS + RBRACE if depth.zero?
87
87
 
88
88
  LBRACE +
89
- obj.map { |k, v| "#{dump_key(k)}=#{dump_val(v, depth)}" }.join(SPACE) +
90
- RBRACE
89
+ obj.map { |k, v| "#{dump_key(k)}=#{dump_val(v, depth)}" }
90
+ .join(SPACE) + RBRACE
91
91
  end
92
92
 
93
93
  def enc_arr(array, depth)
@@ -113,7 +113,8 @@ module TTY
113
113
  case str
114
114
  when SINGLE_QUOTE_REGEX
115
115
  str.inspect
116
- when ESCAPE_STR_REGEX, LITERAL_TRUE, LITERAL_FALSE, LITERAL_NIL, NUM_REGEX
116
+ when ESCAPE_STR_REGEX, LITERAL_TRUE, LITERAL_FALSE,
117
+ LITERAL_NIL, NUM_REGEX
117
118
  ESCAPE_DOUBLE_QUOTE + str.inspect[1..-2] + ESCAPE_DOUBLE_QUOTE
118
119
  else
119
120
  str
@@ -55,7 +55,7 @@ module TTY
55
55
  color: :cyan,
56
56
  levelpad: 0
57
57
  }
58
- }
58
+ }.freeze
59
59
 
60
60
  TEXT_REGEXP = /([{}()\[\]])?(["']?)(\S+?)(["']?=)/.freeze
61
61
  JSON_REGEXP = /\"([^,]+?)\"(?=:)/.freeze
@@ -65,14 +65,24 @@ module TTY
65
65
  json: [JSON_REGEXP, ->(c) { "\"" + c.("\\1") + "\"" }]
66
66
  }.freeze
67
67
 
68
+ # The output stream
69
+ # @api private
68
70
  attr_reader :output
69
71
 
72
+ # The configuration options
73
+ # @api private
70
74
  attr_reader :config
71
75
 
76
+ # The logging level
77
+ # @api private
72
78
  attr_reader :level
73
79
 
80
+ # The format for the message
81
+ # @api private
82
+ attr_reader :message_format
83
+
74
84
  def initialize(output: $stderr, formatter: nil, config: nil, level: nil,
75
- styles: {})
85
+ styles: {}, message_format: "%-25s")
76
86
  @output = Array[output].flatten
77
87
  @formatter = coerce_formatter(formatter || config.formatter).new
78
88
  @formatter_name = @formatter.class.name.split("::").last.downcase
@@ -82,6 +92,7 @@ module TTY
82
92
  @level = level || @config.level
83
93
  @mutex = Mutex.new
84
94
  @pastel = Pastel.new
95
+ @message_format = message_format
85
96
  end
86
97
 
87
98
  # Handle log event output in format
@@ -118,7 +129,7 @@ module TTY
118
129
  fmt << color.(style[:symbol])
119
130
  fmt << color.(style[:label]) + (" " * style[:levelpad])
120
131
  end
121
- fmt << "%-25s" % event.message.join(" ")
132
+ fmt << message_format % event.message.join(" ")
122
133
  unless event.fields.empty?
123
134
  pattern, replacement = *@color_pattern
124
135
  fmt << @formatter.dump(event.fields, max_bytes: config.max_bytes,
@@ -150,11 +161,11 @@ module TTY
150
161
  #
151
162
  # @api private
152
163
  def configure_styles(event)
153
- return {} if event.metadata[:name].nil?
164
+ return {} if event.metadata[:name].nil?
154
165
 
155
166
  STYLES.fetch(event.metadata[:name].to_sym, {})
156
- .dup
157
- .merge!(@styles[event.metadata[:name].to_sym] || {})
167
+ .dup
168
+ .merge!(@styles[event.metadata[:name].to_sym] || {})
158
169
  end
159
170
 
160
171
  def configure_color(style)
@@ -15,7 +15,7 @@ module TTY
15
15
  warn: WARN_LEVEL,
16
16
  error: ERROR_LEVEL,
17
17
  fatal: FATAL_LEVEL
18
- }
18
+ }.freeze
19
19
 
20
20
  # All the default level names
21
21
  #
@@ -60,6 +60,7 @@ module TTY
60
60
  right = right.is_a?(Integer) ? right : level_to_number(right)
61
61
 
62
62
  return :eq if left == right
63
+
63
64
  left < right ? :lt : :gt
64
65
  end
65
66
  end # Levels
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Logger
5
- VERSION = "0.4.0"
6
- end
7
- end
5
+ VERSION = "0.5.0"
6
+ end # Logger
7
+ end # TTY
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-29 00:00:00.000000000 Z
11
+ date: 2020-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel