klogger-logger 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/klogger/logger.rb +27 -14
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ac760c41d8504df687dc68832aeae5aee47757205521a40807dec10ee9e9833
4
- data.tar.gz: 9f2a9ea6b0a464b81c82b5413d6ff733ad82f3aaa875cd7223e6b92da0523ea4
3
+ metadata.gz: eb61b7c35a079dfda06d125ae652fd71d78b994f710aafaf687eb177ec822b8d
4
+ data.tar.gz: b837d26cf1f5d59833a01e6b4c547e5ab05c60f1b23e464c894255010679102b
5
5
  SHA512:
6
- metadata.gz: 9819d09b8cc7e2b9bf069af493dd82e1397b4f25a767879d277159a4ab4dbc90a638080cd5519fb0632a1eeddd68688a4cd1dabb9fedeabc9ff0676909e52488
7
- data.tar.gz: 4f01f2e1612548ebaa408ccbb25b4fa3b479d0834a384f1a3812b7b3bfbef2600ee31b9b14faaf4bc9b3385256779ed415d7ffbcabf80cc15b481d0a7cd756dd
6
+ metadata.gz: d421b706f0460136f38f1219053ce0b2aa55f9bf1c972113c75eaf3caa8a82f1cec798f1558226f6e9bac9fff13d2cb82a44d387d76d3aa45c5a85aa0cdce3cf
7
+ data.tar.gz: 5a2eae41db68d08a53fb92e7118ffe1617a34588dffaf037867d0885e099956900cf0c96252b9f9739992639ee628dc220c7d7bf6e7e678dee2f63c39867c652
@@ -1,6 +1,7 @@
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'
@@ -20,11 +21,13 @@ module Klogger
20
21
  go: Formatters::Go
21
22
  }.freeze
22
23
 
23
- def initialize(name, destination: $stdout, formatter: :json, highlight: false, tags: {})
24
+ def initialize(name, destination: $stdout, formatter: :json, highlight: false, include_group_ids: false, tags: {})
24
25
  @name = name
25
26
  @tags = tags
26
27
  @destinations = []
27
- @groups = Concurrent::ThreadLocalVar.new([])
28
+ @groups = Concurrent::ThreadLocalVar.new { [] }
29
+ @silenced = Concurrent::ThreadLocalVar.new { false }
30
+ @include_group_ids = include_group_ids
28
31
 
29
32
  super(destination)
30
33
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
@@ -44,28 +47,28 @@ module Klogger
44
47
  end
45
48
 
46
49
  def group(**tags)
47
- @groups.value += [tags]
50
+ @groups.value += [{ id: SecureRandom.hex(4), tags: tags }]
48
51
  yield
49
52
  ensure
50
53
  @groups.value.pop
51
54
  end
52
55
 
53
56
  def silence!
54
- @silence = true
57
+ @silenced.value = true
55
58
  yield if block_given?
56
59
  ensure
57
60
  unsilence! if block_given?
58
61
  end
59
62
 
60
63
  def unsilence!
61
- @silence = false
64
+ @silenced.value = false
62
65
  yield if block_given?
63
66
  ensure
64
67
  silence! if block_given?
65
68
  end
66
69
 
67
70
  def silenced?
68
- @silence == true
71
+ @silenced.value == true
69
72
  end
70
73
 
71
74
  def add_destination(destination)
@@ -90,12 +93,11 @@ module Klogger
90
93
  message = block.call
91
94
  end
92
95
 
93
- payload = create_payload(severity, message, tags)
94
- call_destinations(payload)
96
+ payload, group_ids = create_payload(severity, message, tags)
97
+ call_destinations(payload, group_ids)
95
98
  super(severity, payload, progname, &block)
96
99
  end
97
100
 
98
- # rubocop:disable Metrics/AbcSize
99
101
  def create_payload(severity, message, tags)
100
102
  payload = { time: Time.now.to_s, severity: LEVELS[severity]&.to_s, logger: @name }
101
103
  payload.merge!(@tags)
@@ -110,14 +112,14 @@ module Klogger
110
112
  payload.delete(:message) if payload[:message].nil?
111
113
  payload.compact!
112
114
 
113
- @groups.value.each { |group| payload.merge!(group) }
114
- payload
115
+ group_ids = add_groups_to_payload(payload)
116
+
117
+ [payload, group_ids]
115
118
  end
116
- # rubocop:enable Metrics/AbcSize
117
119
 
118
- def call_destinations(payload)
120
+ def call_destinations(payload, group_ids)
119
121
  @destinations.each do |destination|
120
- destination.call(self, payload.dup)
122
+ destination.call(self, payload.dup, group_ids)
121
123
  rescue StandardError => e
122
124
  # If something goes wrong in here, we don't want to break the application
123
125
  # so we will rescue that and we'll just use standard warn.
@@ -125,5 +127,16 @@ module Klogger
125
127
  end
126
128
  end
127
129
 
130
+ def add_groups_to_payload(payload)
131
+ group_ids = []
132
+ @groups.value.each do |group|
133
+ payload.merge!(group[:tags])
134
+ group_ids << group[:id]
135
+ end
136
+
137
+ payload[:groups] = group_ids.join(',') if @include_group_ids
138
+ group_ids
139
+ end
140
+
128
141
  end
129
142
  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.1.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