exception_file_notifier 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6253e8fb63683fc043da38e4a201c0c7b20d50d9
4
+ data.tar.gz: cb722eefe2b781fe6f2792a422bb8a6f967ad1c0
5
+ SHA512:
6
+ metadata.gz: e91366f1931ce5fd49e6592da8881cd3fba762d33c55847b8ca45eafbfdd34ebd3309b7550dc9c9982c045b6dc858c06e485abe8110f8da6060ca07e62a97227
7
+ data.tar.gz: f4da9a5eb2381e6ca1ed74e8f1c69a8e2b4aa2f69812fa76820cb94b9597a522936bdbd329748736a80b30e419329f1981b8cfce6558b6037103e26841798f97
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ rvm:
5
+ - 2.1
6
+ - 2.2
7
+ - 2.3.1
8
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in exception_file_notifier.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Koji Onishi
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.
@@ -0,0 +1,40 @@
1
+ # ExceptionFileNotifier
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/exception_file_notifier`. 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 'exception_file_notifier'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install exception_file_notifier
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 spec` 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/fursich/exception_file_notifier.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exception_file_notifier"
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__)
@@ -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
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'exception_notifier/exception_file_notifier/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "exception_file_notifier"
8
+ spec.version = ::ExceptionNotifier::ExceptionFileNotifier::VERSION
9
+ spec.authors = ["Koji Onishi"]
10
+ spec.email = ["fursich0@gmail.com"]
11
+
12
+ spec.summary = %q{ A custom notifier for ExceptionNotification that generates exception log files in JSON format. }
13
+ spec.description = %q{ Exception File Notifier records exception logs in JSON format, helping you track errors in the production environment. }
14
+ spec.homepage = "https://github.com/fursich/exception_file_notifier"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.required_ruby_version = '>= 2.1'
34
+ spec.required_rubygems_version = '>= 1.8.11'
35
+ # spec.add_dependency("activesupport", ">= 4.0", "< 6")
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.14"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "rails", ">= 4.0", "< 6"
41
+ spec.add_development_dependency "pry"
42
+ spec.add_dependency "exception_notification", ">= 4.0"
43
+ end
@@ -0,0 +1,2 @@
1
+ require "exception_notifier/exception_file_notifier/version"
2
+ require "exception_notifier/file_notifier"
@@ -0,0 +1,5 @@
1
+ module ExceptionNotifier
2
+ module ExceptionFileNotifier
3
+ VERSION = "0.0.6"
4
+ end
5
+ end
@@ -0,0 +1,155 @@
1
+ require "exception_notification"
2
+ require "action_dispatch"
3
+ require "active_support/core_ext/hash"
4
+ require "socket"
5
+ require 'logger'
6
+ require 'json'
7
+
8
+ module ExceptionNotifier
9
+ class FileNotifier < BaseNotifier
10
+ include ExceptionNotifier::BacktraceCleaner
11
+
12
+ def initialize(options={})
13
+ filename = options[:filename] || "log/error.log"
14
+ shift_age = options[:shift_age] || 0
15
+ shift_size = options[:shift_size] || nil
16
+
17
+ @logger_options = [filename, shift_age, shift_size]
18
+ @formatter = -> (_, _, _, message) { message + "\n" }
19
+ end
20
+
21
+ def call(exception=nil, options={})
22
+ append_log exception_message(options[:env], exception, options)
23
+ end
24
+
25
+ def append_log(message)
26
+ log = ::Logger.new(*@logger_options)
27
+ log.formatter = @formatter
28
+ error_log = generate_json(message)
29
+ log.error(error_log)
30
+ log.close
31
+ end
32
+
33
+ def generate_json(message)
34
+ ::JSON.generate(deep_encode(message))
35
+ end
36
+
37
+ def exception_message(env, exception, options={})
38
+ return error_message(exception) unless exception.is_a?(Exception)
39
+
40
+ if env.present?
41
+ exception_notification(options[:env], exception, options)
42
+ else
43
+ background_exception_notification(exception, options)
44
+ end
45
+ end
46
+
47
+ def exception_notification(env, exception, options)
48
+ @env = env
49
+ @exception = exception
50
+ @options = options.reverse_merge(@env['exception_notifier.options'] || {}).symbolize_keys
51
+ @kontroller = @env['action_controller.instance'] || MissingController.new
52
+ @request = ::ActionDispatch::Request.new(@env)
53
+ @backtrace = exception.backtrace ? clean_backtrace(exception) : []
54
+ @timestamp = ::Time.current
55
+ @data = (@env['exception_notifier.exception_data'] || {}).merge(options[:data] || {})
56
+
57
+ render_notice
58
+ end
59
+
60
+ def background_exception_notification(exception, options)
61
+ @exception = exception
62
+ @options = options.symbolize_keys
63
+ @backtrace = exception.backtrace || []
64
+ @timestamp = ::Time.current
65
+ @data = options[:data] || {}
66
+ @env = @kontroller = nil
67
+
68
+ render_notice
69
+ end
70
+
71
+ def error_message(exception)
72
+ {
73
+ message: '[FATAL] NO EXCEPTION GIVEN: Please provide Exception as argument',
74
+ exception: exception.try(:to_s),
75
+ timestamp: ::Time.current,
76
+ }
77
+ end
78
+
79
+ def render_notice
80
+ set_data_variables
81
+
82
+ exception_details = {
83
+ exception: @exception.class,
84
+ controller: @kontroller.present? ? @kontroller.controller_name : '[BACKGROUND]',
85
+ action: @kontroller.present? ? @kontroller.action_name : '[BACKGROUND]',
86
+ exception_message: @exception.message,
87
+
88
+ timestamp: @timestamp,
89
+ server: ::Socket.gethostname,
90
+ rails_root: (defined?(::Rails) && ::Rails.respond_to?(:root) ) ? ::Rails.root : nil,
91
+ process: $$,
92
+ backtrace: @backtrace,
93
+ }
94
+
95
+ if @env.present?
96
+ exception_details.merge!({
97
+ url: @request.url,
98
+ http_method: @request.request_method,
99
+ ip_address: @request.remote_ip,
100
+ parameters: (@request.filtered_parameters.inspect rescue "[NOT ENCODABLE]"),
101
+ session_id: (@request.ssl? ? "[FILTERED]" : @request.session['session_id'] || (@request.env["rack.session.options"] and @request.env["rack.session.options"][:id])),
102
+ session_data: @request.session.to_hash,
103
+ })
104
+ filtered_env = @request.filtered_env
105
+ filtered_env.sort_by { |key, _| key.to_s }.each do |key, value|
106
+ exception_details[key] = value
107
+ end
108
+ end
109
+
110
+ exception_details.merge!({
111
+ data: @data,
112
+ })
113
+
114
+ exception_details
115
+ end
116
+
117
+ class MissingController
118
+ def controller_name
119
+ '[NO CONTROLLER]'
120
+ end
121
+ def action_name
122
+ '[NO CONTROLLER]'
123
+ end
124
+ def method_missing(*args, &block)
125
+ end
126
+ end
127
+
128
+ def set_data_variables
129
+ @data.each do |name, value|
130
+ instance_variable_set("@#{name}", value)
131
+ end
132
+ end
133
+
134
+ def deep_encode(obj)
135
+ case obj
136
+ when Array
137
+ obj.map { |o| deep_encode(o) }
138
+ when Hash
139
+ deep_encode(obj.to_a).to_h
140
+ when String
141
+ encode_to_utf8(obj)
142
+ else
143
+ obj.respond_to?(:to_s) ? encode_to_utf8(obj.to_s) : '[NOT ENCODABLE]'
144
+ end
145
+ end
146
+
147
+ def encode_to_utf8(str)
148
+ quick_sanitization(str).encode(::Encoding.find('UTF-8'), {invalid: :replace, undef: :replace, replace: '?'})
149
+ end
150
+
151
+ def quick_sanitization(str) # stringify any random objects in a safe (and convenient) manner
152
+ str.inspect.gsub(/\A\"(.*)\"\z/,'\1')
153
+ end
154
+ end
155
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exception_file_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Koji Onishi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-23 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
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
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '6'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '4.0'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '6'
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: exception_notification
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '4.0'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '4.0'
103
+ description: " Exception File Notifier records exception logs in JSON format, helping
104
+ you track errors in the production environment. "
105
+ email:
106
+ - fursich0@gmail.com
107
+ executables: []
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - ".travis.yml"
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - bin/console
119
+ - bin/setup
120
+ - exception_file_notifier.gemspec
121
+ - lib/exception_file_notifier.rb
122
+ - lib/exception_notifier/exception_file_notifier/version.rb
123
+ - lib/exception_notifier/file_notifier.rb
124
+ homepage: https://github.com/fursich/exception_file_notifier
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '2.1'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: 1.8.11
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.5.1
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: A custom notifier for ExceptionNotification that generates exception log
148
+ files in JSON format.
149
+ test_files: []