klogger-logger 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: 1ac760c41d8504df687dc68832aeae5aee47757205521a40807dec10ee9e9833
4
- data.tar.gz: 9f2a9ea6b0a464b81c82b5413d6ff733ad82f3aaa875cd7223e6b92da0523ea4
3
+ metadata.gz: d829e62b7472acd3c8c20e0ce8d5e3719c3bf2a5fa6752f23c503b64e56db99f
4
+ data.tar.gz: d8a76281e8ac428c1c5ffacffcc6edc24c3404742e755a5f831287aa0b1862d6
5
5
  SHA512:
6
- metadata.gz: 9819d09b8cc7e2b9bf069af493dd82e1397b4f25a767879d277159a4ab4dbc90a638080cd5519fb0632a1eeddd68688a4cd1dabb9fedeabc9ff0676909e52488
7
- data.tar.gz: 4f01f2e1612548ebaa408ccbb25b4fa3b479d0834a384f1a3812b7b3bfbef2600ee31b9b14faaf4bc9b3385256779ed415d7ffbcabf80cc15b481d0a7cd756dd
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
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'logger'
4
+ require 'securerandom'
4
5
  require 'klogger/formatters/json'
5
6
  require 'klogger/formatters/simple'
6
7
  require 'klogger/formatters/go'
7
8
  require 'concurrent/atomic/thread_local_var'
9
+ require 'klogger/group_set'
8
10
 
9
11
  module Klogger
10
12
  class Logger < ::Logger
@@ -20,12 +22,14 @@ module Klogger
20
22
  go: Formatters::Go
21
23
  }.freeze
22
24
 
23
- def initialize(name, destination: $stdout, formatter: :json, highlight: false, tags: {})
25
+ def initialize(name = nil, destination: $stdout, formatter: :go, highlight: false, include_group_ids: false,
26
+ tags: {})
24
27
  @name = name
25
28
  @tags = tags
26
29
  @destinations = []
27
- @groups = Concurrent::ThreadLocalVar.new([])
28
-
30
+ @group_set = GroupSet.new
31
+ @silenced = Concurrent::ThreadLocalVar.new { false }
32
+ @include_group_ids = include_group_ids
29
33
  super(destination)
30
34
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
31
35
  end
@@ -43,29 +47,34 @@ module Klogger
43
47
  end
44
48
  end
45
49
 
46
- def group(**tags)
47
- @groups.value += [tags]
48
- yield
49
- ensure
50
- @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
51
60
  end
52
61
 
53
62
  def silence!
54
- @silence = true
63
+ @silenced.value = true
55
64
  yield if block_given?
56
65
  ensure
57
66
  unsilence! if block_given?
58
67
  end
59
68
 
60
69
  def unsilence!
61
- @silence = false
70
+ @silenced.value = false
62
71
  yield if block_given?
63
72
  ensure
64
73
  silence! if block_given?
65
74
  end
66
75
 
67
76
  def silenced?
68
- @silence == true
77
+ @silenced.value == true
69
78
  end
70
79
 
71
80
  def add_destination(destination)
@@ -90,14 +99,14 @@ module Klogger
90
99
  message = block.call
91
100
  end
92
101
 
93
- payload = create_payload(severity, message, tags)
94
- call_destinations(payload)
102
+ payload, group_ids = create_payload(severity, message, tags)
103
+ call_destinations(payload, group_ids)
95
104
  super(severity, payload, progname, &block)
96
105
  end
97
106
 
98
- # rubocop:disable Metrics/AbcSize
99
107
  def create_payload(severity, message, tags)
100
- 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
101
110
  payload.merge!(@tags)
102
111
 
103
112
  if message.is_a?(Hash)
@@ -110,14 +119,14 @@ module Klogger
110
119
  payload.delete(:message) if payload[:message].nil?
111
120
  payload.compact!
112
121
 
113
- @groups.value.each { |group| payload.merge!(group) }
114
- payload
122
+ group_ids = add_groups_to_payload(payload)
123
+
124
+ [payload, group_ids]
115
125
  end
116
- # rubocop:enable Metrics/AbcSize
117
126
 
118
- def call_destinations(payload)
127
+ def call_destinations(payload, group_ids)
119
128
  @destinations.each do |destination|
120
- destination.call(self, payload.dup)
129
+ destination.call(self, payload.dup, group_ids)
121
130
  rescue StandardError => e
122
131
  # If something goes wrong in here, we don't want to break the application
123
132
  # so we will rescue that and we'll just use standard warn.
@@ -125,5 +134,19 @@ module Klogger
125
134
  end
126
135
  end
127
136
 
137
+ def add_groups_to_payload(payload)
138
+ group_ids = []
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
145
+ end
146
+
147
+ payload[:groups] = group_ids.join(',') if @include_group_ids
148
+ group_ids
149
+ end
150
+
128
151
  end
129
152
  end
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klogger-logger
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
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-09 00:00:00.000000000 Z
11
+ date: 2023-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -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