hickory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 06f7b612145ede0127857dac94e78b6e90538f6a
4
+ data.tar.gz: ee7f5b25696032a1281ea16cb788e4e7e2b9235c
5
+ SHA512:
6
+ metadata.gz: b5fde2b0952019adcb2deb38bcf7fda83741258f0eeed946dd955c939c33508afbf6b46535c3812370b3a2bc94bb2c26bf257f780aa98a3d8bd99a710a6a81ac
7
+ data.tar.gz: d3f6e63eb0d1e57ceec88ffe447f0b11dbc42a81a4d7af3520df4cb85da7670172b6689a8eae675680b9308840e710dafdd4ba8c90fec6c8c41633853184ba5f
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hickory.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 blahed
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
+ # Hickory
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/hickory`. 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 'hickory'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hickory
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/[USERNAME]/hickory.
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
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hickory"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/hickory.gemspec ADDED
@@ -0,0 +1,25 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hickory/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hickory'
8
+ spec.version = Hickory::VERSION
9
+ spec.authors = ['blahed']
10
+ spec.email = ['trvsdnn@gmail.com']
11
+
12
+ spec.summary = 'An opinionated logger for rails and ruby.'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.16'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'minitest', '~> 5.0'
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_support/concern'
2
+ require 'rails/rack/logger'
3
+
4
+ module Rails
5
+ module Rack
6
+ # Overwrites defaults of Rails::Rack::Logger that cause
7
+ # unnecessary logging.
8
+ # This effectively removes the log lines from the log
9
+ # that say:
10
+ # Started GET / for 192.168.2.1...
11
+ class Logger
12
+ # Overwrites Rails 3.2 code that logs new requests
13
+ def call_app(*args)
14
+ env = args.last
15
+ @app.call(env)
16
+ ensure
17
+ ActiveSupport::LogSubscriber.flush_all!
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ module Hickory
2
+ module Formatters
3
+ class JSON < ::Logger::Formatter
4
+ def initialize(options = {})
5
+ @key_mappings = Hash.new.tap { |mapping|
6
+ mapping[:message] = options.fetch(:message_key, :message)
7
+ mapping[:severity] = options.fetch(:severity_key, :level)
8
+ mapping[:timestamp] = options.fetch(:timestamp_key, :timestamp)
9
+ mapping[:env] = options.fetch(:env_key, :env)
10
+ }
11
+ end
12
+
13
+ def call(severity, time, progname, msg)
14
+ line = msg.tap { |message|
15
+ message[@key_mappings[:message]] = msg.delete(:message) unless @key_mappings[:message] == :message
16
+ message[@key_mappings[:timestamp]] = time
17
+ message[@key_mappings[:severity]] = severity
18
+ message[@key_mappings[:env]] = Rails.env
19
+ message[:progname] = progname if progname.present?
20
+ }
21
+
22
+ line.to_json
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ module Hickory
2
+ module Formatters
3
+ class Simple < ::Logger::Formatter
4
+ def call(severity, time, progname, msg)
5
+ message = msg.delete(:message)
6
+ metadata = msg.map {|k,v| "#{k}=#{v}" }.join(' ')
7
+
8
+ "#{message} #{metadata}\n"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,62 @@
1
+ module Hickory
2
+ module LogSubscribers
3
+ class ActionController < ActiveSupport::LogSubscriber
4
+ def process_action(event)
5
+ return if Hickory.ignore_if.present? && Hickory.ignore_if.call(event)
6
+
7
+ payload = event.payload
8
+ default_keys = %i[method format controller action user_id request_id]
9
+
10
+ metadata = payload.slice(default_keys).tap { |data|
11
+ data.merge!(uri(payload))
12
+ data.merge!(runtimes(event, payload))
13
+ data.merge!(status(payload))
14
+ data.merge!(params(payload))
15
+ }
16
+
17
+ logger.info('canoncial-request', metadata)
18
+ end
19
+
20
+ private
21
+
22
+ def uri(payload)
23
+ return {} unless payload.key?(:path)
24
+
25
+ _uri = URI.parse(payload[:path])
26
+
27
+ { path: _uri.path }.tap { |data|
28
+ data[:query] = _uri.query unless _uri.query.nil?
29
+ }
30
+ end
31
+
32
+ def runtimes(event, payload)
33
+ { duration: event.duration.to_f.round(2) }.tap { |data|
34
+ data[:view] = payload[:view_runtime].to_f.round(2) if payload.key?(:view_runtime)
35
+ data[:db] = payload[:db_runtime].to_f.round(2) if payload.key?(:db_runtime)
36
+ }
37
+ end
38
+
39
+ def status(payload)
40
+ if status = payload[:status]
41
+ { status: status.to_i }
42
+ elsif error = payload[:exception]
43
+ exception, message = error
44
+ { status: get_error_status_code(exception), error: "#{exception}: #{message}" }
45
+ else
46
+ { status: 0 }
47
+ end
48
+ end
49
+
50
+ def params(payload)
51
+ {
52
+ params: payload[:params].except(:controller, :action, :format)
53
+ }
54
+ end
55
+
56
+ def get_error_status_code(exception)
57
+ status = ActionDispatch::ExceptionWrapper.rescue_responses[exception]
58
+ Rack::Utils.status_code(status)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,35 @@
1
+ module Hickory
2
+ module LogSubscribers
3
+ class ActionMailer < ActiveSupport::LogSubscriber
4
+ def process(event)
5
+ metadata = {
6
+ mailer: event.payload[:mailer],
7
+ action: event.payload[:action]
8
+ }
9
+
10
+ logger.info('mailer-process', metadata)
11
+ end
12
+
13
+ def deliver(event)
14
+ payload = event.payload
15
+ metadata = {
16
+ mailer: payload[:mailer],
17
+ message_id: payload[:message_id],
18
+ subject: payload[:subject],
19
+ to: obfuscate_emails(payload[:to]),
20
+ from: obfuscate_emails(payload[:from])
21
+ }
22
+
23
+ logger.info('mailer-deliver', metadata)
24
+ end
25
+
26
+ private
27
+
28
+ def obfuscate_emails(emails)
29
+ emails.map do |email|
30
+ email.sub(/([^\s]+)@([^\s]+)/) { |_| "#{$1[0...4]}****@#{$2[0...3]}***" }
31
+ end.join(', ')
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,59 @@
1
+ module Hickory
2
+ module LogSubscribers
3
+ class ActiveJob < ActiveSupport::LogSubscriber
4
+ def enqueue(event)
5
+ logger.info('job-enqueued', job_metadata(event))
6
+ end
7
+
8
+ def enqueue_retry(event)
9
+ metadata = job_metadata(event)
10
+ metadata.merge!({
11
+ error: event.payload[:error],
12
+ wait: event.payload[:wait]
13
+ })
14
+
15
+ logger.info('job-retry-enqueued', metadata)
16
+ end
17
+
18
+ def perform_start(event)
19
+ logger.info('job-perform-start', job_metadata(event))
20
+ end
21
+
22
+ def perform(event)
23
+ logger.info('job-perform', job_metadata(event))
24
+ end
25
+
26
+ def retry_stopped(event)
27
+ metadata = job_metadata(event)
28
+ metadata.merge!({
29
+ error: event.payload[:error]
30
+ })
31
+
32
+ logger.info('job-retry-stopped', metadata)
33
+ end
34
+
35
+ def retry_discard(event)
36
+ metadata = job_metadata(event)
37
+ metadata.merge!({
38
+ error: event.payload[:error]
39
+ })
40
+
41
+ logger.info('job-discard', metadata)
42
+ end
43
+
44
+ private
45
+
46
+ def job_metadata(event)
47
+ job = event.payload[:job]
48
+
49
+ {
50
+ job_id: job.job_id,
51
+ queue: job.queue_name,
52
+ priority: job.priority,
53
+ executions: job.executions
54
+ }
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,68 @@
1
+ require 'json'
2
+ require 'action_pack'
3
+ require 'active_support/core_ext/class/attribute'
4
+ require 'active_support/log_subscriber'
5
+ # require 'action_view/log_subscriber'
6
+ # require 'action_controller/log_subscriber'
7
+ # require 'request_store'
8
+
9
+ module Hickory
10
+ module LogSubscribers
11
+ class Base < ActiveSupport::LogSubscriber
12
+ # def logger
13
+ # Lograge.logger.presence || super
14
+ # end
15
+
16
+ private
17
+
18
+ # def process_main_event(event)
19
+ # # return if Lograge.ignore?(event)
20
+ #
21
+ # payload = event.payload
22
+ # data = extract_request(event, payload)
23
+ # data = before_format(data, payload)
24
+ # formatted_message = Lograge.formatter.call(data)
25
+ # logger.send(Lograge.log_level, formatted_message)
26
+ # end
27
+ #
28
+ # def extract_request(event, payload)
29
+ # data = initial_data(payload)
30
+ # data.merge!(extract_status(payload))
31
+ # data.merge!(extract_runtimes(event, payload))
32
+ # data.merge!(extract_location)
33
+ # data.merge!(extract_unpermitted_params)
34
+ # data.merge!(custom_options(event))
35
+ # end
36
+ #
37
+ # %i(initial_data extract_status extract_runtimes
38
+ # extract_location extract_unpermitted_params).each do |method_name|
39
+ # define_method(method_name) { |*_arg| {} }
40
+ # end
41
+ #
42
+ # def extract_status(payload)
43
+ # if (status = payload[:status])
44
+ # { status: status.to_i }
45
+ # elsif (error = payload[:exception])
46
+ # exception, message = error
47
+ # { status: get_error_status_code(exception), error: "#{exception}: #{message}" }
48
+ # else
49
+ # { status: 0 }
50
+ # end
51
+ # end
52
+ #
53
+ # def get_error_status_code(exception)
54
+ # status = ActionDispatch::ExceptionWrapper.rescue_responses[exception]
55
+ # Rack::Utils.status_code(status)
56
+ # end
57
+ #
58
+ # def custom_options(event)
59
+ # options = Lograge.custom_options(event) || {}
60
+ # options.merge event.payload[:custom_payload] || {}
61
+ # end
62
+ #
63
+ # def before_format(data, payload)
64
+ # Lograge.before_format(data, payload)
65
+ # end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,35 @@
1
+ require 'logger'
2
+
3
+ require 'active_support/logger_silence'
4
+ require 'active_support/logger_thread_safe_level'
5
+
6
+ require 'hickory/formatters/json'
7
+ require 'hickory/formatters/simple'
8
+
9
+ module Hickory
10
+ class Logger < ::Logger
11
+ include ActiveSupport::LoggerThreadSafeLevel
12
+ include ActiveSupport::LoggerSilence
13
+
14
+ SEVERITIY_LABELS = %w(TRACE DEBUG INFO WARN ERROR FATAL UNKNOWN)
15
+
16
+ %i[trace debug info warn error fatal unknown].each do |level|
17
+
18
+ define_method(level) do |msg = nil, meta={}, &block|
19
+ message = { message: msg }
20
+
21
+ message.merge!(meta) if meta.is_a?(Hash) && !meta.empty?
22
+
23
+ severity = SEVERITIY_LABELS.index(level.to_s.upcase) - 1
24
+
25
+ if block
26
+ send(level, block.call)
27
+ else
28
+ log(severity, message)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ require 'hickory/railtie' if defined?(Rails)
@@ -0,0 +1,18 @@
1
+ require 'rails/railtie'
2
+ require 'active_support/ordered_options'
3
+
4
+ # require 'hickory/log_subscribers/base'
5
+ require 'hickory/log_subscribers/action_controller'
6
+ require 'hickory/log_subscribers/action_mailer'
7
+ require 'hickory/log_subscribers/active_job'
8
+
9
+ module Hickory
10
+ class Railtie < Rails::Railtie
11
+ config.hickory = ActiveSupport::OrderedOptions.new
12
+ config.hickory.canonical_request_lines = false
13
+
14
+ config.after_initialize do |app|
15
+ Hickory.initialize_canonical_logs(app) if app.config.hickory.canonical_request_lines
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Hickory
2
+ VERSION = '0.1.0'
3
+ end
data/lib/hickory.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'hickory/logger'
2
+ require 'hickory/version'
3
+
4
+ module Hickory
5
+ module_function
6
+
7
+ def initialize_canonical_logs(app)
8
+ require 'hickory/ext/rails/rack/logger'
9
+
10
+ self.application = app
11
+ Hickory.ignore_if = config.ignore_if
12
+
13
+ Hickory.remove_existing_log_subscriptions
14
+ Hickory::LogSubscribers::ActionController.attach_to :action_controller
15
+ Hickory::LogSubscribers::ActionMailer.attach_to :action_mailer
16
+ Hickory::LogSubscribers::ActiveJob.attach_to :active_job
17
+ end
18
+
19
+ def application
20
+ @@application
21
+ end
22
+
23
+ def application=(app)
24
+ @@application = app
25
+ end
26
+
27
+ def ignore_if
28
+ @@ignore_if
29
+ end
30
+
31
+ def ignore_if=(check)
32
+ @@ignore_if = check
33
+ end
34
+
35
+ def config
36
+ application.config.hickory
37
+ end
38
+
39
+ def remove_existing_log_subscriptions
40
+ ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
41
+ case subscriber
42
+ when ActionView::LogSubscriber
43
+ unsubscribe(:action_view, subscriber)
44
+ when ActionController::LogSubscriber
45
+ unsubscribe(:action_controller, subscriber)
46
+ when ActionMailer::LogSubscriber
47
+ unsubscribe(:action_mailer, subscriber)
48
+ when ActiveJob::Logging::LogSubscriber
49
+ unsubscribe(:active_job, subscriber)
50
+ end
51
+ end
52
+ end
53
+
54
+ def unsubscribe(component, subscriber)
55
+ events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' }
56
+ events.each do |event|
57
+ ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
58
+ if listener.instance_variable_get('@delegate') == subscriber
59
+ ActiveSupport::Notifications.unsubscribe listener
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hickory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - blahed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-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: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - trvsdnn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - hickory.gemspec
71
+ - lib/hickory.rb
72
+ - lib/hickory/ext/rails/rack/logger.rb
73
+ - lib/hickory/formatters/json.rb
74
+ - lib/hickory/formatters/simple.rb
75
+ - lib/hickory/log_subscribers/action_controller.rb
76
+ - lib/hickory/log_subscribers/action_mailer.rb
77
+ - lib/hickory/log_subscribers/active_job.rb
78
+ - lib/hickory/log_subscribers/base.rb
79
+ - lib/hickory/logger.rb
80
+ - lib/hickory/railtie.rb
81
+ - lib/hickory/version.rb
82
+ homepage:
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: An opinionated logger for rails and ruby.
106
+ test_files: []