sonnet 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sonnet/formatter.rb +75 -0
- data/lib/sonnet/logger.rb +14 -0
- data/lib/sonnet/monkeypatch.rb +39 -0
- data/lib/sonnet/rails.rb +9 -0
- data/lib/sonnet/sidekiq/job_logger.rb +23 -0
- data/lib/sonnet/version.rb +3 -0
- data/lib/sonnet.rb +12 -0
- data/sonnet.gemspec +25 -0
- data/test/sonnet_test.rb +17 -0
- data/test/test_helper.rb +5 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7d7c9190a2c3b30088666a05896be4b39951cc028d9231cf9b11de55531c03bb
|
4
|
+
data.tar.gz: cf736f790ca819802faece8bd735c6a7e0d7aac4feaefce821e3c528c367f08e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: effec6afdf604f2dc9ed8dac4e18a67d45f44df8760e415600a555f7052fa7ef606674818e4a858ce5d51bd23ad7a9591348f45029836b570758726a672101f5
|
7
|
+
data.tar.gz: a667b7bdf7a12bfc8b1447cc32a688e8d62aedbda9494be3288e43ab0a130fc133a8e60ea891746369a3eef6acdfa9f498ecdbd9e4dae0f164c82d4c8b7c4c49
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sonnet (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
byebug (11.0.1)
|
10
|
+
minitest (5.11.3)
|
11
|
+
rake (12.3.2)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler
|
18
|
+
byebug
|
19
|
+
minitest
|
20
|
+
rake
|
21
|
+
sonnet!
|
22
|
+
|
23
|
+
BUNDLED WITH
|
24
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Cory Kaufman-Schofield
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Sonnet
|
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.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sonnet'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sonnet
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/allspiritseve/sonnet.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sonnet"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sonnet
|
4
|
+
class Formatter
|
5
|
+
TIMESTAMP_FORMAT = "%FT%T.%LZ"
|
6
|
+
|
7
|
+
def self.call(severity, time, progname, data)
|
8
|
+
new(severity, time, progname, data).to_json
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(severity, time, progname, data)
|
12
|
+
@severity = severity
|
13
|
+
@time = time
|
14
|
+
@progname = progname
|
15
|
+
@data = data
|
16
|
+
end
|
17
|
+
|
18
|
+
def program
|
19
|
+
@progname || File.basename($0)
|
20
|
+
end
|
21
|
+
|
22
|
+
def timestamp
|
23
|
+
(@time || Time.now).utc.strftime(TIMESTAMP_FORMAT)
|
24
|
+
end
|
25
|
+
|
26
|
+
def data
|
27
|
+
case @data
|
28
|
+
when Exception
|
29
|
+
serialize_exception(@data)
|
30
|
+
when Hash
|
31
|
+
@data
|
32
|
+
else
|
33
|
+
serialize_string(@data)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def level
|
38
|
+
@severity&.downcase
|
39
|
+
end
|
40
|
+
|
41
|
+
# def hostname
|
42
|
+
# @hostname || Socket.gethostname.force_encoding('UTF-8')
|
43
|
+
# end
|
44
|
+
|
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
|
+
def serialize_string(string)
|
54
|
+
{ message: string.to_s }
|
55
|
+
end
|
56
|
+
|
57
|
+
def pid
|
58
|
+
$$
|
59
|
+
end
|
60
|
+
|
61
|
+
def as_json
|
62
|
+
{
|
63
|
+
program: program,
|
64
|
+
# hostname: hostname,
|
65
|
+
level: level,
|
66
|
+
timestamp: timestamp,
|
67
|
+
pid: pid
|
68
|
+
}.merge(data).compact
|
69
|
+
end
|
70
|
+
|
71
|
+
def to_json
|
72
|
+
as_json.to_json + "\n"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sonnet
|
4
|
+
module Logger
|
5
|
+
def self.extended(logger)
|
6
|
+
logger.formatter = Formatter
|
7
|
+
logger.level = log_level
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.log_level
|
11
|
+
::Logger.const_get((ENV["LOG_LEVEL"] || "INFO").upcase)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
if ActiveSupport::VERSION::MAJOR < 6
|
2
|
+
# Monkeypatch in Rails 6 ActiveSupport::TaggedLogging initializer.
|
3
|
+
# Solves problem in which the TaggedLogging initializer modifies an existing logger
|
4
|
+
# by injecting its own functionality in.
|
5
|
+
module ActiveSupport
|
6
|
+
module TaggedLogging
|
7
|
+
def self.new(logger)
|
8
|
+
logger = logger.dup
|
9
|
+
|
10
|
+
if logger.formatter
|
11
|
+
logger.formatter = logger.formatter.dup
|
12
|
+
else
|
13
|
+
# Ensure we set a default formatter so we aren't extending nil!
|
14
|
+
logger.formatter = ActiveSupport::Logger::SimpleFormatter.new
|
15
|
+
end
|
16
|
+
|
17
|
+
logger.formatter.extend Formatter
|
18
|
+
logger.extend(self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Log actual exception instead of a string
|
24
|
+
module ActionDispatch
|
25
|
+
class DebugExceptions
|
26
|
+
private
|
27
|
+
|
28
|
+
def log_error(_request, wrapper)
|
29
|
+
ActiveSupport::Deprecation.silence do
|
30
|
+
ActionController::Base.logger.fatal(wrapper.exception)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
else
|
36
|
+
ActiveSupport::Deprecation.warn('No longer need to monkeypatch ActiveSupport::TaggedLogging!')
|
37
|
+
ActiveSupport::Deprecation.warn('No longer need to monkeypatch ActionDispatch::DebugExceptions!')
|
38
|
+
end
|
39
|
+
|
data/lib/sonnet/rails.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq/job_logger"
|
4
|
+
|
5
|
+
module Album
|
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(:start)
|
13
|
+
yield
|
14
|
+
logger.debug(:done, duration: elapsed(start))
|
15
|
+
rescue
|
16
|
+
logger.debug(:failure, duration: elapsed(start))
|
17
|
+
raise
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/sonnet.rb
ADDED
data/sonnet.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require "sonnet/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sonnet"
|
8
|
+
spec.version = Sonnet::VERSION
|
9
|
+
spec.authors = ["Cory Kaufman-Schofield"]
|
10
|
+
spec.email = ["cory@corykaufman.com"]
|
11
|
+
|
12
|
+
spec.summary = "Structured logs for Ruby applications"
|
13
|
+
spec.description = "Structured logs for Ruby applications"
|
14
|
+
spec.homepage = "https://github.com/allspiritseve/sonnet"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "byebug"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/test/sonnet_test.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "logger"
|
3
|
+
|
4
|
+
class SonnetTest < Minitest::Test
|
5
|
+
def test_that_it_has_a_version_number
|
6
|
+
refute_nil ::Sonnet::VERSION
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_json_logging
|
10
|
+
io = StringIO.new
|
11
|
+
logger = Logger.new(io)
|
12
|
+
logger.extend(Sonnet::Logger)
|
13
|
+
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?"
|
16
|
+
end
|
17
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sonnet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cory Kaufman-Schofield
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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'
|
69
|
+
description: Structured logs for Ruby applications
|
70
|
+
email:
|
71
|
+
- cory@corykaufman.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/sonnet.rb
|
85
|
+
- lib/sonnet/formatter.rb
|
86
|
+
- lib/sonnet/logger.rb
|
87
|
+
- lib/sonnet/monkeypatch.rb
|
88
|
+
- lib/sonnet/rails.rb
|
89
|
+
- lib/sonnet/sidekiq/job_logger.rb
|
90
|
+
- lib/sonnet/version.rb
|
91
|
+
- sonnet.gemspec
|
92
|
+
- test/sonnet_test.rb
|
93
|
+
- test/test_helper.rb
|
94
|
+
homepage: https://github.com/allspiritseve/sonnet
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata:
|
98
|
+
allowed_push_host: https://rubygems.org
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubygems_version: 3.0.1
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Structured logs for Ruby applications
|
118
|
+
test_files: []
|