klogger-logger 1.0.0 → 1.0.1

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 +33 -27
  3. metadata +21 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 721caa676f9ec6ff59d0faa7823f40b63c9ca1fbcde1ac017e19c0f809000574
4
- data.tar.gz: '0686ea87fa8f6f86a79b599ed11009061608adcf2884069381fad7f4b6a69d4e'
3
+ metadata.gz: 1ac760c41d8504df687dc68832aeae5aee47757205521a40807dec10ee9e9833
4
+ data.tar.gz: 9f2a9ea6b0a464b81c82b5413d6ff733ad82f3aaa875cd7223e6b92da0523ea4
5
5
  SHA512:
6
- metadata.gz: 594e56869cb217f308d49e1d5ab5a6669b6c69cb617e64f853a357fd8a30b8d2fc88803157cf54bb7ee9e12641ab2264fab364d7e57f5814c712bbdeb8238805
7
- data.tar.gz: 737f6c90d84067dbb1f6697f5f3c6466c83ebc5d1dd3054481b4397bf40737705b4a345d9901fb923768e28374615c0c428f113beb113d3d56785edcedd4de5c
6
+ metadata.gz: 9819d09b8cc7e2b9bf069af493dd82e1397b4f25a767879d277159a4ab4dbc90a638080cd5519fb0632a1eeddd68688a4cd1dabb9fedeabc9ff0676909e52488
7
+ data.tar.gz: 4f01f2e1612548ebaa408ccbb25b4fa3b479d0834a384f1a3812b7b3bfbef2600ee31b9b14faaf4bc9b3385256779ed415d7ffbcabf80cc15b481d0a7cd756dd
@@ -4,13 +4,14 @@ require 'logger'
4
4
  require 'klogger/formatters/json'
5
5
  require 'klogger/formatters/simple'
6
6
  require 'klogger/formatters/go'
7
+ require 'concurrent/atomic/thread_local_var'
7
8
 
8
9
  module Klogger
9
10
  class Logger < ::Logger
10
11
 
11
12
  attr_reader :name
12
13
  attr_reader :destinations
13
- attr_reader :extra
14
+ attr_reader :tags
14
15
 
15
16
  LEVELS = [:debug, :info, :warn, :error, :fatal].freeze
16
17
  FORMATTERS = {
@@ -19,33 +20,34 @@ module Klogger
19
20
  go: Formatters::Go
20
21
  }.freeze
21
22
 
22
- def initialize(name, destination: $stdout, formatter: :json, highlight: false, extra: {})
23
+ def initialize(name, destination: $stdout, formatter: :json, highlight: false, tags: {})
23
24
  @name = name
24
- @extra = extra
25
+ @tags = tags
25
26
  @destinations = []
27
+ @groups = Concurrent::ThreadLocalVar.new([])
26
28
 
27
29
  super(destination)
28
30
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
29
31
  end
30
32
 
31
- def exception(exception, message = nil, **additional)
33
+ def exception(exception, message = nil, **tags)
32
34
  error({ message: message,
33
35
  exception: exception.class.name,
34
36
  exception_message: exception.message,
35
- backtrace: exception.backtrace[0, 4].join("\n") }.merge(additional))
37
+ backtrace: exception.backtrace[0, 4].join("\n") }.merge(tags))
36
38
  end
37
39
 
38
40
  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)
41
+ define_method(level) do |message = nil, progname = nil, **tags, &block|
42
+ add(Logger.const_get(level.to_s.upcase), message, progname, **tags, &block)
41
43
  end
42
44
  end
43
45
 
44
- def group(**additional)
45
- groups << additional
46
+ def group(**tags)
47
+ @groups.value += [tags]
46
48
  yield
47
49
  ensure
48
- groups.pop
50
+ @groups.value.pop
49
51
  end
50
52
 
51
53
  def silence!
@@ -76,33 +78,27 @@ module Klogger
76
78
 
77
79
  private
78
80
 
79
- def groups
80
- @groups ||= []
81
- end
82
-
83
- def add(severity, message = nil, progname = nil, **extra, &block)
81
+ def add(severity, message = nil, progname = nil, **tags, &block)
84
82
  return if silenced?
85
83
 
86
84
  severity ||= Logger::UNKNOWN
87
85
  return if severity < level
88
86
 
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}"
87
+ if message && block_given?
88
+ message = "#{message}: #{block.call}"
89
+ elsif block_given?
90
+ message = block.call
97
91
  end
98
92
 
93
+ payload = create_payload(severity, message, tags)
94
+ call_destinations(payload)
99
95
  super(severity, payload, progname, &block)
100
96
  end
101
97
 
102
98
  # rubocop:disable Metrics/AbcSize
103
- def create_payload(severity, message, extra)
99
+ def create_payload(severity, message, tags)
104
100
  payload = { time: Time.now.to_s, severity: LEVELS[severity]&.to_s, logger: @name }
105
- payload.merge!(@extra)
101
+ payload.merge!(@tags)
106
102
 
107
103
  if message.is_a?(Hash)
108
104
  payload.merge!(message)
@@ -110,14 +106,24 @@ module Klogger
110
106
  payload[:message] = message
111
107
  end
112
108
 
113
- payload.merge!(extra)
109
+ payload.merge!(tags)
114
110
  payload.delete(:message) if payload[:message].nil?
115
111
  payload.compact!
116
112
 
117
- groups.each { |group| payload.merge!(group) }
113
+ @groups.value.each { |group| payload.merge!(group) }
118
114
  payload
119
115
  end
120
116
  # rubocop:enable Metrics/AbcSize
121
117
 
118
+ def call_destinations(payload)
119
+ @destinations.each do |destination|
120
+ destination.call(self, payload.dup)
121
+ rescue StandardError => e
122
+ # If something goes wrong in here, we don't want to break the application
123
+ # so we will rescue that and we'll just use standard warn.
124
+ Kernel.warn "Error while sending payload to destination (#{e.class}): #{e.message}"
125
+ end
126
+ end
127
+
122
128
  end
123
129
  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.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
@@ -10,6 +10,26 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2023-03-09 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