alephant-logger-json 0.0.2 → 0.1.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
  SHA1:
3
- metadata.gz: 3968d465458c83d5311c94efbca73fc9d51573cf
4
- data.tar.gz: a132988fc00b3aafc4e504be158512de6e903f5f
3
+ metadata.gz: 84eb3ae131800ea10978a1bef31ae0a51bde66c4
4
+ data.tar.gz: d7eda923aa0c8a7fb348840675f040185b6f295a
5
5
  SHA512:
6
- metadata.gz: 22d2e4a1564a559732dd57b738944b969b3cc73c3341c0fd1c69dc364acfb0bd7e3b9eaff853b7049cba1aa6e136e98a53139eb8538265dd4c237d0b23c32cfd
7
- data.tar.gz: f7485a3235e148c99eeefb6b5116886b6840bb9e9121e15dd2cb876b33428deaaa83fa61645fe34f8a9ce678e2df90340b8d2557a6509d287f32f5eae0c98bb1
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 )
@@ -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
@@ -1,7 +1,7 @@
1
1
  module Alephant
2
2
  module Logger
3
3
  class JSON
4
- VERSION = "0.0.2"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -29,10 +29,52 @@ describe Alephant::Logger::JSON do
29
29
  end
30
30
  end
31
31
 
32
- describe "log levels" do
33
- %w[debug info warn error].each do |level|
34
- it_behaves_like "JSON logging" do
35
- let(:level) { level }
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.2
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-18 00:00:00.000000000 Z
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