loggery 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 +16 -0
- data/.rspec +2 -0
- data/.rubocop.yml +21 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +121 -0
- data/Rakefile +8 -0
- data/lib/loggery.rb +22 -0
- data/lib/loggery/controller/logging_context.rb +31 -0
- data/lib/loggery/gem/version.rb +7 -0
- data/lib/loggery/metadata/logstash_event_util.rb +44 -0
- data/lib/loggery/metadata/middleware/rack.rb +22 -0
- data/lib/loggery/metadata/middleware/sidekiq.rb +39 -0
- data/lib/loggery/metadata/store.rb +33 -0
- data/lib/loggery/railtie.rb +50 -0
- data/lib/loggery/sidekiq/setup.rb +22 -0
- data/lib/loggery/sidekiq/sidekiq_exception_logger.rb +9 -0
- data/lib/loggery/util.rb +23 -0
- data/loggery-gem.gemspec +36 -0
- metadata +219 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c0b7a03ec7d234a7fde324b7c5d08f01cfad7f64
|
4
|
+
data.tar.gz: 89318bb7a4a045a9248d0c78a7897fca044f738f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab421d8f289c4a5a059d6f0e1d651e2020a469099bf84adc808bce618d47843451ecc14ef5998c15abcde4482b1a2f3cf537ca355e839749a4edf6ba282b60f0
|
7
|
+
data.tar.gz: 3ebff63c9e25d2b0ca1e73e571b102b27985265b63dc4622d69e998277ab94b2053d8f216a3d9e30b7729cff5053df060cf6f875dc9cbbc855391f65c1c51935
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
EnforcedStyle: double_quotes
|
6
|
+
|
7
|
+
Metrics/LineLength:
|
8
|
+
Max: 120
|
9
|
+
|
10
|
+
Style/BlockLength:
|
11
|
+
Exclude:
|
12
|
+
- 'spec/**/*.rb'
|
13
|
+
|
14
|
+
Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/Lambda:
|
18
|
+
EnforcedStyle: literal
|
19
|
+
|
20
|
+
Style/MethodLength:
|
21
|
+
Max: 20
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at simon@stem.ps. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 LieferFactory GmbH
|
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,121 @@
|
|
1
|
+
# Loggery Gem
|
2
|
+
|
3
|
+
Make your Rails app produce [Logstash](https://www.elastic.co/products/logstash) compatible log
|
4
|
+
files that can be used for structured, centralized logging in
|
5
|
+
[Kibana](https://www.elastic.co/products/kibana).
|
6
|
+
|
7
|
+
This is a convenience gem that heavily builds on previous work by
|
8
|
+
[Lograge](https://github.com/roidrage/lograge) by
|
9
|
+
[roidrage](https://github.com/dwbutler/logstash-logger) and
|
10
|
+
[logstash-logger](https://github.com/dwbutler/logstash-logger) by
|
11
|
+
[dwbutler](https://github.com/dwbutler). It mainly connects these gems and sets some useful
|
12
|
+
defaults.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'loggery'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
### Basic Rails integration
|
27
|
+
```ruby
|
28
|
+
config.loggery.enabled = true
|
29
|
+
```
|
30
|
+
|
31
|
+
to your `config/application.rb`. In this basic setup, Loggery will save your log output to
|
32
|
+
`log/logstash-<rails-env>.log` in JSON format. It will also:
|
33
|
+
* use [lograge](https://github.com/roidrage/lograge) to create a single-line log entry for every
|
34
|
+
Rails request including the db / view / total duration, controller name, action, http status, etc...
|
35
|
+
* add the process PID to every log line
|
36
|
+
* add the request ID to every HTTP request, to easily follow the log trace of any single request.
|
37
|
+
|
38
|
+
### Custom logging
|
39
|
+
|
40
|
+
In addition to logging strings like any normal Rails app
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
Rails.logger.info "OMG something just happened!"
|
44
|
+
```
|
45
|
+
|
46
|
+
You can now also log hashes with additional information:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Rails.logger.info message: "OMG something just happened!", reason: "Foo servive not available", context:
|
50
|
+
some_hash.inspect, attempt: attempt_counter, time_taken: your_time_measurement
|
51
|
+
```
|
52
|
+
|
53
|
+
This allows you to give your logs more context about the thing you were trying to do, the state of
|
54
|
+
input variables, etc...
|
55
|
+
|
56
|
+
The following context attribute names should be avoided, because they are used by other tools in the
|
57
|
+
chain. Using those will result in an exception to make sure you notice during development and
|
58
|
+
testing:
|
59
|
+
* type
|
60
|
+
* uid
|
61
|
+
* _id
|
62
|
+
* _type
|
63
|
+
* _source
|
64
|
+
* _all
|
65
|
+
* _parent
|
66
|
+
* _fieldnames
|
67
|
+
* _routing
|
68
|
+
* _index
|
69
|
+
* _size
|
70
|
+
* _timestamp
|
71
|
+
* _ttl
|
72
|
+
|
73
|
+
### Add user metadata to logs
|
74
|
+
|
75
|
+
If you would like to enrich your log records with information about the active user, add this to
|
76
|
+
your `ApplicationController`:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
include Loggery::Controller::LoggingContext
|
80
|
+
|
81
|
+
def loggery_log_context
|
82
|
+
{
|
83
|
+
user_id: current_user&.id,
|
84
|
+
user_email: current_user&.email
|
85
|
+
}
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
The above example assumes you would like to log the `id` and `email` of your user. You can provide a
|
90
|
+
method `loggery_log_context` to include whichever additional information from your controllers you
|
91
|
+
would like to add to your log records.
|
92
|
+
|
93
|
+
### Sidekiq
|
94
|
+
|
95
|
+
If you're using Sidekiq you can enable structured logging in sidekiq by adding these lines to
|
96
|
+
`config/initializers/sidekiq.rb`:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
Loggery.setup_sidekiq(config)
|
100
|
+
```
|
101
|
+
|
102
|
+
This will make sure that useful sidekiq-metadata is added to your log lines to make tracing job
|
103
|
+
executions easier. The added info is:
|
104
|
+
* sidekiq job-id
|
105
|
+
* sidekiq queue
|
106
|
+
* thread-id
|
107
|
+
* worker name
|
108
|
+
* worker arguments
|
109
|
+
* retry count
|
110
|
+
* process PID
|
111
|
+
|
112
|
+
|
113
|
+
## Contributing
|
114
|
+
|
115
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/liefery/loggery-gem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
116
|
+
|
117
|
+
|
118
|
+
## License
|
119
|
+
|
120
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
121
|
+
|
data/Rakefile
ADDED
data/lib/loggery.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "loggery/gem/version"
|
4
|
+
require "loggery/util"
|
5
|
+
require "loggery/controller/logging_context"
|
6
|
+
require "loggery/sidekiq/setup"
|
7
|
+
require "loggery/sidekiq/sidekiq_exception_logger"
|
8
|
+
require "loggery/metadata/logstash_event_util"
|
9
|
+
require "loggery/metadata/store"
|
10
|
+
require "loggery/metadata/middleware/rack"
|
11
|
+
require "loggery/metadata/middleware/sidekiq"
|
12
|
+
require "loggery/railtie"
|
13
|
+
|
14
|
+
require "lograge"
|
15
|
+
require "logstash-event"
|
16
|
+
require "logstash-logger"
|
17
|
+
|
18
|
+
module Loggery
|
19
|
+
def self.setup_sidekiq
|
20
|
+
Loggery::Sidekiq::Setup.setup
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Provides a hook in Rails controllers that allows enriching log lines from within a Rails request
|
4
|
+
# with data that is available only within a controller action, for instance user information.
|
5
|
+
|
6
|
+
module Loggery
|
7
|
+
module Controller
|
8
|
+
module LoggingContext
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
before_action :loggery_set_metadata
|
13
|
+
end
|
14
|
+
|
15
|
+
# to be overridden in including class
|
16
|
+
def loggery_log_context
|
17
|
+
{}
|
18
|
+
end
|
19
|
+
|
20
|
+
def loggery_set_metadata
|
21
|
+
return unless Loggery::Metadata::Store.store
|
22
|
+
metadata = loggery_default_metadata.merge loggery_log_context
|
23
|
+
Loggery::Metadata::Store.store.merge!(metadata)
|
24
|
+
end
|
25
|
+
|
26
|
+
def loggery_default_metadata
|
27
|
+
{}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Hooks into LogStashLogger and adds the logging metadata that has been collected to the actual
|
4
|
+
# Logstash event
|
5
|
+
|
6
|
+
module Loggery
|
7
|
+
module Metadata
|
8
|
+
module LogstashEventUtil
|
9
|
+
extend self
|
10
|
+
|
11
|
+
MAGIC_FIELDS = %i{type uid _id _type _source _all _parent _fieldnames _routing
|
12
|
+
_index _size _timestamp _ttl }.freeze
|
13
|
+
|
14
|
+
def event_metadata(event)
|
15
|
+
return unless loglevel_includes_event?(event)
|
16
|
+
stored_metadata = Loggery::Metadata::Store.store || {}
|
17
|
+
metadata = default_metadata.merge(stored_metadata)
|
18
|
+
set_logstash_event_metadata(event, metadata)
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_logstash_event_metadata(event, metadata)
|
22
|
+
metadata.each { |k, v| event[k] = v }
|
23
|
+
fail_if_magic_fields_are_used(event)
|
24
|
+
end
|
25
|
+
|
26
|
+
def default_metadata
|
27
|
+
{ pid: Process.pid }
|
28
|
+
end
|
29
|
+
|
30
|
+
def loglevel_includes_event?(event)
|
31
|
+
severity = event['severity'].downcase
|
32
|
+
Rails.logger.respond_to?(severity) && Rails.logger.public_send("#{severity}?")
|
33
|
+
end
|
34
|
+
|
35
|
+
def fail_if_magic_fields_are_used(event)
|
36
|
+
MAGIC_FIELDS.each do |magic_field|
|
37
|
+
if event[magic_field.to_s].present? || event[magic_field.to_sym].present?
|
38
|
+
raise "'#{magic_field}' is a reserved field name of logstash. It should not be set in a custom event"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Rack middleware that adds basic request metadata to all log lines.
|
4
|
+
|
5
|
+
module Loggery
|
6
|
+
module Metadata
|
7
|
+
module Middleware
|
8
|
+
class Rack
|
9
|
+
def initialize(app)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
Loggery::Metadata::Store.with_metadata(worker_type: "web",
|
15
|
+
request_id: env["action_dispatch.request_id"]) do
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Sidekiq middleware that adds basic sidekiq metadata to all log lines.
|
4
|
+
|
5
|
+
module Loggery
|
6
|
+
module Metadata
|
7
|
+
module Middleware
|
8
|
+
class Sidekiq
|
9
|
+
include Loggery::Util
|
10
|
+
|
11
|
+
# Clients can provide their own error handler
|
12
|
+
class << self
|
13
|
+
attr_accessor(:error_handler) { ->(e) { Sidekiq::Logging.logger.error(e) } }
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(_worker, msg, queue)
|
17
|
+
Loggery::Metadata::Store.with_metadata(jid: msg["jid"],
|
18
|
+
thread_id: Thread.current.object_id.to_s(36),
|
19
|
+
worker: msg["class"],
|
20
|
+
args: msg["args"].inspect,
|
21
|
+
queue: queue,
|
22
|
+
retry_count: msg["retry_count"],
|
23
|
+
worker_type: "sidekiq") do
|
24
|
+
log_job_runtime(:sidekiq_job, "#{msg['class']} (#{msg['args']})") do
|
25
|
+
begin
|
26
|
+
yield
|
27
|
+
rescue StandardError => e
|
28
|
+
# Log exceptions here, otherwise they won't have the metadata available anymore by
|
29
|
+
# the time they reach the Sidekiq default error handler.
|
30
|
+
self.class.error_handler&.call(e)
|
31
|
+
raise e
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Loggery
|
4
|
+
module Metadata
|
5
|
+
module Store
|
6
|
+
METADATA_KEY = :logging_metadata
|
7
|
+
|
8
|
+
def self.store
|
9
|
+
Thread.current[METADATA_KEY]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.with_metadata(metadata)
|
13
|
+
init_store
|
14
|
+
merge!(metadata)
|
15
|
+
yield
|
16
|
+
ensure
|
17
|
+
close_store
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.merge!(metadata)
|
21
|
+
store.merge!(metadata)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.init_store
|
25
|
+
Thread.current[METADATA_KEY] = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.close_store
|
29
|
+
Thread.current[METADATA_KEY] = nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Loggery
|
4
|
+
module LogstashSetup
|
5
|
+
def self.setup(config)
|
6
|
+
config.logstash.type = :file
|
7
|
+
config.logstash.path = config.loggery.log_file
|
8
|
+
|
9
|
+
LogStashLogger.configure do |logstash|
|
10
|
+
logstash.customize_event do |event|
|
11
|
+
Loggery::Metadata::LogstashEventUtil.event_metadata(event)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module LogrageSetup
|
18
|
+
def self.setup(config)
|
19
|
+
config.lograge.enabled = true
|
20
|
+
config.lograge.keep_original_rails_log = false
|
21
|
+
config.lograge.logger = Rails.logger
|
22
|
+
config.lograge.formatter = Lograge::Formatters::Logstash.new
|
23
|
+
|
24
|
+
config.lograge.custom_options = ->(_event) do
|
25
|
+
{ event_type: :request }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module LoggerySetup
|
31
|
+
def self.setup(app)
|
32
|
+
LogstashSetup.setup(app.config)
|
33
|
+
LogrageSetup.setup(app.config)
|
34
|
+
|
35
|
+
app.config.middleware.insert_before Rails::Rack::Logger, Loggery::Metadata::Middleware::Rack
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Railtie < Rails::Railtie
|
40
|
+
config.loggery = OpenStruct.new
|
41
|
+
|
42
|
+
# Default options
|
43
|
+
config.loggery.enabled = true
|
44
|
+
config.loggery.log_file = "log/logstash-#{Rails.env}.log"
|
45
|
+
|
46
|
+
initializer :loggery, before: :initialize_logger do |app|
|
47
|
+
LoggerySetup.setup(app) if app.config.loggery.enabled
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Loggery
|
4
|
+
module Sidekiq
|
5
|
+
module Setup
|
6
|
+
def self.setup
|
7
|
+
::Sidekiq::Logging.logger = Rails.logger
|
8
|
+
|
9
|
+
::Sidekiq.configure_server do |config|
|
10
|
+
config.server_middleware do |chain|
|
11
|
+
chain.add Loggery::Metadata::Middleware::Sidekiq
|
12
|
+
end
|
13
|
+
|
14
|
+
# Sidekiq by default logs deeply nested json which throws off the json logger and elasticsearch.
|
15
|
+
# We therefore want to use our own logger that serializes this hash
|
16
|
+
config.error_handlers.clear
|
17
|
+
config.error_handlers << SidekiqExceptionLogger.new
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Logs an exception and a sidekiq context hash in logstash compatible form
|
4
|
+
|
5
|
+
class SidekiqExceptionLogger
|
6
|
+
def call(exception, context_hash)
|
7
|
+
Sidekiq.logger.warn(class: exception.class.name, message: exception.message, context: context_hash.inspect)
|
8
|
+
end
|
9
|
+
end
|
data/lib/loggery/util.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Loggery
|
4
|
+
module Util
|
5
|
+
def log_job_runtime(job_type, job_instance_name = nil)
|
6
|
+
job_name = ["Job type #{job_type}", job_instance_name].compact.join " - "
|
7
|
+
|
8
|
+
Rails.logger.info event_type: :"#{job_type}_started", message: "#{job_name} started"
|
9
|
+
|
10
|
+
begin
|
11
|
+
start_time = Time.current
|
12
|
+
yield if block_given?
|
13
|
+
ensure
|
14
|
+
end_time = Time.current
|
15
|
+
duration = end_time - start_time
|
16
|
+
|
17
|
+
Rails.logger.info event_type: :"#{job_type}_finished",
|
18
|
+
message: "#{job_name} finished",
|
19
|
+
duration: duration
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/loggery-gem.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "loggery/gem/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "loggery"
|
9
|
+
spec.version = Loggery::Gem::VERSION
|
10
|
+
spec.authors = ["Simon Stemplinger"]
|
11
|
+
spec.email = ["simon@stem.ps"]
|
12
|
+
|
13
|
+
spec.summary = "Generate logstash compatible log output from Rails apps."
|
14
|
+
spec.homepage = "https://github.com/liefery/loggery-gem"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "lograge", "~> 0.6"
|
25
|
+
spec.add_runtime_dependency "logstash-event", "~> 1.2"
|
26
|
+
spec.add_runtime_dependency "logstash-logger", "~> 0.25"
|
27
|
+
spec.add_runtime_dependency "rails", ">= 4.0"
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
30
|
+
spec.add_development_dependency "pry-byebug"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec"
|
33
|
+
spec.add_development_dependency "rubocop"
|
34
|
+
spec.add_development_dependency "sidekiq"
|
35
|
+
spec.add_development_dependency "sqlite3"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: loggery
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Stemplinger
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: lograge
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: logstash-event
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: logstash-logger
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.25'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.25'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.14'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.14'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '10.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: sidekiq
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: sqlite3
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
description:
|
168
|
+
email:
|
169
|
+
- simon@stem.ps
|
170
|
+
executables: []
|
171
|
+
extensions: []
|
172
|
+
extra_rdoc_files: []
|
173
|
+
files:
|
174
|
+
- ".gitignore"
|
175
|
+
- ".rspec"
|
176
|
+
- ".rubocop.yml"
|
177
|
+
- ".travis.yml"
|
178
|
+
- CODE_OF_CONDUCT.md
|
179
|
+
- Gemfile
|
180
|
+
- LICENSE.txt
|
181
|
+
- README.md
|
182
|
+
- Rakefile
|
183
|
+
- lib/loggery.rb
|
184
|
+
- lib/loggery/controller/logging_context.rb
|
185
|
+
- lib/loggery/gem/version.rb
|
186
|
+
- lib/loggery/metadata/logstash_event_util.rb
|
187
|
+
- lib/loggery/metadata/middleware/rack.rb
|
188
|
+
- lib/loggery/metadata/middleware/sidekiq.rb
|
189
|
+
- lib/loggery/metadata/store.rb
|
190
|
+
- lib/loggery/railtie.rb
|
191
|
+
- lib/loggery/sidekiq/setup.rb
|
192
|
+
- lib/loggery/sidekiq/sidekiq_exception_logger.rb
|
193
|
+
- lib/loggery/util.rb
|
194
|
+
- loggery-gem.gemspec
|
195
|
+
homepage: https://github.com/liefery/loggery-gem
|
196
|
+
licenses:
|
197
|
+
- MIT
|
198
|
+
metadata: {}
|
199
|
+
post_install_message:
|
200
|
+
rdoc_options: []
|
201
|
+
require_paths:
|
202
|
+
- lib
|
203
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
209
|
+
requirements:
|
210
|
+
- - ">="
|
211
|
+
- !ruby/object:Gem::Version
|
212
|
+
version: '0'
|
213
|
+
requirements: []
|
214
|
+
rubyforge_project:
|
215
|
+
rubygems_version: 2.5.1
|
216
|
+
signing_key:
|
217
|
+
specification_version: 4
|
218
|
+
summary: Generate logstash compatible log output from Rails apps.
|
219
|
+
test_files: []
|