json_tagged_logger 0.3.0 → 0.5.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: fde9c968aab725f59b4d0002b2e318b286edb8e8b7790222d1002e51ce631e6e
4
- data.tar.gz: 97e23c6b02ec7faa8869659c59ef8ba1f2bc200cd607b4e248d2e8a7e74cab5f
3
+ metadata.gz: 3c914a43a81cb199406223b8d4ab2cdf00a059139bda4eb75b4d8781fa6cff24
4
+ data.tar.gz: 3b1a1b1071facc268b4b5267863f8e3167fde5f6df7dd609db09c0124761ec90
5
5
  SHA512:
6
- metadata.gz: 2c21e9f1e126a80a8160587f04d30ca7ccd83433471e78c017320734d570f3cb43935e78bf8bd0a858d114c98fd2fc2115b45363f9e0e283d4c3d6ba182dd94e
7
- data.tar.gz: be70dcda430a3341613c09c85eebcc66f98c9361658192808ab6fb9b0bd63b498713e8ff907fad6c6ce116dead29c302acb11c8bb51bf91edd5e8a6b3df880ab
6
+ metadata.gz: 5b5016dc1565e4d02cc98187fd7a59c70fba7125770f01aa638bf23fc51f7d0a07d3677465296f0e4bd1d599fa3c102b6c55d2ae7ebfeaf9cb939836c0429dc4
7
+ data.tar.gz: 5a4bfce86d924b53577fc6be3111b1bcd76466a0c4a38f5aa9979f31b56a09c1509911b243294bcba25b34b8eb4f416e239c5ad8cbf03f4091538789d9645419
data/README.md CHANGED
@@ -62,7 +62,7 @@ will get you something like
62
62
  }
63
63
  ```
64
64
 
65
- [Note: I've pretty-printed the output in these examples for easier reading. The actual log output will be on a single line without extra whitespace.]
65
+ [_Note_: By default, `JsonTaggedLogger::Formatter` outputs logs as single lines without extra whitespace. Setting `JsonTaggedLogger::Formatter#pretty_print` to `true` will pretty print the logs, as I've done in these examples.]
66
66
 
67
67
  Importantly, if the controller action (or any code it calls along the way) has an explicit call to `Rails.logger.tagged("TAG").info("tagged log message")`, you'll get the same key/value tags (`request_id`, `host`, `my_param`, &c.) in the JSON document along with a `tags` key:
68
68
 
@@ -3,6 +3,12 @@ require 'json'
3
3
 
4
4
  module JsonTaggedLogger
5
5
  class Formatter
6
+ attr_accessor :pretty_print
7
+
8
+ def initialize(pretty_print: false)
9
+ @pretty_pretty = pretty_print
10
+ end
11
+
6
12
  def call(severity, _time, _progname, message)
7
13
  log = {
8
14
  level: severity,
@@ -36,7 +42,7 @@ module JsonTaggedLogger
36
42
  end
37
43
  end
38
44
 
39
- log.compact.to_json + "\n"
45
+ format_for_output(log)
40
46
  end
41
47
 
42
48
  private
@@ -69,5 +75,17 @@ module JsonTaggedLogger
69
75
  message
70
76
  end
71
77
  end
78
+
79
+ def format_for_output(log_hash)
80
+ compacted_log = log_hash.compact
81
+
82
+ output_json = if pretty_print
83
+ JSON.pretty_generate(compacted_log)
84
+ else
85
+ JSON.generate(compacted_log)
86
+ end
87
+
88
+ output_json + "\n"
89
+ end
72
90
  end
73
91
  end
@@ -2,20 +2,24 @@ require 'action_dispatch'
2
2
 
3
3
  module JsonTaggedLogger
4
4
  module TagFromSession
5
- def self.get(log_label, session_key = log_label)
5
+ def self.get(*simple_tags, **labeled_tags)
6
+ labels = simple_tags + labeled_tags.keys
7
+ session_keys = simple_tags + labeled_tags.values
8
+
6
9
  lambda do |request|
7
- { log_label => get_value_from_session(request, session_key) }.to_json
10
+ values = get_values_from_session(request, session_keys)
11
+ labels.zip(values).to_h.to_json
8
12
  end
9
13
  end
10
14
 
11
15
  private
12
16
 
13
- def self.get_value_from_session(request, key)
17
+ def self.get_values_from_session(request, keys)
14
18
  session_options = Rails.application.config.session_options
15
19
  session_store = Rails.application.config.session_store.new(Rails.application, session_options)
16
20
  session = ActionDispatch::Request::Session.create(session_store, request, session_options)
17
21
 
18
- session[key]
22
+ keys.map { |k| session[k] }
19
23
  ensure
20
24
  # Clean up side effects from loading the session so it can be loaded as
21
25
  # usual during the normal request cycle. Leaving these headers in place
@@ -1,3 +1,3 @@
1
1
  module JsonTaggedLogger
2
- VERSION = "0.3.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_tagged_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Santry