alephant-logger-json 0.3.0 → 0.3.1

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: 771869cbd5bcad995127e6ef602ec4ae3c1c6177
4
- data.tar.gz: 4cd2f63c8aba00076408f9f42ecd33c6dc1a3b48
3
+ metadata.gz: fb999ad1fdc190e0cf59150933ec5f1e8e4403da
4
+ data.tar.gz: b48484a0c9c855c2e5f95d79caf8b0173085051b
5
5
  SHA512:
6
- metadata.gz: bb3f8e8dfaf453a946aff808903601b49881d199740b248db192bf4837a05b8fc47be1e290983baf7a01b91e896f2135b75416ccfd08c5c1cf0182cdc44957f0
7
- data.tar.gz: 4c5853cde0064536adb6c03c080acd884470e2b9885c81ec293efd6939453d5ba04fcc58236afa149f2c9f726f43485caadd96f6f286df2abe5dda8f833afba5
6
+ metadata.gz: 8ddda013e92f5508ec2fdd7ce30caee46ebd01a468a5f14eccecf43fcf69b4398413e26570c5f09a5469f02b4394f7063def31e9f6b32dfe8091ed26b3f12997
7
+ data.tar.gz: 808cd3d07ceca926a115624d4eab3653b76cda13510c7c632b795b2bd917bd469d2f53677a2a102a20f7e90a89b5a5dd7bce02aa54d648b9966070a50187e253
data/README.md CHANGED
@@ -30,13 +30,39 @@ 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:
33
+ ### Nesting
34
+
35
+ By default, nested JSON values are flattened to strings. To enable nesting, provided that your log analysis tooling supports that, create `Alephant::Logger::JSON` as follows:
36
+
36
37
  ```
37
38
  Alephant::Logger::JSON.new("path/to/logfile.log", :nesting => true)
38
39
  ```
39
40
 
41
+ ### Distributed Tracing
42
+
43
+ The logger will set a key of `uuid` to `n/a` by default for each log request.
44
+
45
+ This value can be changed by providing a lambda function that contains the logic to determine this value.
46
+
47
+ There are two methods available to help you:
48
+
49
+ - `Alephant::Logger::JSON.session?`: boolean response checking if `@@session` has been set
50
+ - `Alephant::Logger::JSON.session`: accepts a lambda function (its return value is internally assigned to `@@session`)
51
+
52
+ When using tracing, you'll need to provide a binding context as the first argument to your log level method calls.
53
+
54
+ This is to resolve issues with lambda's scope availability. See `Kernal#binding` for more details.
55
+
56
+ Example usage:
57
+
58
+ ```
59
+ logger.info(binding, :foo => :bar)
60
+ ```
61
+
62
+ If no `binding` is provided then tracing is ignored and the logger falls back to its default value.
63
+
64
+ > Note: you can hide the binding necessity behind an abstraction layer if you prefer
65
+
40
66
  ## Contributing
41
67
 
42
68
  1. Fork it ( https://github.com/BBC-News/alephant-logger-json/fork )
@@ -0,0 +1,34 @@
1
+ module DynamicBinding
2
+ class LookupStack
3
+ def initialize(bindings = [])
4
+ @bindings = bindings
5
+ end
6
+
7
+ def method_missing(m, *args)
8
+ @bindings.reverse_each do |bind|
9
+ begin
10
+ method = eval("method(%s)" % m.inspect, bind)
11
+ rescue NameError
12
+ else
13
+ return method.call(*args)
14
+ end
15
+ begin
16
+ value = eval(m.to_s, bind)
17
+ return value
18
+ rescue NameError
19
+ end
20
+ end
21
+ raise NoMethodError, "No such variable or method: %s" % m
22
+ end
23
+
24
+ def run_proc(p, *args)
25
+ instance_exec(*args, &p)
26
+ end
27
+ end
28
+ end
29
+
30
+ class Proc
31
+ def call_with_binding(bind, *args)
32
+ DynamicBinding::LookupStack.new([bind]).run_proc(self, *args)
33
+ end
34
+ end
@@ -1,7 +1,7 @@
1
1
  module Alephant
2
2
  module Logger
3
3
  class JSON
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
6
6
  end
7
7
  end
@@ -1,3 +1,4 @@
1
+ require_relative "./dynamic_binding.rb"
1
2
  require "json"
2
3
 
3
4
  module Alephant
@@ -10,12 +11,12 @@ module Alephant
10
11
  end
11
12
 
12
13
  [:debug, :info, :warn, :error].each do |level|
13
- define_method(level) do |hash|
14
+ define_method(level) do |b=nil, hash|
14
15
  return if hash.is_a? String
15
16
  @@session = -> { "n/a" } unless defined? @@session
16
17
  h = {
17
18
  :timestamp => Time.now.to_s,
18
- :uuid => @@session.(),
19
+ :uuid => b.nil? ? "n/a" : @@session.call_with_binding(b),
19
20
  :level => level.to_s
20
21
  }.merge hash
21
22
  hash = flatten_values_to_s h unless @nesting
@@ -66,7 +66,7 @@ describe Alephant::Logger::JSON do
66
66
  end
67
67
 
68
68
  ::Alephant::Logger::JSON.session fn
69
- subject.send(level, log_hash)
69
+ subject.send(level, binding, log_hash)
70
70
  ::Alephant::Logger::JSON.session -> { "n/a" }
71
71
  end
72
72
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-logger-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Arnould
@@ -108,6 +108,7 @@ files:
108
108
  - README.md
109
109
  - Rakefile
110
110
  - alephant-logger-json.gemspec
111
+ - lib/alephant/logger/dynamic_binding.rb
111
112
  - lib/alephant/logger/json.rb
112
113
  - lib/alephant/logger/json/version.rb
113
114
  - spec/alephant/logger/json_spec.rb