log_error_handler 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1aec30724757e1105c92482736908fc96ce99d1e
4
+ data.tar.gz: d6b9aa4f31ac86d2ec87fd38638a09aee104922b
5
+ SHA512:
6
+ metadata.gz: 82311d2f84ba62d7f1e658dd5d3fe9d13d552e9e3d705ab80a9c055039a0ea0f2f5ef67115146265bb05011d2bfa4678682b885a846187c05af066c01e5bf121
7
+ data.tar.gz: 0079d04ef4423ddd5b67a8162209b4f5a43f3b7f87415cdfcfc99a495133d451a83387481c185fef23678e2ff5b6ee0b9db05d6a8401db279aafe08d1d87e31e
data/.gitignore ADDED
@@ -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
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Documentation:
4
+ Enabled: false
5
+ AllCops:
6
+ Exclude:
7
+ - 'db/*.rb'
8
+ Metrics/MethodLength:
9
+ Max: 20
10
+ Metrics/AbcSize:
11
+ Max: 30
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -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 gashuk95@gmail.com. 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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in log_error_handler.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # LogErrorHandler
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/log_error_handler`. 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 'log_error_handler'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install log_error_handler
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/[USERNAME]/log_error_handler. 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.
36
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'log_error_handler'
5
+ require 'pry'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require_relative '../lib/log_error_handler'
5
+ require 'optparse'
6
+
7
+ options = {}
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Library for tracking logs and send notification, when error finded\n" \
10
+ "Usage: your_program_with_standard_stdout | log_error_handler\n" \
11
+ "OR: log_error_handler -l path_to_your_logfile\n\n\n"
12
+
13
+ opts.on('-e', '--error_regexp=ERROR_REGEXP', Regexp, 'Defined regexp for detecting error.') do |ergxp|
14
+ options[:error_regexp] = ergxp
15
+ end
16
+
17
+ opts.on('-t', '--tid_regexp=ERROR_REGEXP', Regexp, 'Defined regexp for detecting thread id.') do |tidrgxp|
18
+ options[:tid_regexp] = tidrgxp
19
+ end
20
+
21
+ opts.on(
22
+ '--not_modify_timeout=NOT_MODIFY_TIMEOUT',
23
+ 'Defined waiting timeout in seconds after last modifying, before close file.'
24
+ ) do |t|
25
+ options[:not_modify_timeout] = t.to_i
26
+ end
27
+
28
+ opts.on(
29
+ '--log_file_tracker_waiting=LOG_FILE_TRACKER_WAITING',
30
+ 'Defined timeout in seconds before new cycle to review writed logfiles.'
31
+ ) do |t|
32
+ options[:log_file_tracker_waiting] = t.to_i
33
+ end
34
+
35
+ opts.on('--default', 'Show default config options') do
36
+ LogErrorHandler::Tracker.new.options.each do |key, value|
37
+ puts "#{key}: #{value}"
38
+ end
39
+ exit
40
+ end
41
+
42
+ opts.on('-f', '--file=FILE', 'Path to output file') do |path|
43
+ options[:file] = path
44
+ end
45
+
46
+ opts.on('-u', '--url=URL', 'URL for send error messages') do |url|
47
+ options[:uri] = URI(url)
48
+ end
49
+
50
+ opts.on('-m', '--http_method=HTTP_METHOD', 'HTTP method for send error messages') do |http_method|
51
+ options[:http_method] = http_method
52
+ end
53
+
54
+ opts.on('-a', '--additional_params=PARAMS', 'Additional params for send error message.') do |hash|
55
+ options[:additional_params] = JSON.parse(hash)
56
+ end
57
+
58
+ opts.on('-k', '--key=KEY', 'Key for send error messages') do |key|
59
+ options[:error_message_key] = key
60
+ end
61
+
62
+ opts.on('-d', '--daemonize', 'Daemonize') do
63
+ Process.daemon(true, true)
64
+ end
65
+
66
+ opts.on(
67
+ '--additional_params_from_file=ADDITIONAL_PARAMS_FROM_FILE',
68
+ 'Additional params from json file for send error message.'
69
+ ) do |path|
70
+ options[:additional_params] = JSON.parse(File.open(path, &:read))
71
+ end
72
+
73
+ opts.on('-l', '--log_file=LOG_FILE', 'Path to your log file') do |path|
74
+ options[:log_file] = path
75
+ end
76
+
77
+ opts.on_tail('-h', '--help', 'Show this message') do
78
+ puts opts
79
+ exit
80
+ end
81
+ end.parse!
82
+
83
+ LogErrorHandler::Tracker.start(options)
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
@@ -0,0 +1,18 @@
1
+ module LogErrorHandler
2
+ class HttpOut
3
+ def initialize(opts)
4
+ @options = opts
5
+ @req = Net::HTTP.const_get(@options[:http_method].capitalize).new(@options[:uri])
6
+ end
7
+
8
+ def puts(message)
9
+ data = @options[:additional_params] || {}
10
+ data[@options[:error_message_key]] = Base64.encode64(message)
11
+ @req.set_form_data(data)
12
+ Net::HTTP.new(@options[:uri].hostname, @options[:uri].port).request(@req)
13
+ end
14
+
15
+ def close
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ module LogErrorHandler
2
+ class LogFileTracker
3
+ attr_reader :thread
4
+
5
+ def initialize(tracker)
6
+ @tracker = tracker
7
+ end
8
+
9
+ def self.start(tracker)
10
+ log_file_tracker = new(tracker)
11
+ log_file_tracker.start
12
+ log_file_tracker
13
+ end
14
+
15
+ def start
16
+ @thread = Thread.new { log_file_tracker }
17
+ end
18
+
19
+ private
20
+
21
+ def log_file_tracker
22
+ loop do
23
+ sleep @tracker.options[:log_file_tracker_waiting]
24
+
25
+ @tracker.mutex.lock
26
+ @tracker.tracking_logs.each do |key, value|
27
+ next if Time.now.to_i - value[:timestamp] <= @tracker.options[:not_modify_timeout]
28
+ if value[:status] == :error
29
+ value[:file].rewind
30
+ @tracker.out.puts value[:file].read
31
+ end
32
+ value[:file].close
33
+ value[:file].unlink
34
+ @tracker.tracking_logs.delete(key)
35
+ end
36
+ @tracker.mutex.unlock
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ module LogErrorHandler
2
+ class OutFactory
3
+ def self.retrieve(opts)
4
+ if opts[:file]
5
+ File.new(opts[:file], 'w')
6
+ elsif opts[:uri]
7
+ HttpOut.new(opts)
8
+ else
9
+ $stdout
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ module LogErrorHandler
2
+ class StdinReader
3
+ TMP_DIR_NAME = 'log_error_handler'.freeze
4
+
5
+ def initialize(tracker)
6
+ @tracker = tracker
7
+ end
8
+
9
+ def self.start(tracker)
10
+ stdin_reader = new(tracker)
11
+ stdin_reader.start
12
+ stdin_reader
13
+ end
14
+
15
+ def start
16
+ Dir.mkdir("/tmp/#{TMP_DIR_NAME}") unless Dir.exist?("/tmp/#{TMP_DIR_NAME}")
17
+ $stdin.each do |line|
18
+ tid = line[@tracker.options[:tid_regexp]]
19
+ @last_tid = tid || @last_tid
20
+ write_to_file(line) if @last_tid
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def write_to_file(line)
27
+ @tracker.mutex.lock
28
+ @tracker.tracking_logs[@last_tid] ||= {
29
+ file: Tempfile.new("log_error_handler/#{@last_tid}"),
30
+ status: :ok,
31
+ timestamp: Time.now.to_i
32
+ }
33
+ @tracker.mutex.unlock
34
+
35
+ text = line.sub(@tracker.options[:tid_regexp], '')
36
+ @tracker.tracking_logs[@last_tid][:file].write(text)
37
+ @tracker.tracking_logs[@last_tid][:status] = :error if text[@tracker.options[:error_regexp]]
38
+ @tracker.tracking_logs[@last_tid][:timestamp] = Time.now.to_i
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module LogErrorHandler
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,66 @@
1
+ require_relative 'log_error_handler/version'
2
+ require 'tempfile'
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'base64'
6
+ require_relative 'log_error_handler/log_file_tracker'
7
+ require_relative 'log_error_handler/stdin_reader'
8
+ require_relative 'log_error_handler/http_out'
9
+ require_relative 'log_error_handler/out_factory'
10
+
11
+ module LogErrorHandler
12
+ class Tracker
13
+ attr_accessor :tracking_logs
14
+ attr_reader :options, :out, :mutex
15
+
16
+ DEFAULT_OPTIONS = {
17
+ error_regexp: /500.*error/i,
18
+ tid_regexp: /^\[\d+\]/,
19
+ not_modify_timeout: 3,
20
+ log_file_tracker_waiting: 300,
21
+ http_method: :post,
22
+ error_message_key: :message
23
+ }.freeze
24
+
25
+ def initialize(opts = {})
26
+ @tracking_logs = {}
27
+ @options = DEFAULT_OPTIONS.merge(opts)
28
+ @out = OutFactory.retrieve(@options)
29
+ @mutex = Mutex.new
30
+ end
31
+
32
+ def self.start(opts = {})
33
+ tracker = new(opts)
34
+ if opts[:log_file]
35
+ IO.popen("tail -f #{opts[:log_file]}") do |stdin|
36
+ $stdin = stdin
37
+ tracker.start
38
+ end
39
+ else
40
+ tracker.start
41
+ end
42
+
43
+ tracker
44
+ end
45
+
46
+ def start
47
+ @log_file_tracker = LogFileTracker.start(self)
48
+ StdinReader.start(self)
49
+ ensure
50
+ close_all_files
51
+ end
52
+
53
+ private
54
+
55
+ def close_all_files
56
+ @log_file_tracker.thread.kill
57
+ @tracking_logs.each do |_key, value|
58
+ value[:file].rewind
59
+ @out.puts value[:file].read if value[:status] == :error
60
+ value[:file].close
61
+ value[:file].unlink
62
+ @out.close
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'log_error_handler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'log_error_handler'
8
+ spec.version = LogErrorHandler::VERSION
9
+ spec.authors = ['Thinkmobiles']
10
+ spec.email = ['gashuk95@gmail.com']
11
+
12
+ spec.summary = 'Gem for notification when logs have any errors.'
13
+ spec.description = 'Simple gem for server error notification. This gem use only logfile, and not depends on any languages'
14
+ spec.homepage = 'https://github.com/thinkmobiles/log_error_handler'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = 'bin'
28
+ spec.executables = ['log_error_handler']
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'bundler', '~> 1.14'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'pry', '~> 0.10.4'
35
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log_error_handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thinkmobiles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-22 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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
69
+ description: Simple gem for server error notification. This gem use only logfile,
70
+ and not depends on any languages
71
+ email:
72
+ - gashuk95@gmail.com
73
+ executables:
74
+ - log_error_handler
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".rubocop.yml"
81
+ - ".travis.yml"
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/log_error_handler
88
+ - bin/setup
89
+ - lib/log_error_handler.rb
90
+ - lib/log_error_handler/http_out.rb
91
+ - lib/log_error_handler/log_file_tracker.rb
92
+ - lib/log_error_handler/out_factory.rb
93
+ - lib/log_error_handler/stdin_reader.rb
94
+ - lib/log_error_handler/version.rb
95
+ - log_error_handler.gemspec
96
+ homepage: https://github.com/thinkmobiles/log_error_handler
97
+ licenses: []
98
+ metadata:
99
+ allowed_push_host: https://rubygems.org
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.5.1
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Gem for notification when logs have any errors.
120
+ test_files: []