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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fde1c29033040450a05efda38fc80a4ab39647af542de057bf1a51a12ae9c98
4
- data.tar.gz: e02bacca362f4992aa567576d88720c961e3b733e7c9abdba5bdba9487aad0ac
3
+ metadata.gz: 3961783bc4eb8f519c30df1a232e34031827335f1e4804f5c5d444ef15bfb57c
4
+ data.tar.gz: 2fccb0e4851778f673a1ff10de4a6ed771a919c7692612d0320cfabf3728d849
5
5
  SHA512:
6
- metadata.gz: 1bb24a4c08a4429764bd50319d088e342147fbf794ccb908213bd0dc74f010d01acafe9aa2c39f87b3b09396f9d29f072ae3f37a76fd6b2ef2730cd437ba8f0e
7
- data.tar.gz: 65509aff4d33b784ec8576e3e78f2ef6bfe2fb9ffea11f9cf03f2877955eba628b12a7051667c68f679ecaf8381d7ac2142bf6f8b4448bcb5a86acfd6f8fc281
6
+ metadata.gz: 133fb98a3a340477a2076b35d7256aaeb52f6b43e62c1f1b7703d0b4b16a599dabd7defbd7127f3d7ad1463a4de175946db9ca4d05735e63616be0a5b6749c4c
7
+ data.tar.gz: bde201a3bddec39c6179d0b371aabfae9d2a9039f1975bb155ad68db0ccb4bebeb5c416f3ab5c933334a03b3196edde874360699823c4afa6f3c0c2d5cc14463
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in sonnet.gemspec
data/Gemfile.lock CHANGED
@@ -1,14 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sonnet (0.1.5)
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rake/testtask"
3
5
 
@@ -7,4 +9,4 @@ Rake::TestTask.new(:test) do |t|
7
9
  t.test_files = FileList["test/**/*_test.rb"]
8
10
  end
9
11
 
10
- task :default => :test
12
+ task default: :test
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "sonnet"
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, queue, &block)
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)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "time"
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({}, &:merge)
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(data).merge(context).compact
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
- class Rails < ::Rails::Engine
6
+ class Rails < ::Rails::Engine
5
7
  config.before_configuration do
6
- require "sonnet/monkeypatch"
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
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Sonnet
2
- VERSION = "0.1.5"
4
+ VERSION = "0.1.6"
3
5
  end
data/sonnet.gemspec CHANGED
@@ -1,4 +1,4 @@
1
- lib = File.expand_path("../lib", __FILE__)
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
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class SonnetTest < Minitest::Test
data/test/test_helper.rb CHANGED
@@ -1,6 +1,37 @@
1
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
- require "minitest/autorun"
3
- require "byebug"
4
- require "logger"
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.5
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-04-09 00:00:00.000000000 Z
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