alephant-logger-json 0.0.2 → 0.1.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 +7 -0
- data/lib/alephant/logger/json.rb +13 -1
- data/lib/alephant/logger/json/version.rb +1 -1
- data/spec/alephant/logger/json_spec.rb +46 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84eb3ae131800ea10978a1bef31ae0a51bde66c4
|
4
|
+
data.tar.gz: d7eda923aa0c8a7fb348840675f040185b6f295a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1941155f17fc7b8d0795a98550ed9ce743eaea9c84799cd78c00d8588b747a3ed241247c070390469be85148c276df0e6936391e18d91343287287a6c6174eac
|
7
|
+
data.tar.gz: 16962fa4fc1344968f6ed9546d921e38effe01a6fb375bdbdfa17ed40ffd13c4a406fa4075d66c7cf4ddffbe965f2a9346537d79d583f3c919d066f66beb9637
|
data/README.md
CHANGED
@@ -30,6 +30,13 @@ logger = Alephant::Logger.setup json_driver
|
|
30
30
|
logger.info({ "some_field" => "some_value", "other_field" => "other_value" })
|
31
31
|
```
|
32
32
|
|
33
|
+
By default, nested JSON values are flattened to strings. To enable nesting,
|
34
|
+
provided that your log analysis tooling supports that, create
|
35
|
+
`Alephant::Logger::JSON` as follows:
|
36
|
+
```
|
37
|
+
Alephant::Logger::JSON.new("path/to/logfile.log", :nesting => true)
|
38
|
+
```
|
39
|
+
|
33
40
|
## Contributing
|
34
41
|
|
35
42
|
1. Fork it ( https://github.com/BBC-News/alephant-logger-json/fork )
|
data/lib/alephant/logger/json.rb
CHANGED
@@ -3,14 +3,26 @@ require "json"
|
|
3
3
|
module Alephant
|
4
4
|
module Logger
|
5
5
|
class JSON
|
6
|
-
def initialize(log_path)
|
6
|
+
def initialize(log_path, options = {})
|
7
7
|
@log_file = File.open(log_path, "a+")
|
8
8
|
@log_file.sync = true
|
9
|
+
@nesting = options.fetch(:nesting, false)
|
9
10
|
end
|
10
11
|
|
12
|
+
private
|
13
|
+
|
14
|
+
def flatten_values_to_s(hash)
|
15
|
+
Hash[hash.map { |k, v| [k, v.to_s] }]
|
16
|
+
end
|
17
|
+
|
18
|
+
public
|
19
|
+
|
11
20
|
[:debug, :info, :warn, :error].each do |level|
|
12
21
|
define_method(level) do |hash|
|
13
22
|
hash["level"] = level.to_s
|
23
|
+
|
24
|
+
hash = flatten_values_to_s hash if not @nesting
|
25
|
+
|
14
26
|
@log_file.write(::JSON.generate(hash) + "\n")
|
15
27
|
end
|
16
28
|
end
|
@@ -29,10 +29,52 @@ describe Alephant::Logger::JSON do
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
shared_context "nested log hash" do
|
33
|
+
let(:log_hash) do
|
34
|
+
{ "nest" => nest }
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:nest) { { "bird" => "eggs" } }
|
38
|
+
end
|
39
|
+
|
40
|
+
shared_examples "nests flattened to strings" do
|
41
|
+
include_context "nested log hash"
|
42
|
+
|
43
|
+
specify do
|
44
|
+
expect(log_file).to receive(:write) do |json_dump|
|
45
|
+
expect(JSON.parse(json_dump)["nest"]).to eq nest.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
subject.send(level, log_hash)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
shared_examples "nesting allowed" do
|
53
|
+
include_context "nested log hash"
|
54
|
+
|
55
|
+
specify do
|
56
|
+
expect(log_file).to receive(:write) do |json_dump|
|
57
|
+
expect(JSON.parse json_dump).to eq log_hash
|
58
|
+
end
|
59
|
+
|
60
|
+
subject.send(level, log_hash)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
%w[debug info warn error].each do |level|
|
65
|
+
describe "##{level}" do
|
66
|
+
let(:level) { level }
|
67
|
+
|
68
|
+
it_behaves_like "JSON logging"
|
69
|
+
|
70
|
+
it_behaves_like "nests flattened to strings"
|
71
|
+
|
72
|
+
context "with nesting allowed" do
|
73
|
+
subject do
|
74
|
+
described_class.new(log_path, :nesting => true)
|
75
|
+
end
|
76
|
+
|
77
|
+
it_behaves_like "nesting allowed"
|
36
78
|
end
|
37
79
|
end
|
38
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alephant-logger-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Arnould
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|