sonnet 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37fe52c625a9e9652eb886216565a9ea485f3f309650881d3576690e979bd1ab
4
- data.tar.gz: d50175a894ac543914f9547bf9ddd6b601863a6124fd8fec0c6545f5619634b7
3
+ metadata.gz: cd726535211c5d03c5db6a93f14aae3121802b99a85d60ee955fe8d455c77d51
4
+ data.tar.gz: ff06b123ee59cd638298cf81e1f3a3394e5c43df362c81f4cca2180cf65b8baf
5
5
  SHA512:
6
- metadata.gz: 38d76037845898e166536ad70e5fc0fb9c6544462c5fe04a5c3bcadf1b48a2915b4a639ad543dbea16b9c8857b3c6c72bcf40d202f25be4c2194dacf77d99f77
7
- data.tar.gz: 9378241dcd507f983cbe7ab2b5954780c3d2aafd676cde6aed02c3795e03af44ffc4ecd11110ae48571c2c7754c7b6b9a601c0799a8c7bc166467d13a5e791fd
6
+ metadata.gz: ea0b0ebf4fbbcdc71b65365b47cb259d95b4e8a17c07cb2590e6e502d8d0a71d14dd69a74f54f159da624584a153551362fa03aa892aaf137f6573ddfe6236e3
7
+ data.tar.gz: 5c6d8c066d1c9daf4c7f611458b905a0018251efb1006a9f4b90d4c8ad995fc99edb64b710df55698a1bc364919e3d6f6eba4323a30bb4194fb976c894dacb02
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sonnet (0.1.2)
4
+ sonnet (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Sonnet
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/sonnet.rb CHANGED
@@ -8,5 +8,3 @@ require "json"
8
8
 
9
9
  module Sonnet
10
10
  end
11
-
12
- require "sonnet/rails" if defined?(::Rails::Engine)
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 test_that_it_has_a_version_number
8
- refute_nil ::Sonnet::VERSION
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 test_json_logging
12
- logger.info("What's the story, morning glory?")
13
- assert_equal log[0].fetch("message"), "What's the story, morning glory?"
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 test_exception_logging
19
+ def test_log_exception
17
20
  logger.error(Error.new("something went wrong"))
18
- assert_equal log[0]["message"], "something went wrong"
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
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
2
  require "minitest/autorun"
3
+ require "byebug"
4
+ require "logger"
3
5
 
4
6
  require "sonnet"
5
- require "byebug"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sonnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cory Kaufman-Schofield