klogger-logger 1.0.0 → 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 +52 -33
  3. metadata +22 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 721caa676f9ec6ff59d0faa7823f40b63c9ca1fbcde1ac017e19c0f809000574
4
- data.tar.gz: '0686ea87fa8f6f86a79b599ed11009061608adcf2884069381fad7f4b6a69d4e'
3
+ metadata.gz: eb61b7c35a079dfda06d125ae652fd71d78b994f710aafaf687eb177ec822b8d
4
+ data.tar.gz: b837d26cf1f5d59833a01e6b4c547e5ab05c60f1b23e464c894255010679102b
5
5
  SHA512:
6
- metadata.gz: 594e56869cb217f308d49e1d5ab5a6669b6c69cb617e64f853a357fd8a30b8d2fc88803157cf54bb7ee9e12641ab2264fab364d7e57f5814c712bbdeb8238805
7
- data.tar.gz: 737f6c90d84067dbb1f6697f5f3c6466c83ebc5d1dd3054481b4397bf40737705b4a345d9901fb923768e28374615c0c428f113beb113d3d56785edcedd4de5c
6
+ metadata.gz: d421b706f0460136f38f1219053ce0b2aa55f9bf1c972113c75eaf3caa8a82f1cec798f1558226f6e9bac9fff13d2cb82a44d387d76d3aa45c5a85aa0cdce3cf
7
+ data.tar.gz: 5a2eae41db68d08a53fb92e7118ffe1617a34588dffaf037867d0885e099956900cf0c96252b9f9739992639ee628dc220c7d7bf6e7e678dee2f63c39867c652
@@ -1,16 +1,18 @@
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'
8
+ require 'concurrent/atomic/thread_local_var'
7
9
 
8
10
  module Klogger
9
11
  class Logger < ::Logger
10
12
 
11
13
  attr_reader :name
12
14
  attr_reader :destinations
13
- attr_reader :extra
15
+ attr_reader :tags
14
16
 
15
17
  LEVELS = [:debug, :info, :warn, :error, :fatal].freeze
16
18
  FORMATTERS = {
@@ -19,51 +21,54 @@ module Klogger
19
21
  go: Formatters::Go
20
22
  }.freeze
21
23
 
22
- def initialize(name, destination: $stdout, formatter: :json, highlight: false, extra: {})
24
+ def initialize(name, destination: $stdout, formatter: :json, highlight: false, include_group_ids: false, tags: {})
23
25
  @name = name
24
- @extra = extra
26
+ @tags = tags
25
27
  @destinations = []
28
+ @groups = Concurrent::ThreadLocalVar.new { [] }
29
+ @silenced = Concurrent::ThreadLocalVar.new { false }
30
+ @include_group_ids = include_group_ids
26
31
 
27
32
  super(destination)
28
33
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
29
34
  end
30
35
 
31
- def exception(exception, message = nil, **additional)
36
+ def exception(exception, message = nil, **tags)
32
37
  error({ message: message,
33
38
  exception: exception.class.name,
34
39
  exception_message: exception.message,
35
- backtrace: exception.backtrace[0, 4].join("\n") }.merge(additional))
40
+ backtrace: exception.backtrace[0, 4].join("\n") }.merge(tags))
36
41
  end
37
42
 
38
43
  LEVELS.each do |level|
39
- define_method(level) do |message = nil, progname = nil, **additional, &block|
40
- add(Logger.const_get(level.to_s.upcase), message, progname, **additional, &block)
44
+ define_method(level) do |message = nil, progname = nil, **tags, &block|
45
+ add(Logger.const_get(level.to_s.upcase), message, progname, **tags, &block)
41
46
  end
42
47
  end
43
48
 
44
- def group(**additional)
45
- groups << additional
49
+ def group(**tags)
50
+ @groups.value += [{ id: SecureRandom.hex(4), tags: tags }]
46
51
  yield
47
52
  ensure
48
- groups.pop
53
+ @groups.value.pop
49
54
  end
50
55
 
51
56
  def silence!
52
- @silence = true
57
+ @silenced.value = true
53
58
  yield if block_given?
54
59
  ensure
55
60
  unsilence! if block_given?
56
61
  end
57
62
 
58
63
  def unsilence!
59
- @silence = false
64
+ @silenced.value = false
60
65
  yield if block_given?
61
66
  ensure
62
67
  silence! if block_given?
63
68
  end
64
69
 
65
70
  def silenced?
66
- @silence == true
71
+ @silenced.value == true
67
72
  end
68
73
 
69
74
  def add_destination(destination)
@@ -76,33 +81,26 @@ module Klogger
76
81
 
77
82
  private
78
83
 
79
- def groups
80
- @groups ||= []
81
- end
82
-
83
- def add(severity, message = nil, progname = nil, **extra, &block)
84
+ def add(severity, message = nil, progname = nil, **tags, &block)
84
85
  return if silenced?
85
86
 
86
87
  severity ||= Logger::UNKNOWN
87
88
  return if severity < level
88
89
 
89
- payload = create_payload(severity, message, extra)
90
-
91
- @destinations.each do |destination|
92
- destination.call(self, payload.dup)
93
- rescue StandardError => e
94
- # If something goes wrong in here, we don't want to break the application
95
- # so we will rescue that and we'll just use standard warn.
96
- Kernel.warn "Error while sending payload to destination (#{e.class}): #{e.message}"
90
+ if message && block_given?
91
+ message = "#{message}: #{block.call}"
92
+ elsif block_given?
93
+ message = block.call
97
94
  end
98
95
 
96
+ payload, group_ids = create_payload(severity, message, tags)
97
+ call_destinations(payload, group_ids)
99
98
  super(severity, payload, progname, &block)
100
99
  end
101
100
 
102
- # rubocop:disable Metrics/AbcSize
103
- def create_payload(severity, message, extra)
101
+ def create_payload(severity, message, tags)
104
102
  payload = { time: Time.now.to_s, severity: LEVELS[severity]&.to_s, logger: @name }
105
- payload.merge!(@extra)
103
+ payload.merge!(@tags)
106
104
 
107
105
  if message.is_a?(Hash)
108
106
  payload.merge!(message)
@@ -110,14 +108,35 @@ module Klogger
110
108
  payload[:message] = message
111
109
  end
112
110
 
113
- payload.merge!(extra)
111
+ payload.merge!(tags)
114
112
  payload.delete(:message) if payload[:message].nil?
115
113
  payload.compact!
116
114
 
117
- groups.each { |group| payload.merge!(group) }
118
- payload
115
+ group_ids = add_groups_to_payload(payload)
116
+
117
+ [payload, group_ids]
118
+ end
119
+
120
+ def call_destinations(payload, group_ids)
121
+ @destinations.each do |destination|
122
+ destination.call(self, payload.dup, group_ids)
123
+ rescue StandardError => e
124
+ # If something goes wrong in here, we don't want to break the application
125
+ # so we will rescue that and we'll just use standard warn.
126
+ Kernel.warn "Error while sending payload to destination (#{e.class}): #{e.message}"
127
+ end
128
+ end
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
119
139
  end
120
- # rubocop:enable Metrics/AbcSize
121
140
 
122
141
  end
123
142
  end
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klogger-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
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
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: json
15
35
  requirement: !ruby/object:Gem::Requirement