sonnet 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Gemfile.lock +10 -1
- data/Rakefile +3 -1
- data/bin/console +1 -0
- data/examples/sidekiq.rb +3 -1
- data/lib/sonnet/formatter.rb +12 -7
- data/lib/sonnet/logger.rb +4 -4
- data/lib/sonnet/rails.rb +30 -2
- data/lib/sonnet/{monkeypatch.rb → rails_monkeypatch.rb} +2 -1
- data/lib/sonnet/version.rb +3 -1
- data/sonnet.gemspec +2 -1
- data/test/logger_test.rb +34 -0
- data/test/sonnet/formatter_test.rb +35 -0
- data/test/sonnet_test.rb +2 -0
- data/test/test_helper.rb +35 -4
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3961783bc4eb8f519c30df1a232e34031827335f1e4804f5c5d444ef15bfb57c
|
4
|
+
data.tar.gz: 2fccb0e4851778f673a1ff10de4a6ed771a919c7692612d0320cfabf3728d849
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 133fb98a3a340477a2076b35d7256aaeb52f6b43e62c1f1b7703d0b4b16a599dabd7defbd7127f3d7ad1463a4de175946db9ca4d05735e63616be0a5b6749c4c
|
7
|
+
data.tar.gz: bde201a3bddec39c6179d0b371aabfae9d2a9039f1975bb155ad68db0ccb4bebeb5c416f3ab5c933334a03b3196edde874360699823c4afa6f3c0c2d5cc14463
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,14 +1,22 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sonnet (0.1.
|
4
|
+
sonnet (0.1.6)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
builder (3.2.3)
|
9
11
|
byebug (11.0.1)
|
10
12
|
minitest (5.11.3)
|
13
|
+
minitest-reporters (1.3.6)
|
14
|
+
ansi
|
15
|
+
builder
|
16
|
+
minitest (>= 5.0)
|
17
|
+
ruby-progressbar
|
11
18
|
rake (12.3.2)
|
19
|
+
ruby-progressbar (1.10.0)
|
12
20
|
|
13
21
|
PLATFORMS
|
14
22
|
ruby
|
@@ -17,6 +25,7 @@ DEPENDENCIES
|
|
17
25
|
bundler
|
18
26
|
byebug
|
19
27
|
minitest
|
28
|
+
minitest-reporters
|
20
29
|
rake
|
21
30
|
sonnet!
|
22
31
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/examples/sidekiq.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "sidekiq/job_logger"
|
2
4
|
|
3
5
|
module SidekiqLogging
|
@@ -9,7 +11,7 @@ module SidekiqLogging
|
|
9
11
|
end
|
10
12
|
|
11
13
|
class JobLogger < Sidekiq::JobLogger
|
12
|
-
def call(job_hash,
|
14
|
+
def call(job_hash, _queue, &_block)
|
13
15
|
Sidekiq.logger.with_context(SidekiqLogging.job_context(job_hash)) do
|
14
16
|
begin
|
15
17
|
start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
data/lib/sonnet/formatter.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'json'
|
4
|
+
require 'time'
|
5
5
|
|
6
6
|
module Sonnet
|
7
7
|
class Formatter
|
@@ -18,10 +18,11 @@ module Sonnet
|
|
18
18
|
@time = time
|
19
19
|
@progname = progname
|
20
20
|
@data = data
|
21
|
+
@tags = []
|
21
22
|
end
|
22
23
|
|
23
24
|
def program
|
24
|
-
@progname || File.basename($0)
|
25
|
+
@progname || File.basename($0, '.rb').split(' ')[0]
|
25
26
|
end
|
26
27
|
|
27
28
|
def timestamp
|
@@ -52,7 +53,11 @@ module Sonnet
|
|
52
53
|
end
|
53
54
|
|
54
55
|
def context
|
55
|
-
self.class.current_context.inject({},
|
56
|
+
self.class.current_context.inject({}) do |memo, context|
|
57
|
+
tags = memo.fetch(:tags, []) + [*context.delete(:tags)].compact
|
58
|
+
tag_context = tags.empty? ? {} : { tags: tags }
|
59
|
+
memo.merge(context).merge(tag_context)
|
60
|
+
end
|
56
61
|
end
|
57
62
|
|
58
63
|
def level
|
@@ -74,11 +79,11 @@ module Sonnet
|
|
74
79
|
level: level,
|
75
80
|
timestamp: timestamp,
|
76
81
|
pid: pid
|
77
|
-
}.merge(
|
82
|
+
}.merge(context).merge(data).compact
|
78
83
|
end
|
79
84
|
|
80
|
-
def to_json
|
81
|
-
as_json.to_json + "\n"
|
85
|
+
def to_json(opts = nil)
|
86
|
+
as_json.to_json(opts) + "\n"
|
82
87
|
end
|
83
88
|
end
|
84
89
|
end
|
data/lib/sonnet/logger.rb
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
module Sonnet
|
4
4
|
module Logger
|
5
|
+
def self.new(logger)
|
6
|
+
logger.extend self
|
7
|
+
end
|
8
|
+
|
5
9
|
def self.extended(logger)
|
6
10
|
logger.formatter = Formatter
|
7
11
|
logger.level = log_level
|
@@ -11,10 +15,6 @@ module Sonnet
|
|
11
15
|
::Logger.const_get((ENV["LOG_LEVEL"] || "INFO").upcase)
|
12
16
|
end
|
13
17
|
|
14
|
-
def self.new(logger)
|
15
|
-
logger.extend(self)
|
16
|
-
end
|
17
|
-
|
18
18
|
def with_context(context = {})
|
19
19
|
formatter.current_context.push(context)
|
20
20
|
yield self
|
data/lib/sonnet/rails.rb
CHANGED
@@ -1,9 +1,37 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'sonnet'
|
4
|
+
|
3
5
|
module Sonnet
|
4
|
-
|
6
|
+
class Rails < ::Rails::Engine
|
5
7
|
config.before_configuration do
|
6
|
-
require "sonnet/
|
8
|
+
require "sonnet/rails_monkeypatch"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module RailsLogger
|
13
|
+
include ActiveSupport::LoggerThreadSafeLevel
|
14
|
+
include LoggerSilence
|
15
|
+
|
16
|
+
def self.included(base)
|
17
|
+
base.singleton_class.prepend (Module.new do
|
18
|
+
def extended(logger)
|
19
|
+
super(logger)
|
20
|
+
logger.after_initialize if logger.respond_to?(:after_initialize)
|
21
|
+
end
|
22
|
+
end)
|
7
23
|
end
|
24
|
+
|
25
|
+
def tagged(*tags, &block)
|
26
|
+
if tags.present?
|
27
|
+
with_context(tags: tags, &block)
|
28
|
+
else
|
29
|
+
yield self
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module Logger
|
35
|
+
include RailsLogger
|
8
36
|
end
|
9
37
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
if ActiveSupport::VERSION::MAJOR < 6
|
2
4
|
# Monkeypatch in Rails 6 ActiveSupport::TaggedLogging initializer.
|
3
5
|
# Solves problem in which the TaggedLogging initializer modifies an existing logger
|
@@ -36,4 +38,3 @@ else
|
|
36
38
|
ActiveSupport::Deprecation.warn('No longer need to monkeypatch ActiveSupport::TaggedLogging!')
|
37
39
|
ActiveSupport::Deprecation.warn('No longer need to monkeypatch ActionDispatch::DebugExceptions!')
|
38
40
|
end
|
39
|
-
|
data/lib/sonnet/version.rb
CHANGED
data/sonnet.gemspec
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
lib = File.expand_path(
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
|
4
4
|
require "sonnet/version"
|
@@ -21,5 +21,6 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler"
|
22
22
|
spec.add_development_dependency "byebug"
|
23
23
|
spec.add_development_dependency "minitest"
|
24
|
+
spec.add_development_dependency "minitest-reporters"
|
24
25
|
spec.add_development_dependency "rake"
|
25
26
|
end
|
data/test/logger_test.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class LoggerTest < Minitest::Test
|
4
|
+
Error = Class.new(StandardError)
|
5
|
+
|
6
|
+
def setup
|
7
|
+
logger.extend(Sonnet::Logger)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_log_info
|
11
|
+
logger.info("What's the story, morning glory?")
|
12
|
+
assert_equal log[0].slice(:level, :message), level: "info", message: "What's the story, morning glory?"
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_log_debug
|
16
|
+
logger.debug("this should not be logged")
|
17
|
+
assert_nil log[0]
|
18
|
+
logger.level = Logger::DEBUG
|
19
|
+
logger.debug("this should be logged")
|
20
|
+
assert_equal log[0].slice(:level, :message), level: "debug", message: "this should be logged"
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_log_exception
|
24
|
+
logger.error(Error.new("something went wrong"))
|
25
|
+
assert_equal messages(log), ["something went wrong"]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_log_with_context
|
29
|
+
logger.with_context(color: "blue") { logger.info("What's the story, morning glory?") }
|
30
|
+
logger.info("definitely maybe")
|
31
|
+
assert_equal log[0].slice(:color, :message), color: "blue", message: "What's the story, morning glory?"
|
32
|
+
assert_equal log[1].slice(:color, :message), message: "definitely maybe"
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
module Sonnet
|
6
|
+
class FormatterTest < Minitest::Test
|
7
|
+
def test_call
|
8
|
+
now = Time.now
|
9
|
+
with_program_name("sidekiq 5.2.5 diaco [0 of 10 busy]") do
|
10
|
+
log_line = JSON.parse(formatter.call("INFO", now, nil, "some message"), symbolize_names: true)
|
11
|
+
assert_equal log_line, {
|
12
|
+
program: "sidekiq",
|
13
|
+
level: "info",
|
14
|
+
timestamp: now.iso8601(3),
|
15
|
+
pid: $$,
|
16
|
+
message: "some message"
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def formatter
|
24
|
+
Formatter
|
25
|
+
end
|
26
|
+
|
27
|
+
def with_program_name(name)
|
28
|
+
original_program_name = $0
|
29
|
+
$0 = name
|
30
|
+
yield if block_given?
|
31
|
+
ensure
|
32
|
+
$0 = original_program_name
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/test/sonnet_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,37 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/reporters'
|
6
|
+
require 'byebug'
|
7
|
+
require 'logger'
|
8
|
+
|
9
|
+
Minitest::Reporters.use! [Minitest::Reporters::ProgressReporter.new]
|
5
10
|
|
6
11
|
require "sonnet"
|
12
|
+
|
13
|
+
module LoggingHelpers
|
14
|
+
def logger
|
15
|
+
@logger ||= Logger.new(io)
|
16
|
+
end
|
17
|
+
|
18
|
+
def io
|
19
|
+
@io ||= StringIO.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def assert_log_line(actual, expected)
|
23
|
+
assert_equal expected, actual.slice(*expected.keys)
|
24
|
+
end
|
25
|
+
|
26
|
+
def log
|
27
|
+
io.string.each_line.map { |line| JSON.parse(line, symbolize_names: true) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def messages(lines)
|
31
|
+
lines.map { |line| line[:message] }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Minitest::Test
|
36
|
+
include LoggingHelpers
|
37
|
+
end
|
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.6
|
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-
|
11
|
+
date: 2019-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-reporters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rake
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -85,10 +99,12 @@ files:
|
|
85
99
|
- lib/sonnet.rb
|
86
100
|
- lib/sonnet/formatter.rb
|
87
101
|
- lib/sonnet/logger.rb
|
88
|
-
- lib/sonnet/monkeypatch.rb
|
89
102
|
- lib/sonnet/rails.rb
|
103
|
+
- lib/sonnet/rails_monkeypatch.rb
|
90
104
|
- lib/sonnet/version.rb
|
91
105
|
- sonnet.gemspec
|
106
|
+
- test/logger_test.rb
|
107
|
+
- test/sonnet/formatter_test.rb
|
92
108
|
- test/sonnet_test.rb
|
93
109
|
- test/test_helper.rb
|
94
110
|
homepage: https://github.com/allspiritseve/sonnet
|