klogger-logger 1.3.2 → 1.4.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: ec65c2728e6aee2870704aeb67e2224f3ffe8b94173f0dafa5e54354f9ec4737
4
- data.tar.gz: 51244c5f4ab5fc54d5a9dda4aa14a1051fed37fd718b205c3bd14cf62b7d52ee
3
+ metadata.gz: 07fd4a128b55717b068300e71272284d05eaefa3ed32b28955236679a41fa02e
4
+ data.tar.gz: c09a76bd59fabc13754f4af45c74373c17dfe19efe17fe2d59b99dff965d770c
5
5
  SHA512:
6
- metadata.gz: dab76a4af78b3684b078caf4348712637b952bd4abdc61cf73ba5bc1722feb6754b3dee5eaa4d1a49f5bf22cb86c5a30a5570861822735a64c4c379c56bfe27b
7
- data.tar.gz: 0b4fe70463502a574a6e461aec57559bf26e1141ae24d1b129fb2320058fb1acba31c42979564db4e1058c42f4785034fdb586ea05a5e8b5c944c53a4c48ca20
6
+ metadata.gz: f03fe8c99955e64c347d905e0f9225df67706a3c72cfb596b5bf1949234b139f1e56a0b86d849a6b31d6b2b295dd748c2f39feebb0875a2df4940ef6426faeea
7
+ data.tar.gz: 3f913bd3e2980d04045e1bc001ea1ecb214f9b5d55066f33c5423ae11b5a8180a7a8bbd0bea9c72d781f48dca4a32e7366f4a80bc837ee77c283825b0724e648
data/README.md CHANGED
@@ -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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.2
1
+ 1.4.0
@@ -25,7 +25,11 @@ 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
@@ -39,12 +43,10 @@ module Klogger
39
43
 
40
44
  def exception(exception, message = nil, **tags)
41
45
  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)
46
+ message: message,
47
+ exception: exception.class.name,
48
+ exception_message: exception.message,
49
+ backtrace: exception.backtrace[0, 4].join("\n"), **tags
48
50
  )
49
51
  end
50
52
 
@@ -96,6 +98,10 @@ module Klogger
96
98
  @destinations.delete(destination)
97
99
  end
98
100
 
101
+ def create_tagged_logger(**tags)
102
+ TaggedLogger.new(self, **tags)
103
+ end
104
+
99
105
  private
100
106
 
101
107
  def add(severity, message = nil, progname = nil, **tags, &block)
@@ -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.4.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-03-01 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: