sonnet 0.1.3 → 0.1.4

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: cd726535211c5d03c5db6a93f14aae3121802b99a85d60ee955fe8d455c77d51
4
- data.tar.gz: ff06b123ee59cd638298cf81e1f3a3394e5c43df362c81f4cca2180cf65b8baf
3
+ metadata.gz: 273fb36aa6154de4836aae0cb4f44ccb4b9799652f8dfebbd08a9ee04d2fa669
4
+ data.tar.gz: d8f9d7f79f4a5b436754c7ee7adab6d39a30440b041e306e5e37861592ba7beb
5
5
  SHA512:
6
- metadata.gz: ea0b0ebf4fbbcdc71b65365b47cb259d95b4e8a17c07cb2590e6e502d8d0a71d14dd69a74f54f159da624584a153551362fa03aa892aaf137f6573ddfe6236e3
7
- data.tar.gz: 5c6d8c066d1c9daf4c7f611458b905a0018251efb1006a9f4b90d4c8ad995fc99edb64b710df55698a1bc364919e3d6f6eba4323a30bb4194fb976c894dacb02
6
+ metadata.gz: d4ec687ec2c6e93d459a30065d6c4849dff9533cca1dbd6861c9cf18591b490b52e0f250d9e07e4c107d543b83546afb0160f4a23d0ff8ccc2b6663a09c477c5
7
+ data.tar.gz: 83305c920737815295ddfc792ae7b979247d3b334a848ff726afa1ea2e16ff73974be8d53513bc9376a5c38235cb7de44b025d4aa02c857bcbd42b8ab33af850
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sonnet (0.1.3)
4
+ sonnet (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # Sonnet
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sonnet`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Structured logging for Ruby applications.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Alternatives
6
+
7
+ * [Ougai](https://github.com/tilfin/ougai)
8
+ * [Semantic Logger](https://github.com/rocketjob/semantic_logger)
6
9
 
7
10
  ## Installation
8
11
 
@@ -22,13 +25,11 @@ Or install it yourself as:
22
25
 
23
26
  ## Usage
24
27
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+ ```ruby
29
+ require "sonnet"
30
+ logger = Logger.new(STDOUT)
31
+ logger.extend(Sonnet::Logger)
32
+ ```
32
33
 
33
34
  ## Contributing
34
35
 
@@ -0,0 +1,41 @@
1
+ require "sidekiq/job_logger"
2
+
3
+ module SidekiqLogging
4
+ def self.job_context(job_hash)
5
+ {
6
+ worker_class: job_hash["wrapped"] || job_hash["class"],
7
+ jid: job_hash["jid"]
8
+ }
9
+ end
10
+
11
+ class JobLogger < Sidekiq::JobLogger
12
+ def call(job_hash, queue, &block)
13
+ Sidekiq.logger.with_context(SidekiqLogging.job_context(job_hash)) do
14
+ begin
15
+ start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
16
+ Sidekiq.logger.debug(message: "start")
17
+ yield
18
+ Sidekiq.logger.debug(message: "done", duration: elapsed(start))
19
+ rescue
20
+ Sidekiq.logger.debug(message: "failure", duration: elapsed(start))
21
+ raise
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ class ErrorHandler
28
+ def call(exception, context_hash)
29
+ Sidekiq.logger.with_context(SidekiqLogging.job_context(context_hash[:job])) do
30
+ Sidekiq.logger.warn(exception)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Sidekiq.configure_server do |config|
37
+ Sidekiq::Logging.logger = Rails.logger
38
+ config.options[:job_logger] = SidekiqLogging::JobLogger
39
+ config.error_handlers.pop
40
+ config.error_handlers << SidekiqLogging::ErrorHandler.new
41
+ end
@@ -4,7 +4,5 @@ require "sonnet/formatter"
4
4
  require "sonnet/logger"
5
5
  require "sonnet/version"
6
6
 
7
- require "json"
8
-
9
7
  module Sonnet
10
8
  end
@@ -1,11 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "sonnet/serializer"
3
+ require "json"
4
+ require "time"
4
5
 
5
6
  module Sonnet
6
7
  class Formatter
7
- TIMESTAMP_FORMAT = "%FT%T.%LZ"
8
-
9
8
  def self.call(severity, time, progname, data)
10
9
  new(severity, time, progname, data).to_json
11
10
  end
@@ -26,13 +25,13 @@ module Sonnet
26
25
  end
27
26
 
28
27
  def timestamp
29
- (@time || Time.now).utc.strftime(TIMESTAMP_FORMAT)
28
+ (@time || Time.now).utc.iso8601(3)
30
29
  end
31
30
 
32
31
  def data
33
32
  case @data
34
33
  when Exception
35
- Serializer.serialize_exception(@data)
34
+ serialize_exception(@data)
36
35
  when Hash
37
36
  @data
38
37
  else
@@ -40,16 +39,18 @@ module Sonnet
40
39
  end
41
40
  end
42
41
 
43
- def application_context
42
+ def serialize_exception(exception)
44
43
  {
45
- program: program,
46
- # hostname: hostname,
47
- level: level,
48
- timestamp: timestamp,
49
- pid: pid
44
+ kind: exception.class.name,
45
+ message: exception.to_s,
46
+ stack: exception.backtrace&.slice(0, 3)
50
47
  }
51
48
  end
52
49
 
50
+ def serialize_string(string)
51
+ { message: string.to_s }
52
+ end
53
+
53
54
  def context
54
55
  self.class.current_context.inject({}, &:merge)
55
56
  end
@@ -62,16 +63,18 @@ module Sonnet
62
63
  # @hostname || Socket.gethostname.force_encoding('UTF-8')
63
64
  # end
64
65
 
65
- def serialize_string(string)
66
- { message: string.to_s }
67
- end
68
-
69
66
  def pid
70
67
  $$
71
68
  end
72
69
 
73
70
  def as_json
74
- context.merge(data).merge(application_context).compact
71
+ {
72
+ program: program,
73
+ # hostname: hostname,
74
+ level: level,
75
+ timestamp: timestamp,
76
+ pid: pid
77
+ }.merge(data).merge(context).compact
75
78
  end
76
79
 
77
80
  def to_json
@@ -1,3 +1,3 @@
1
1
  module Sonnet
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Cory Kaufman-Schofield"]
10
10
  spec.email = ["cory@corykaufman.com"]
11
11
 
12
- spec.summary = "Structured logs for Ruby applications"
13
- spec.description = "Structured logs for Ruby applications"
12
+ spec.summary = "Structured logging for Ruby applications"
13
+ spec.description = "Structured logging for Ruby applications"
14
14
  spec.homepage = "https://github.com/allspiritseve/sonnet"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sonnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cory Kaufman-Schofield
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-28 00:00:00.000000000 Z
11
+ date: 2019-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Structured logs for Ruby applications
69
+ description: Structured logging for Ruby applications
70
70
  email:
71
71
  - cory@corykaufman.com
72
72
  executables: []
@@ -81,14 +81,12 @@ files:
81
81
  - Rakefile
82
82
  - bin/console
83
83
  - bin/setup
84
+ - examples/sidekiq.rb
84
85
  - lib/sonnet.rb
85
86
  - lib/sonnet/formatter.rb
86
87
  - lib/sonnet/logger.rb
87
88
  - lib/sonnet/monkeypatch.rb
88
89
  - lib/sonnet/rails.rb
89
- - lib/sonnet/serializer.rb
90
- - lib/sonnet/sidekiq/exception_handler.rb
91
- - lib/sonnet/sidekiq/job_logger.rb
92
90
  - lib/sonnet/version.rb
93
91
  - sonnet.gemspec
94
92
  - test/sonnet_test.rb
@@ -116,5 +114,5 @@ requirements: []
116
114
  rubygems_version: 3.0.1
117
115
  signing_key:
118
116
  specification_version: 4
119
- summary: Structured logs for Ruby applications
117
+ summary: Structured logging for Ruby applications
120
118
  test_files: []
@@ -1,13 +0,0 @@
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
@@ -1,24 +0,0 @@
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
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "sidekiq/job_logger"
4
-
5
- module Sonnet
6
- module Sidekiq
7
- class JobLogger < ::Sidekiq::JobLogger
8
- def call(item, queue)
9
- ::Sidekiq::Logging.with_context(source: item['class']) do
10
- begin
11
- start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
12
- logger.debug(message: "start")
13
- yield
14
- logger.debug(message: "done", duration: elapsed(start))
15
- rescue
16
- logger.debug(message: "failure", duration: elapsed(start))
17
- raise
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end