sonnet 0.1.3 → 0.1.4
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +10 -9
- data/examples/sidekiq.rb +41 -0
- data/lib/sonnet.rb +0 -2
- data/lib/sonnet/formatter.rb +19 -16
- data/lib/sonnet/version.rb +1 -1
- data/sonnet.gemspec +2 -2
- metadata +5 -7
- data/lib/sonnet/serializer.rb +0 -13
- data/lib/sonnet/sidekiq/exception_handler.rb +0 -24
- data/lib/sonnet/sidekiq/job_logger.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 273fb36aa6154de4836aae0cb4f44ccb4b9799652f8dfebbd08a9ee04d2fa669
|
4
|
+
data.tar.gz: d8f9d7f79f4a5b436754c7ee7adab6d39a30440b041e306e5e37861592ba7beb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4ec687ec2c6e93d459a30065d6c4849dff9533cca1dbd6861c9cf18591b490b52e0f250d9e07e4c107d543b83546afb0160f4a23d0ff8ccc2b6663a09c477c5
|
7
|
+
data.tar.gz: 83305c920737815295ddfc792ae7b979247d3b334a848ff726afa1ea2e16ff73974be8d53513bc9376a5c38235cb7de44b025d4aa02c857bcbd42b8ab33af850
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# Sonnet
|
2
2
|
|
3
|
-
|
3
|
+
Structured logging for Ruby applications.
|
4
4
|
|
5
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
|
data/examples/sidekiq.rb
ADDED
@@ -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
|
data/lib/sonnet.rb
CHANGED
data/lib/sonnet/formatter.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
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.
|
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
|
-
|
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
|
42
|
+
def serialize_exception(exception)
|
44
43
|
{
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
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
|
data/lib/sonnet/version.rb
CHANGED
data/sonnet.gemspec
CHANGED
@@ -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
|
13
|
-
spec.description = "Structured
|
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.
|
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-
|
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
|
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
|
117
|
+
summary: Structured logging for Ruby applications
|
120
118
|
test_files: []
|
data/lib/sonnet/serializer.rb
DELETED
@@ -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
|