klogger-logger 1.1.0 → 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: eb61b7c35a079dfda06d125ae652fd71d78b994f710aafaf687eb177ec822b8d
4
- data.tar.gz: b837d26cf1f5d59833a01e6b4c547e5ab05c60f1b23e464c894255010679102b
3
+ metadata.gz: d829e62b7472acd3c8c20e0ce8d5e3719c3bf2a5fa6752f23c503b64e56db99f
4
+ data.tar.gz: d8a76281e8ac428c1c5ffacffcc6edc24c3404742e755a5f831287aa0b1862d6
5
5
  SHA512:
6
- metadata.gz: d421b706f0460136f38f1219053ce0b2aa55f9bf1c972113c75eaf3caa8a82f1cec798f1558226f6e9bac9fff13d2cb82a44d387d76d3aa45c5a85aa0cdce3cf
7
- data.tar.gz: 5a2eae41db68d08a53fb92e7118ffe1617a34588dffaf037867d0885e099956900cf0c96252b9f9739992639ee628dc220c7d7bf6e7e678dee2f63c39867c652
6
+ metadata.gz: 58b478bddf41b569b4af77b0ef19ca70fccfd4b4f06f31d12c8c411f00f31ef1943f662f264b176e21c9df8087965187e525ddb11f4fad50a4b8e55fa00dcc98
7
+ data.tar.gz: 73cf53c1c9bd298e5b09518e2ce7f7903d077b9c81648a156a1ea1da61b20f9a287ec3e0cea5a3acde0b97a544f449b1b295466f978741db1b7862e9792a5b6b
@@ -15,7 +15,7 @@ module Klogger
15
15
  string = String.new
16
16
  string << time.strftime('%Y-%m-%d %H:%M:%S %z')
17
17
  string << ' '
18
- string << colorize(payload[:severity].ljust(8, ' ').upcase, payload[:severity].to_sym)
18
+ string << colorize(payload[:severity].ljust(7, ' ').upcase, payload[:severity].to_sym)
19
19
  if payload[:message]
20
20
  string << colorize(payload[:message], :white)
21
21
  string << ' '
@@ -27,7 +27,7 @@ module Klogger
27
27
  string << colorize(sanitize_value(value), :white)
28
28
  string << ' '
29
29
  end
30
- string + "\n"
30
+ string.strip + "\n"
31
31
  end
32
32
  # rubocop:enable Metrics/AbcSize
33
33
  # rubocop:enable Metrics/MethodLength
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'concurrent/atomic/thread_local_var'
4
+
5
+ module Klogger
6
+ class GroupSet
7
+
8
+ def initialize
9
+ @groups = Concurrent::ThreadLocalVar.new { [] }
10
+ end
11
+
12
+ def groups
13
+ @groups.value
14
+ end
15
+
16
+ def call(**tags)
17
+ add(**tags)
18
+ yield
19
+ ensure
20
+ pop
21
+ end
22
+
23
+ def add(**tags)
24
+ id = SecureRandom.hex(4)
25
+ @groups.value += [{ id: id, tags: tags }]
26
+ id
27
+ end
28
+
29
+ def pop
30
+ @groups.value.pop
31
+ end
32
+
33
+ end
34
+ end
@@ -6,6 +6,7 @@ require 'klogger/formatters/json'
6
6
  require 'klogger/formatters/simple'
7
7
  require 'klogger/formatters/go'
8
8
  require 'concurrent/atomic/thread_local_var'
9
+ require 'klogger/group_set'
9
10
 
10
11
  module Klogger
11
12
  class Logger < ::Logger
@@ -21,14 +22,14 @@ module Klogger
21
22
  go: Formatters::Go
22
23
  }.freeze
23
24
 
24
- def initialize(name, destination: $stdout, formatter: :json, highlight: false, include_group_ids: false, tags: {})
25
+ def initialize(name = nil, destination: $stdout, formatter: :go, highlight: false, include_group_ids: false,
26
+ tags: {})
25
27
  @name = name
26
28
  @tags = tags
27
29
  @destinations = []
28
- @groups = Concurrent::ThreadLocalVar.new { [] }
30
+ @group_set = GroupSet.new
29
31
  @silenced = Concurrent::ThreadLocalVar.new { false }
30
32
  @include_group_ids = include_group_ids
31
-
32
33
  super(destination)
33
34
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
34
35
  end
@@ -46,11 +47,16 @@ module Klogger
46
47
  end
47
48
  end
48
49
 
49
- def group(**tags)
50
- @groups.value += [{ id: SecureRandom.hex(4), tags: tags }]
51
- yield
52
- ensure
53
- @groups.value.pop
50
+ def group(**tags, &block)
51
+ @group_set.call(**tags, &block)
52
+ end
53
+
54
+ def add_group(**tags)
55
+ @group_set.add(**tags)
56
+ end
57
+
58
+ def pop_group
59
+ @group_set.pop
54
60
  end
55
61
 
56
62
  def silence!
@@ -99,7 +105,8 @@ module Klogger
99
105
  end
100
106
 
101
107
  def create_payload(severity, message, tags)
102
- payload = { time: Time.now.to_s, severity: LEVELS[severity]&.to_s, logger: @name }
108
+ payload = { time: Time.now.to_s, severity: LEVELS[severity]&.to_s }
109
+ payload[:logger] = @name if @name
103
110
  payload.merge!(@tags)
104
111
 
105
112
  if message.is_a?(Hash)
@@ -129,9 +136,12 @@ module Klogger
129
136
 
130
137
  def add_groups_to_payload(payload)
131
138
  group_ids = []
132
- @groups.value.each do |group|
133
- payload.merge!(group[:tags])
134
- group_ids << group[:id]
139
+
140
+ [Klogger.global_groups, @group_set].each do |group_set|
141
+ group_set.groups.each do |group|
142
+ payload.merge!(group[:tags])
143
+ group_ids << group[:id]
144
+ end
135
145
  end
136
146
 
137
147
  payload[:groups] = group_ids.join(',') if @include_group_ids
data/lib/klogger.rb CHANGED
@@ -8,4 +8,12 @@ module Klogger
8
8
  Logger.new(*args, **kwargs)
9
9
  end
10
10
 
11
+ def self.global_groups
12
+ @global_groups ||= GroupSet.new
13
+ end
14
+
15
+ def self.group(**tags, &block)
16
+ global_groups.call(**tags, &block)
17
+ end
18
+
11
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klogger-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
@@ -78,6 +78,7 @@ files:
78
78
  - lib/klogger/formatters/go.rb
79
79
  - lib/klogger/formatters/json.rb
80
80
  - lib/klogger/formatters/simple.rb
81
+ - lib/klogger/group_set.rb
81
82
  - lib/klogger/json_highlighter.rb
82
83
  - lib/klogger/logger.rb
83
84
  - lib/klogger/version.rb