klogger-logger 1.3.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec65c2728e6aee2870704aeb67e2224f3ffe8b94173f0dafa5e54354f9ec4737
4
- data.tar.gz: 51244c5f4ab5fc54d5a9dda4aa14a1051fed37fd718b205c3bd14cf62b7d52ee
3
+ metadata.gz: 545977913e2a22754ceaa46726ed6c1ccaa28c4af5ff3bda20ddf76fbf0783ad
4
+ data.tar.gz: db4cd99e286083d70e768f659cb1446ab4d289a6ed6bbb36ae4f51000771e9ff
5
5
  SHA512:
6
- metadata.gz: dab76a4af78b3684b078caf4348712637b952bd4abdc61cf73ba5bc1722feb6754b3dee5eaa4d1a49f5bf22cb86c5a30a5570861822735a64c4c379c56bfe27b
7
- data.tar.gz: 0b4fe70463502a574a6e461aec57559bf26e1141ae24d1b129fb2320058fb1acba31c42979564db4e1058c42f4785034fdb586ea05a5e8b5c944c53a4c48ca20
6
+ metadata.gz: 30e4cb6330c169eea17379c012bca5e0b829fe06a4f2bd4597e5d7cde189c981aa514ce4e891ee7e32dcafc3391d8856db440cf759b8b8769abf51ec890f7d40
7
+ data.tar.gz: 39ceffeb22baa21c39f3ef2461a3e469535340e77bbb6795d59fc74078db17b813576c2f9a1df42f476e87cd16d6af295e88ecf3e83d6d047cb60accc9a4bc5f
data/README.md CHANGED
@@ -78,7 +78,7 @@ logger.info { "Hello world!" }
78
78
  logger.info('Result of 1 + 1') { 1 + 1 } # Logs with a message of "Result: 2"
79
79
  ```
80
80
 
81
- ### Loggging exceptions
81
+ ### Logging exceptions
82
82
 
83
83
  Exceptions happen and when they do, you want to know about them. Klogger provides a helper method to log exceptions. These will automatically be logged with the `error` severity.
84
84
 
@@ -135,7 +135,7 @@ Klogger.group(ip: '1.2.3.4') do
135
135
  end
136
136
 
137
137
  # If you can't use a block you can manually open and close a group but you'll need to be sure to close it
138
- # when you're finished.
138
+ # when you're finished.
139
139
  group_id = Klogger.global_groups.add(ip: '1.2.3.4')
140
140
  # ... do anything that you want - everything will be tagged as appropriate
141
141
  Klogger.global_groups.pop
@@ -149,6 +149,16 @@ logger.tagged(name: 'steve') do
149
149
  end
150
150
  ```
151
151
 
152
+ ### Tagged Loggers
153
+
154
+ If you wish to apply tags to a series of log entries but you don't wish to use blocks, you can create a "sub" logger which will always include those tags for all messages sent to it.
155
+
156
+ ```ruby
157
+ logger = Klogger.new(:logger)
158
+ tagged_logger = logger.create_tagged_logger(tag: 'my-tag')
159
+ tagged_logger.info "Hello world!" # => will be tagged with tag=my-tag
160
+ ```
161
+
152
162
  ### Silencing
153
163
 
154
164
  Sometimes you don't want to log for a little while. You can use the `silence` method to temporarily disable logging.
@@ -187,4 +197,9 @@ end
187
197
  # Create a logger and add the destination
188
198
  logger = Klogger.new(name)
189
199
  logger.add_destination(GraylogDestination.new('graylog.example.com', 12201))
200
+
201
+ # If you only want to send certain data to another block, you can do so
202
+ logger.with_destination(other_destination) do
203
+ # ...
204
+ end
190
205
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.2
1
+ 1.5.0
@@ -25,13 +25,18 @@ module Klogger
25
25
  go: Formatters::Go
26
26
  }.freeze
27
27
 
