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 +4 -4
- data/README.md +1 -1
- data/lib/json_tagged_logger/formatter.rb +19 -1
- data/lib/json_tagged_logger/tag_from_session.rb +8 -4
- data/lib/json_tagged_logger/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c914a43a81cb199406223b8d4ab2cdf00a059139bda4eb75b4d8781fa6cff24
|
4
|
+
data.tar.gz: 3b1a1b1071facc268b4b5267863f8e3167fde5f6df7dd609db09c0124761ec90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
[
|
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
|
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(
|
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
|
-
|
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.
|
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[
|
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
|