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 +4 -4
- data/README.md +29 -3
- data/lib/alephant/logger/dynamic_binding.rb +34 -0
- data/lib/alephant/logger/json/version.rb +1 -1
- data/lib/alephant/logger/json.rb +3 -2
- data/spec/alephant/logger/json_spec.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb999ad1fdc190e0cf59150933ec5f1e8e4403da
|
4
|
+
data.tar.gz: b48484a0c9c855c2e5f95d79caf8b0173085051b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
34
|
-
|
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
|
data/lib/alephant/logger/json.rb
CHANGED
@@ -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
|
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.
|
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
|