28
- def initialize(name = nil, destination: $stdout, formatter: :go, highlight: false, include_group_ids: false,
28
+ def initialize(name = nil,
29
+ destination: $stdout,
30
+ formatter: :go,
31
+ highlight: false,
32
+ include_group_ids: false,
29
33
  tags: {})
30
34
  @name = name
31
35
  @tags = tags
32
36
  @destinations = []
33
37
  @group_set = GroupSet.new
34
38
  @silenced = Concurrent::ThreadLocalVar.new { false }
39
+ @block_destinations = Concurrent::ThreadLocalVar.new { [] }
35
40
  @include_group_ids = include_group_ids
36
41
  super(destination)
37
42
  self.formatter = FORMATTERS[formatter].new(highlight: highlight)
@@ -39,12 +44,10 @@ module Klogger
39
44
 
40
45
  def exception(exception, message = nil, **tags)
41
46
  error(
42
- **{
43
- message: message,
44
- exception: exception.class.name,
45
- exception_message: exception.message,
46
- backtrace: exception.backtrace[0, 4].join("\n")
47
- }.merge(tags)
47
+ message: message,
48
+ exception: exception.class.name,
49
+ exception_message: exception.message,
50
+ backtrace: exception.backtrace[0, 4].join("\n"), **tags
48
51
  )
49
52
  end
50
53
 
@@ -96,6 +99,17 @@ module Klogger
96
99
  @destinations.delete(destination)
97
100
  end
98
101
 
102
+ def with_destination(destination)
103
+ @block_destinations.value << destination
104
+ yield
105
+ ensure
106
+ @block_destinations.value.delete(destination)
107
+ end
108
+
109
+ def create_tagged_logger(**tags)
110
+ TaggedLogger.new(self, **tags)
111
+ end
112
+
99
113
  private
100
114
 
101
115
  def add(severity, message = nil, progname = nil, **tags, &block)
@@ -136,7 +150,7 @@ module Klogger
136
150
  end
137
151
 
138
152
  def call_destinations(payload, group_ids)
139
- @destinations.each do |destination|
153
+ (@destinations + @block_destinations.value).each do |destination|
140
154
  destination.call(self, payload.dup, group_ids)
141
155
  rescue StandardError => e
142
156
  # If something goes wrong in here, we don't want to break the application
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'klogger/logger'
4
+
5
+ module Klogger
6
+ class TaggedLogger
7
+
8
+ def initialize(parent, **tags)
9
+ @parent = parent
10
+ @tags = tags
11
+ end
12
+
13
+ Klogger::Logger::LEVELS.each do |level|
14
+ define_method(level) do |message = nil, progname = nil, **tags, &block|
15
+ @parent.public_send(level, message, progname, **@tags.merge(tags), &block)
16
+ end
17
+ end
18
+
19
+ def exception(exception, message = nil, **tags)
20
+ @parent.exception(exception, message, **@tags.merge(tags))
21
+ end
22
+
23
+ def group(**tags, &block)
24
+ @parent.group(**@tags.merge(tags), &block)
25
+ end
26
+
27
+ def add_group(**tags)
28
+ @parent.add_group(**@tags.merge(tags))
29
+ end
30
+
31
+ def pop_group
32
+ @parent.pop_group
33
+ end
34
+
35
+ def tagged(**tags, &block)
36
+ @parent.tagged(**@tags.merge(tags), &block)
37
+ end
38
+
39
+ def silence!(&block)
40
+ @parent.silence!(&block)
41
+ end
42
+
43
+ def unsilence!(&block)
44
+ @parent.unsilence!(&block)
45
+ end
46
+
47
+ def silenced?
48
+ @parent.silenced?
49
+ end
50
+
51
+ def create_tagged_logger(**tags)
52
+ @parent.create_tagged_logger(**@tags.merge(tags))
53
+ end
54
+
55
+ end
56
+ end
data/lib/klogger.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'klogger/logger'
4
+ require 'klogger/tagged_logger'
4
5
 
5
6
  module Klogger
6
7
 
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.3.2
4
+ version: 1.5.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-07-25 00:00:00.000000000 Z
11
+ date: 2024-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -84,6 +84,7 @@ files:
84
84
  - lib/klogger/group_set.rb
85
85
  - lib/klogger/json_highlighter.rb
86
86
  - lib/klogger/logger.rb
87
+ - lib/klogger/tagged_logger.rb
87
88
  - lib/klogger/version.rb
88
89
  homepage: https://github.com/krystal/klogger
89
90
  licenses:
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  - !ruby/object:Gem::Version
105
106
  version: '0'
106
107
  requirements: []
107
- rubygems_version: 3.3.26
108
+ rubygems_version: 3.3.27
108
109
  signing_key:
109
110
  specification_version: 4
110
111
  summary: A simple Ruby logger