cogger 2.3.0 → 2.4.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: 051cbeb63381e6680f86fd1b73a1e6648d6ecc1befc37824d0425cbb12d0fb63
4
- data.tar.gz: 8772b28a29157d2f023096eaf40ba28c252888e3430e5ddc673ade49888b7da2
3
+ metadata.gz: d3dd4c51c4410b817ca889b3c06abf51fcab7e6e91f5746caed31d1bf45f3eb7
4
+ data.tar.gz: aa1f55995d710f2147071f4eaf9709e625c1fdd809d255860a3113ea6d742064
5
5
  SHA512:
6
- metadata.gz: 4b290ebea636e3744c68dd07c1cd37bd310fa3c99600a8b7a32ed93dcdcc651559ff718fcfe43446c7bad91772ba25eb4bb60bfe0ff9f2c6f85d8158dc58c9c1
7
- data.tar.gz: 163afc50ba9118ab76f2ff75f351b4d2b60992b29f317bbfab218e310ada42890fec1e5cfed6993bde0421d67126b65038adbe7b81791f062c07ae82e32c3143
6
+ metadata.gz: 61b318f30b68581323296d09159df52721d5a4e7554ada7d7d6ac09416016d26dee20b83630aa708ddc1b02e43966ab0e1e98b3fb08c2243a769a840a7853f04
7
+ data.tar.gz: 4731b4c644c505603098c41b0f0ca5a3aa81b0d81c175dfe37c914cdbb5b06fb57619f4f1f154007dfafb331263262646ae50338db9eff930084b78072697209
checksums.yaml.gz.sig CHANGED
@@ -1 +1,2 @@
1
- ;&S4L���^y ,Ҫ��R�GQ���pQ7lhG��tIe� P�'|��*p_��:��.�#ZV4�9O��s�_����E���6y }g���T���T�`Q�8f+��[
1
+ ���v������I+„"�:o���lW"ݺ�:C[eI����T��aUN�,ɞ�jpCA�"Z�E�� ��[̠x}��9��غȍ���3?�VV"����Y�_ҊBx��^aL��u /O�j6\���п*�I���j�kԲ���/J���.!���q1���ĺ*����kh���O�����dt�������/D, ��
2
+ �`U�� \LaW��O�[�9BJ
data/README.adoc CHANGED
@@ -142,6 +142,34 @@ logger = Cogger.new id: :demo,
142
142
  suffix: "%Y"
143
143
  ----
144
144
 
145
+ === Identity
146
+
147
+ Your logger's identity defaults to `$PROGRAM_NAME` -- as mentioned earlier -- but can be customized during initialization. Example:
148
+
149
+ [source,ruby]
150
+ ----
151
+ logger = Cogger.new id: :demo
152
+ logger.info "A demonstration."
153
+
154
+ # 🟢 [demo] A demonstration.
155
+ ----
156
+
157
+ You can also customize the identity by passing in an ID per log entry. Example:
158
+
159
+ [source,ruby]
160
+ ----
161
+ logger = Cogger.new
162
+
163
+ logger.info "A demonstration.", id: :other
164
+ # 🟢 [other] A demonstration.
165
+
166
+ logger.info(id: :other) { "A demonstration." }
167
+ # 🟢 [other] A demonstration.
168
+
169
+ logger.info { {id: :other, message: "A demonstration."} }
170
+ # 🟢 [other] A demonstration.
171
+ ----
172
+
145
173
  === Levels
146
174
 
147
175
  Supported levels can be obtained via `Cogger::LEVELS`. Example:
@@ -165,7 +193,26 @@ The above adheres to {rfc_3339_link} and can be customized -- as mentioned earli
165
193
 
166
194
  [source,ruby]
167
195
  ----
168
- Cogger.new datetime_format: "%Y-%m-%d"
196
+ logger = Cogger.new formatter: :json, datetime_format: "%Y-%m-%d"
197
+ logger.info "A demonstration."
198
+
199
+ # {"id":"console","level":"INFO","at":"2026-06-28","message":"A demonstration."}
200
+ ----
201
+
202
+ You can also customize the date/time format by passing in an date/time format per log entry. Example:
203
+
204
+ [source,ruby]
205
+ ----
206
+ logger = Cogger.new formatter: :json
207
+
208
+ logger.info "A demonstration.", datetime_format: "%Y-%m-%d"
209
+ # {"id":"console","level":"INFO","at":"2026-06-28","message":"A demonstration."}
210
+
211
+ logger.info(datetime_format: "%Y-%m-%d") { "A demonstration." }
212
+ # {"id":"console","level":"INFO","at":"2026-06-28","message":"A demonstration."}
213
+
214
+ logger.info { {datetime_format: "%Y-%m-%d", message: "A demonstration."} }
215
+ # {"id":"console","level":"INFO","at":"2026-06-28","message":"A demonstration."}
169
216
  ----
170
217
 
171
218
  === Environment
@@ -876,7 +923,7 @@ Tags allow you to tag your messages at both a global and local (i.e. per message
876
923
  logger = Cogger.new tags: %w[WEB]
877
924
  logger.info "Demo"
878
925
 
879
- # 🟢 [console] [WEB] Demo
926
+ # 🟢 [console] ["WEB"] Demo
880
927
  ----
881
928
 
882
929
  You can use multiple tags as well:
@@ -886,7 +933,7 @@ You can use multiple tags as well:
886
933
  logger = Cogger.new tags: %w[WEB EXAMPLE]
887
934
  logger.info "Demo"
888
935
 
889
- # 🟢 [console] [WEB] [EXAMPLE] Demo
936
+ # 🟢 [console] ["WEB"] ["EXAMPLE"] Demo
890
937
  ----
891
938
 
892
939
  You are not limited to string-based tags. Any object will work:
@@ -896,7 +943,7 @@ You are not limited to string-based tags. Any object will work:
896
943
  logger = Cogger.new tags: ["ONE", :two, 3, {four: "FOUR"}, proc { "FIVE" }]
897
944
  logger.info "Demo"
898
945
 
899
- # 🟢 [console] [ONE] [two] [3] [FIVE] [four=FOUR] Demo
946
+ # 🟢 [console] ["ONE"] [:two] [3] ["FIVE"] [four="FOUR"] Demo
900
947
  ----
901
948
 
902
949
  With the above, we have string, symbol, integer, hash, and proc tags. With hashes, you'll always get a the key/value pair formatted as: `key=value`. Procs/lambdas allow you to lazy evaluate your tag at time of logging which provides a powerful way to acquire the current process ID, thread ID, and so forth.
@@ -908,7 +955,7 @@ In addition to global tags, you can use local tags per log message. Example:
908
955
  logger = Cogger.new
909
956
  logger.info "Demo", tags: ["ONE", :two, 3, {four: "FOUR"}, proc { "FIVE" }]
910
957
 
911
- # 🟢 [console] [ONE] [two] [3] [FIVE] [four=FOUR] Demo
958
+ # 🟢 [console] ["ONE"] [:two] [3] ["FIVE"] [four="FOUR"] Demo
912
959
  ----
913
960
 
914
961
  You can also combine global and local tags:
@@ -918,16 +965,33 @@ You can also combine global and local tags:
918
965
  logger = Cogger.new tags: ["ONE", :two]
919
966
  logger.info "Demo", tags: [3, proc { "FOUR" }]
920
967
 
921
- # 🟢 [console] [ONE] [two] [3] [FOUR] Demo
968
+ # 🟢 [console] ["ONE"] [:two] [3] ["FOUR"] Demo
922
969
  ----
923
970
 
971
+ When using blocks, use a hash inside the block instead of supplying the tags before using the block. Example:
972
+
973
+ [source,ruby]
974
+ ----
975
+ logger = Cogger.new level: :debug
976
+
977
+ # No
978
+ logger.debug(tags: ["ONE", :TWO]) { "Demo" }
979
+ 🔎 [console] ["ONE"] [:TWO] Demo
980
+
981
+ # Yes
982
+ logger.debug { {tags: ["ONE", :TWO], message: "Demo"} }
983
+ 🔎 [console] ["ONE"] [:TWO] Demo
984
+ ----
985
+
986
+ This ensures your tags and associated message are only evaluated when the correct log level is accepted. This is especially important when dealing with tags that need additional evaluation like procs because you'll improve performance when the debug log level, in this example, isn't set.
987
+
924
988
  As you can see, tags are highly versatile. That said, the following guidelines are worth consideration when using them:
925
989
 
926
- * Prefer uppercase tag names to make them visually stand out.
927
- * Prefer short names, ideally 1-4 characters since long tags defeat the purpose of brevity.
928
- * Prefer consistent tag names by using tags that are not synonymous or ambiguous.
929
- * Prefer using tags by feature rather than things like environments. Examples: API, DB, MAILER.
930
- * Prefer the JSON formatter for structured metadata instead of tags. Logging JSON formatted messages with tags will work but sticking with a traditional hash, instead of tags, will probably serve you better.
990
+ * Prefer uppercase tags to make them visually stand out.
991
+ * Prefer short tags, ideally 1-4 characters since long tags defeat the purpose of brevity.
992
+ * Prefer consistent tags by using tags that are not synonymous or ambiguous.
993
+ * Prefer feature tags instead of environment tags. Examples: API, DB, MAILER.
994
+ * Prefer the JSON formatter for structured metadata instead of tags. Logging JSON formatted messages with tags will work but sticking with a traditional hash, instead of tags, will serve you better.
931
995
  * Avoid using `id`, `level`, `at`, and/or `message` as hash keys since those are reserved keys and will be rejected if supplied.
932
996
 
933
997
  === Filters
data/cogger.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "cogger"
5
- spec.version = "2.3.0"
5
+ spec.version = "2.4.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://alchemists.io/projects/cogger"
data/lib/cogger/entry.rb CHANGED
@@ -30,7 +30,7 @@ module Cogger
30
30
 
31
31
  def self.sanitize! content, payload
32
32
  body = if content.is_a? Hash
33
- content.delete(:message).tap { payload.merge! content }
33
+ content.delete(:message).tap { payload.merge! content.except(:level, :at) }
34
34
  else
35
35
  content
36
36
  end
@@ -17,7 +17,8 @@ module Cogger
17
17
 
18
18
  def call(*input)
19
19
  *, entry = input
20
- attributes = sanitize(entry, :tagged_attributes).tap(&:compact!)
20
+ attributes = sanitize entry, :tagged_attributes
21
+ attributes.delete :message unless attributes[:message]
21
22
 
22
23
  parser.call(template, attributes).to_json << NEW_LINE
23
24
  end
@@ -16,7 +16,8 @@ module Cogger
16
16
 
17
17
  def call(*input)
18
18
  *, entry = input
19
- attributes = sanitize(entry, :tagged_attributes).tap(&:compact!)
19
+ attributes = sanitize entry, :tagged_attributes
20
+ attributes.delete :message unless attributes[:message]
20
21
 
21
22
  concat(attributes).chop! << NEW_LINE
22
23
  end
@@ -35,7 +35,7 @@ module Cogger
35
35
 
36
36
  attr_reader :pattern
37
37
 
38
- def dump(value) = value.to_s.gsub(pattern, &:dump)
38
+ def dump(value) = value ? value.to_s.gsub(pattern, &:dump) : value.inspect
39
39
  end
40
40
  end
41
41
  end
data/lib/cogger/hub.rb CHANGED
@@ -101,22 +101,23 @@ module Cogger
101
101
  crash message, error
102
102
  end
103
103
 
104
- # rubocop:todo Metrics/MethodLength
105
104
  def dispatch(level, message, **payload, &)
106
- entry = configuration.entry.for(
105
+ entry = build_entry(level, message, payload, &)
106
+ configuration.mutex.synchronize { streams.each { |logger| logger.public_send level, entry } }
107
+ true
108
+ end
109
+
110
+ def build_entry(level, message, payload, &)
111
+ configuration.entry.for(
107
112
  message,
108
113
  id: configuration.id,
109
114
  level:,
110
- tags: configuration.entag(payload.delete(:tags)),
115
+ tags: configuration.entag(payload[:tags]),
111
116
  datetime_format: configuration.datetime_format,
112
- **payload,
117
+ **payload.except(:level, :at, :tags),
113
118
  &
114
119
  )
115
-
116
- configuration.mutex.synchronize { streams.each { |logger| logger.public_send level, entry } }
117
- true
118
120
  end
119
- # rubocop:enable Metrics/MethodLength
120
121
 
121
122
  def crash message, error
122
123
  configuration.with(id: :cogger, io: $stdout, formatter: Formatters::Crash.new)
data/lib/cogger/tag.rb CHANGED
@@ -27,19 +27,23 @@ module Cogger
27
27
 
28
28
  def empty? = singles.empty? && pairs.empty?
29
29
 
30
- def to_h = empty? ? Core::EMPTY_HASH : {tags: singles.to_a, **pairs}.tap(&:compress!)
30
+ def to_h
31
+ return Core::EMPTY_HASH if empty?
32
+
33
+ singles.empty? ? pairs : {tags: singles.to_a, **pairs}
34
+ end
31
35
 
32
36
  def to_s = empty? ? Core::EMPTY_STRING : "#{format_singles} #{format_pairs}".tap(&:strip!)
33
37
 
34
38
  private
35
39
 
36
40
  def format_singles
37
- singles.map { |value| "[#{value}]" }
41
+ singles.map { |value| "[#{value.inspect}]" }
38
42
  .join " "
39
43
  end
40
44
 
41
45
  def format_pairs
42
- pairs.map { |key, value| "[#{key}=#{value}]" }
46
+ pairs.map { |key, value| "[#{key}=#{value.inspect}]" }
43
47
  .join(" ")
44
48
  end
45
49
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  - !ruby/object:Gem::Version
176
176
  version: '0'
177
177
  requirements: []
178
- rubygems_version: 4.0.12
178
+ rubygems_version: 4.0.15
179
179
  specification_version: 4
180
180
  summary: A customizable and feature rich logger.
181
181
  test_files: []
metadata.gz.sig CHANGED
Binary file