json_tagged_logger 0.3.0 → 0.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 +4 -4
- data/README.md +1 -1
- data/lib/json_tagged_logger/formatter.rb +19 -1
- 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: f290019420ad4691086787da1f7c2982383eaae9812bc2eb1874778ce5d00718
|
4
|
+
data.tar.gz: 9a52e70773163bd3428612d56b1f723bd50550763f84710eda06aec1a02cd9a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 902135acc6779b5d4a5f63313a8f0ced0125d08998d61ebf77064dd4159764cb14bac9353a11f0efc70a02cc99bed628eaea25dbbd95169dc79c3ef5b782d786
|
7
|
+
data.tar.gz: 3ad70d7851f82976ac866d1ad3553670f3122c8476429de4064d71333214cadbebcfb7e9af1d013151c04f83ff5d8038ce52cc7fd88fb453c7bf8529074aae50
|
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
|