lumber 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +5 -0
- data/lib/lumber/json_formatter.rb +12 -2
- data/lib/lumber/version.rb +1 -1
- data/spec/json_formatter_spec.rb +39 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6695093aa77737d03320b495d979a1cbb8bd88f
|
4
|
+
data.tar.gz: 22f4e7ea92bb1582ddbfae50c2e308250e59eb20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45c8f777f62c0bf92c1eb8758815e0bf99b5e1d50d71e0fda8f431a36fc5e846085e56c90acc2832a43fceddc2ca409dc3d7fe0d2b9926e2e56b6e40e2b0e139
|
7
|
+
data.tar.gz: 91fe68609b03ae3a48f2cfe4b9269361fef12bb7f20a64d5cf9aee37092de66458c14b69d89e17811d0324dc481d916fbdb07662fd683a572f9eedba5c644b2b
|
data/CHANGELOG
CHANGED
@@ -6,13 +6,23 @@ module Lumber
|
|
6
6
|
class JsonFormatter < Log4r::BasicFormatter
|
7
7
|
|
8
8
|
def initialize(hash={})
|
9
|
-
@key_mapping = {}
|
10
9
|
mapping = hash['key_mapping'] || {}
|
11
10
|
@key_mapping = Hash[mapping.collect {|k, v| [k.to_s, v.to_s.split('.')]}]
|
11
|
+
|
12
|
+
@fields = {}
|
13
|
+
fields = hash['fields'] || {}
|
14
|
+
fields.each do |k, v|
|
15
|
+
if v.kind_of? String
|
16
|
+
@fields[k.to_s] = v.to_s.gsub(/#\{(.+)\}/) {|m| eval($1) }
|
17
|
+
else
|
18
|
+
@fields[k.to_s] = v
|
19
|
+
end
|
20
|
+
end
|
12
21
|
end
|
13
22
|
|
14
23
|
def format(logevent)
|
15
|
-
data =
|
24
|
+
data = @fields.dup
|
25
|
+
|
16
26
|
assign_mapped_key(data, :timestamp, Time.now.to_s)
|
17
27
|
assign_mapped_key(data, :logger, logevent.fullname)
|
18
28
|
assign_mapped_key(data, :level, Log4r::LNAMES[logevent.level].downcase)
|
data/lib/lumber/version.rb
CHANGED
data/spec/json_formatter_spec.rb
CHANGED
@@ -27,7 +27,7 @@ describe Lumber::JsonFormatter do
|
|
27
27
|
outputter.formatter.should be_a_kind_of Lumber::JsonFormatter
|
28
28
|
end
|
29
29
|
|
30
|
-
it "can receive configuration" do
|
30
|
+
it "can receive key_mapping configuration" do
|
31
31
|
yml = <<-EOF
|
32
32
|
log4r_config:
|
33
33
|
pre_config:
|
@@ -54,6 +54,34 @@ describe Lumber::JsonFormatter do
|
|
54
54
|
outputter.formatter.instance_variable_get(:@key_mapping).should eq({'level' => ['severity'], 'backtrace' => ['exception', 'bt']})
|
55
55
|
end
|
56
56
|
|
57
|
+
it "can receive fields configuration" do
|
58
|
+
yml = <<-'EOF'
|
59
|
+
log4r_config:
|
60
|
+
pre_config:
|
61
|
+
root:
|
62
|
+
level: 'DEBUG'
|
63
|
+
loggers:
|
64
|
+
- name: "mylogger"
|
65
|
+
|
66
|
+
outputters:
|
67
|
+
- type: StdoutOutputter
|
68
|
+
name: stdout
|
69
|
+
formatter:
|
70
|
+
type: JsonFormatter
|
71
|
+
fields:
|
72
|
+
version: 1
|
73
|
+
dynamic: "#{1+1}"
|
74
|
+
EOF
|
75
|
+
yml.should include('#{1+1}')
|
76
|
+
cfg = Log4r::YamlConfigurator
|
77
|
+
cfg['hostname'] = 'foo'
|
78
|
+
cfg.load_yaml_string(yml)
|
79
|
+
outputter = Log4r::Outputter['stdout']
|
80
|
+
outputter.formatter.should_not be_nil
|
81
|
+
outputter.formatter.should be_a_kind_of Lumber::JsonFormatter
|
82
|
+
outputter.formatter.instance_variable_get(:@fields).should eq({'version' => 1, 'dynamic' => '2'})
|
83
|
+
end
|
84
|
+
|
57
85
|
end
|
58
86
|
|
59
87
|
context "#assign_mapped_key" do
|
@@ -129,6 +157,16 @@ describe Lumber::JsonFormatter do
|
|
129
157
|
json['severity'].should == 'info'
|
130
158
|
end
|
131
159
|
|
160
|
+
it "logs as json with fields" do
|
161
|
+
@formatter = Lumber::JsonFormatter.new('fields' => {'version' => 1})
|
162
|
+
@outputter.formatter = @formatter
|
163
|
+
|
164
|
+
@logger.info("howdy")
|
165
|
+
json = JSON.parse(@sio.string)
|
166
|
+
json['message'].should == 'howdy'
|
167
|
+
json['version'].should == 1
|
168
|
+
end
|
169
|
+
|
132
170
|
it "logs exception as json" do
|
133
171
|
ex = StandardError.new("mybad")
|
134
172
|
raise ex rescue nil
|