sonnet 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/sonnet/formatter.rb +19 -7
- data/lib/sonnet/logger.rb +7 -0
- data/lib/sonnet/version.rb +1 -1
- data/lib/sonnet.rb +0 -2
- data/test/sonnet_test.rb +23 -9
- data/test/test_helper.rb +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd726535211c5d03c5db6a93f14aae3121802b99a85d60ee955fe8d455c77d51
|
4
|
+
data.tar.gz: ff06b123ee59cd638298cf81e1f3a3394e5c43df362c81f4cca2180cf65b8baf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea0b0ebf4fbbcdc71b65365b47cb259d95b4e8a17c07cb2590e6e502d8d0a71d14dd69a74f54f159da624584a153551362fa03aa892aaf137f6573ddfe6236e3
|
7
|
+
data.tar.gz: 5c6d8c066d1c9daf4c7f611458b905a0018251efb1006a9f4b90d4c8ad995fc99edb64b710df55698a1bc364919e3d6f6eba4323a30bb4194fb976c894dacb02
|
data/Gemfile.lock
CHANGED
data/lib/sonnet/formatter.rb
CHANGED
@@ -10,6 +10,10 @@ module Sonnet
|
|
10
10
|
new(severity, time, progname, data).to_json
|
11
11
|
end
|
12
12
|
|
13
|
+
def self.current_context
|
14
|
+
Thread.current[:sonnet_current_context] ||= []
|
15
|
+
end
|
16
|
+
|
13
17
|
def initialize(severity, time, progname, data)
|
14
18
|
@severity = severity
|
15
19
|
@time = time
|
@@ -36,6 +40,20 @@ module Sonnet
|
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
43
|
+
def application_context
|
44
|
+
{
|
45
|
+
program: program,
|
46
|
+
# hostname: hostname,
|
47
|
+
level: level,
|
48
|
+
timestamp: timestamp,
|
49
|
+
pid: pid
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def context
|
54
|
+
self.class.current_context.inject({}, &:merge)
|
55
|
+
end
|
56
|
+
|
39
57
|
def level
|
40
58
|
@severity&.downcase
|
41
59
|
end
|
@@ -53,13 +71,7 @@ module Sonnet
|
|
53
71
|
end
|
54
72
|
|
55
73
|
def as_json
|
56
|
-
|
57
|
-
program: program,
|
58
|
-
# hostname: hostname,
|
59
|
-
level: level,
|
60
|
-
timestamp: timestamp,
|
61
|
-
pid: pid
|
62
|
-
}.merge(data).compact
|
74
|
+
context.merge(data).merge(application_context).compact
|
63
75
|
end
|
64
76
|
|
65
77
|
def to_json
|
data/lib/sonnet/logger.rb
CHANGED
@@ -10,5 +10,12 @@ module Sonnet
|
|
10
10
|
def self.log_level
|
11
11
|
::Logger.const_get((ENV["LOG_LEVEL"] || "INFO").upcase)
|
12
12
|
end
|
13
|
+
|
14
|
+
def with_context(context = {})
|
15
|
+
formatter.current_context.push(context)
|
16
|
+
yield self
|
17
|
+
ensure
|
18
|
+
formatter.current_context.pop
|
19
|
+
end
|
13
20
|
end
|
14
21
|
end
|
data/lib/sonnet/version.rb
CHANGED
data/lib/sonnet.rb
CHANGED
data/test/sonnet_test.rb
CHANGED
@@ -1,21 +1,35 @@
|
|
1
1
|
require "test_helper"
|
2
|
-
require "logger"
|
3
2
|
|
4
3
|
class SonnetTest < Minitest::Test
|
5
4
|
Error = Class.new(StandardError)
|
6
5
|
|
7
|
-
def
|
8
|
-
|
6
|
+
def test_log_info
|
7
|
+
logger.info("What's the story, morning glory?")
|
8
|
+
assert_log_line log[0], level: "info", message: "What's the story, morning glory?"
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
logger.
|
13
|
-
|
11
|
+
def test_log_debug
|
12
|
+
logger.debug("this should not be logged")
|
13
|
+
assert_nil log[0]
|
14
|
+
logger.level = Logger::DEBUG
|
15
|
+
logger.debug("this should be logged")
|
16
|
+
assert_log_line log[0], level: "debug", message: "this should be logged"
|
14
17
|
end
|
15
18
|
|
16
|
-
def
|
19
|
+
def test_log_exception
|
17
20
|
logger.error(Error.new("something went wrong"))
|
18
|
-
|
21
|
+
assert_log_line log[0], message: "something went wrong"
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_log_with_context
|
25
|
+
logger.with_context(color: "blue") { logger.info("What's the story, morning glory?") }
|
26
|
+
logger.info("definitely maybe")
|
27
|
+
assert_log_line log[0], color: "blue", message: "What's the story, morning glory?"
|
28
|
+
assert_log_line log[1], message: "definitely maybe"
|
29
|
+
end
|
30
|
+
|
31
|
+
def assert_log_line(actual, expected)
|
32
|
+
assert_equal expected, actual.slice(*expected.keys)
|
19
33
|
end
|
20
34
|
|
21
35
|
def logger
|
@@ -27,6 +41,6 @@ class SonnetTest < Minitest::Test
|
|
27
41
|
end
|
28
42
|
|
29
43
|
def log
|
30
|
-
io.string.each_line.map { |line| JSON.parse(line) }
|
44
|
+
io.string.each_line.map { |line| JSON.parse(line, symbolize_names: true) }
|
31
45
|
end
|
32
46
|
end
|
data/test/test_helper.rb
CHANGED