sonnet 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: 7d7c9190a2c3b30088666a05896be4b39951cc028d9231cf9b11de55531c03bb
4
- data.tar.gz: cf736f790ca819802faece8bd735c6a7e0d7aac4feaefce821e3c528c367f08e
3
+ metadata.gz: 37fe52c625a9e9652eb886216565a9ea485f3f309650881d3576690e979bd1ab
4
+ data.tar.gz: d50175a894ac543914f9547bf9ddd6b601863a6124fd8fec0c6545f5619634b7
5
5
  SHA512:
6
- metadata.gz: effec6afdf604f2dc9ed8dac4e18a67d45f44df8760e415600a555f7052fa7ef606674818e4a858ce5d51bd23ad7a9591348f45029836b570758726a672101f5
7
- data.tar.gz: a667b7bdf7a12bfc8b1447cc32a688e8d62aedbda9494be3288e43ab0a130fc133a8e60ea891746369a3eef6acdfa9f498ecdbd9e4dae0f164c82d4c8b7c4c49
6
+ metadata.gz: 38d76037845898e166536ad70e5fc0fb9c6544462c5fe04a5c3bcadf1b48a2915b4a639ad543dbea16b9c8857b3c6c72bcf40d202f25be4c2194dacf77d99f77
7
+ data.tar.gz: 9378241dcd507f983cbe7ab2b5954780c3d2aafd676cde6aed02c3795e03af44ffc4ecd11110ae48571c2c7754c7b6b9a601c0799a8c7bc166467d13a5e791fd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sonnet (0.1.0)
4
+ sonnet (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "sonnet/serializer"
4
+
3
5
  module Sonnet
4
6
  class Formatter
5
7
  TIMESTAMP_FORMAT = "%FT%T.%LZ"
@@ -26,7 +28,7 @@ module Sonnet
26
28
  def data
27
29
  case @data
28
30
  when Exception
29
- serialize_exception(@data)
31
+ Serializer.serialize_exception(@data)
30
32
  when Hash
31
33
  @data
32
34
  else
@@ -42,14 +44,6 @@ module Sonnet
42
44
  # @hostname || Socket.gethostname.force_encoding('UTF-8')
43
45
  # end
44
46
 
45
- def serialize_exception(exception)
46
- {
47
- kind: exception.class.name,
48
- message: exception.to_s,
49
- stack: exception.backtrace&.slice(0, 3)
50
- }
51
- end
52
-
53
47
  def serialize_string(string)
54
48
  { message: string.to_s }
55
49
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sonnet
4
+ module Serializer
5
+ def self.serialize_exception(exception)
6
+ {
7
+ kind: exception.class.name,
8
+ message: exception.to_s,
9
+ stack: exception.backtrace&.slice(0, 3)
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq/exception_handler"
4
+ require "sonnet/serializer"
5
+
6
+ module Sonnet
7
+ module Sidekiq
8
+ module ExceptionHandler
9
+ def self.extended(base)
10
+ ::Sidekiq::ExceptionHandler::Logger.prepend(Logger)
11
+ end
12
+
13
+ module Logger
14
+ def call(ex, ctxHash)
15
+ ::Sidekiq.logger.warn(
16
+ exception: ex,
17
+ message: ctxHash["context"],
18
+ job: ctxHash[:job]
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -2,18 +2,18 @@
2
2
 
3
3
  require "sidekiq/job_logger"
4
4
 
5
- module Album
5
+ module Sonnet
6
6
  module Sidekiq
7
7
  class JobLogger < ::Sidekiq::JobLogger
8
8
  def call(item, queue)
9
9
  ::Sidekiq::Logging.with_context(source: item['class']) do
10
10
  begin
11
11
  start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
12
- logger.debug(:start)
12
+ logger.debug(message: "start")
13
13
  yield
14
- logger.debug(:done, duration: elapsed(start))
14
+ logger.debug(message: "done", duration: elapsed(start))
15
15
  rescue
16
- logger.debug(:failure, duration: elapsed(start))
16
+ logger.debug(message: "failure", duration: elapsed(start))
17
17
  raise
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module Sonnet
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/test/sonnet_test.rb CHANGED
@@ -2,16 +2,31 @@ require "test_helper"
2
2
  require "logger"
3
3
 
4
4
  class SonnetTest < Minitest::Test
5
+ Error = Class.new(StandardError)
6
+
5
7
  def test_that_it_has_a_version_number
6
8
  refute_nil ::Sonnet::VERSION
7
9
  end
8
10
 
9
11
  def test_json_logging
10
- io = StringIO.new
11
- logger = Logger.new(io)
12
- logger.extend(Sonnet::Logger)
13
12
  logger.info("What's the story, morning glory?")
14
- line = JSON.parse(io.string)
15
- assert_equal line.fetch("message"), "What's the story, morning glory?"
13
+ assert_equal log[0].fetch("message"), "What's the story, morning glory?"
14
+ end
15
+
16
+ def test_exception_logging
17
+ logger.error(Error.new("something went wrong"))
18
+ assert_equal log[0]["message"], "something went wrong"
19
+ end
20
+
21
+ def logger
22
+ @logger ||= Logger.new(io).tap { |logger| logger.extend(Sonnet::Logger) }
23
+ end
24
+
25
+ def io
26
+ @io ||= StringIO.new
27
+ end
28
+
29
+ def log
30
+ io.string.each_line.map { |line| JSON.parse(line) }
16
31
  end
17
32
  end
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.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cory Kaufman-Schofield
@@ -86,6 +86,8 @@ files:
86
86
  - lib/sonnet/logger.rb
87
87
  - lib/sonnet/monkeypatch.rb
88
88
  - lib/sonnet/rails.rb
89
+ - lib/sonnet/serializer.rb
90
+ - lib/sonnet/sidekiq/exception_handler.rb
89
91
  - lib/sonnet/sidekiq/job_logger.rb
90
92
  - lib/sonnet/version.rb
91
93
  - sonnet.gemspec