hot_catch 0.1.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c2d259461a59b9e380e8d585b9f18983a8ac82a
4
- data.tar.gz: 8518d122dd47d19b6a446c847839cb67e5014710
3
+ metadata.gz: 9c3ffc6c97a1c071dd500eb5bf63d32525484983
4
+ data.tar.gz: 3ab01fd9408f014fe245dfbfba5cc75d83b372e5
5
5
  SHA512:
6
- metadata.gz: e105f28810b3fbf3473c23c81f00bb5a1604c9685aaaa4e9becaf280958b8d98bf57edb24dfc1055f4ff5628012412c2a35db39eb8bbb45d69794550cc834c79
7
- data.tar.gz: 6ab6e9ce8f7f34da6fddf433a39640c27668fed432349f365086e25f819113d8e5d18b75c10f15af8ff83745bc948b8213a0ea9c8780ca072f57db0348d13533
6
+ metadata.gz: 51f63b5bd8658ac2feed968223acecf87264c1d7dd2b02b095f53ae24d2ffd82655a4ea7f73d6e6d4e126bb5c943f61537d1beef7b572aa088d1559fe3e7dd24
7
+ data.tar.gz: d37489e27e967197f739988a0418c706d549f5fb55f9f3e4b015903668593db468d3ee678cbdec022558af476de5fcd5bc99913e381a007adb9f08c9057a3572
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # HotCatch
2
- Данный гем позволяет изменить логирование в базу данных.
3
- В дальнейшем будет отправлять логи главному приложению.
2
+ Данный гем позволяет изменить логирование в базу данных и посылать
3
+ запросы на главный сервер.
4
4
 
5
5
  ## Использование
6
6
  Логи можно посмотреть в консоли:
@@ -9,7 +9,10 @@
9
9
  > HotCatchLog.first.log_data
10
10
  ```
11
11
  В данной реализации логи разделены по статусам запросов (поле `status HotCatchLog`).
12
- Кроме того одинаковые ошибки собираются в одну, при этом увеличивается счётчик в моделе (поле `count_log HotCatchLog`).
12
+ Кроме того одинаковые ошибки собираются в одну, при этом увеличивается счётчик в моделе `HotCatchLog` поле `count_log`.
13
+
14
+ Ответы от сервера, куда отправляются логи, доступен в файле `log/hot_catch_log_response_errors`.
15
+ Если всё порядке - этого файла нет или он пуст.
13
16
 
14
17
  ## Установка
15
18
  1. Добавить гем в `Gemfile`
@@ -20,6 +23,8 @@ gem 'hot_catch'
20
23
  3. Запустить генератор установки: `$ rails generate hot_catch:install`
21
24
  Для удаления: `$ rails generate hot_catch:uninstall`
22
25
  4. `$ rails db:migrate`
26
+ 5. Изменить в `config/hot_catch_config.json` url - адрес сервера для отправки лог файлов.
27
+ Автоматически запросы посылаются на адрес *url/main_hot_catch_logs*, но нужно указать только *url*.
23
28
 
24
29
  Это всё! Теперь все логи формируются в базе данных.
25
30
 
@@ -41,17 +41,29 @@ RUBY
41
41
  end
42
42
 
43
43
  def change_files
44
- insert_into_file "config/application.rb", "\nrequire_relative 'hot_catch_logger'\n", :before => "module ExampleApp"
44
+ insert_into_file "config/application.rb", "\nrequire_relative 'hot_catch_logger'\n", :before => "module"
45
45
 
46
46
  application "config.middleware.insert_before Rails::Rack::Logger, Rails::Rack::HotCatchLogger\n
47
47
  config.middleware.delete Rails::Rack::Logger"
48
48
 
49
49
  insert_string = "\n require 'hot_catch/custom_log_subscribers.rb'
50
- config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new('hot_catch_buf_file'))\n"
50
+ config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new('tmp/hot_catch_buf_file'))\n"
51
51
 
52
52
  insert_into_file "config/environments/development.rb", insert_string, :after => "Rails.application.configure do"
53
53
  insert_into_file "config/environments/production.rb", insert_string, :after => "Rails.application.configure do"
54
54
  end
55
55
 
56
+ def add_hot_catch_config
57
+ create_file "config/hot_catch_config.json" do
58
+ "/* http://[url] */
59
+ {\"url\":\"[your_server_url]\"}"
60
+ end
61
+ end
62
+
63
+ def add_initialize_file_for_sender_logs
64
+ create_file "config/initializers/hot_catch_sender_logs.rb" do
65
+ "Rails.application.config.sender_logs = HotCatch::MakeHttpsRequest.new"
66
+ end
67
+ end
56
68
  end
57
69
  end
@@ -23,6 +23,18 @@ module HotCatch
23
23
  gsub_file "config/environments/production.rb", /.*\s.*hot_catch_buf_file'\)\)/, ""
24
24
  end
25
25
 
26
+ def remove_log_files
27
+ remove_file "tmp/hot_catch_buf_file"
28
+ remove_file "tmp/hot_catch_buf_file2"
29
+ end
30
+
31
+ def remove_hot_catch_config
32
+ remove_file "config/hot_catch_config.json"
33
+ end
34
+
35
+ def remove_initialize_file_for_sender_logs
36
+ remove_file "config/initializers/hot_catch_sender_logs.rb"
37
+ end
26
38
 
27
39
  end
28
40
  end
@@ -1,3 +1,75 @@
1
1
  module HotCatch
2
2
  require 'hot_catch/custom_log_subscribers.rb'
3
+ require 'uri'
4
+ require 'net/https'
5
+ require 'json'
6
+
7
+ # ======================================================================
8
+ # Для использования нужно:
9
+ # 1. Править файл hot_catch_config.json для указания url нужного сервера
10
+ # 2. sender_logs = HotCatch::MakeHttpsRequest.new
11
+ # 3. Сформировать сообщение на примере:
12
+ # ====================================
13
+ # body_log = {main_hot_catch_log: {
14
+ # "log_data":"some message",
15
+ # "name_app":"new6_app",
16
+ # "count_log":10,
17
+ # "id_log_origin_app":1,
18
+ # "from_log":"Rails",
19
+ # "status":"SERVER_ERROR"}
20
+ # }
21
+ # ====================================
22
+ # 4. sender_logs.send_log(body_log)
23
+ # 5. Ошибки доступны в файле hot_catch_log_response_errors
24
+ # ======================================================================
25
+
26
+ class MakeHttpsRequest
27
+ def initialize
28
+ @url = ""
29
+ File.open("config/hot_catch_config.json"){ |file| @url = JSON.parse(file.read)["url"] }
30
+ @url += "/main_hot_catch_logs"
31
+ end
32
+
33
+ def rails_g_log(log_data, count_log, id_log, status)
34
+ send_log(
35
+ {main_hot_catch_log:
36
+ {
37
+ "log_data":log_data,
38
+ "name_app":Rails.application.class.parent_name,
39
+ "count_log":count_log,
40
+ "id_log_origin_app":id_log,
41
+ "from_log":"Rails",
42
+ "status":status
43
+ }
44
+ }
45
+ )
46
+ end
47
+
48
+ def send_log(body_log)
49
+ response = call(@url, body_log)
50
+ # логируется, если на запрос пришёл какой-либо ответ
51
+ unless response.empty?
52
+ str = "\n" + (?-*20) + "\n#{Time.now}\nlogs:\n#{body_log.to_s}\nresponse\n:#{response}\n" + (?-*20) + "\n"
53
+ File.open("log/hot_catch_log_response_errors", 'a'){ |file| file.write str }
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def call(url, hash_json)
60
+ uri = URI.parse(url)
61
+ req = Net::HTTP::Post.new(uri.to_s)
62
+ req.body = hash_json.to_json
63
+ req['Content-Type'] = 'application/json'
64
+ response = https(uri).request(req)
65
+ response.body
66
+ end
67
+
68
+ def https(uri)
69
+ Net::HTTP.new(uri.host, uri.port).tap do |http|
70
+ # http.use_ssl = true
71
+ # http.verify_mode = OpenSSL::SSL::VERIFY_NONE
72
+ end
73
+ end
74
+ end
3
75
  end
@@ -50,7 +50,7 @@ class CustomActionControllerLogSubscriber < ActionController::LogSubscriber
50
50
  status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
51
51
  end
52
52
 
53
- File.open('hot_catch_buf_file', 'a'){ |file| file.puts "!!!#{status}!!!" }
53
+ File.open('tmp/hot_catch_buf_file', 'a'){ |file| file.puts "!!!#{status}!!!" }
54
54
 
55
55
  info do
56
56
 
@@ -19,13 +19,23 @@ class HotCatchLog < ApplicationRecord
19
19
  if strip_str(cur_log.log_data) == str2
20
20
  cur_log.count_log += 1
21
21
  cur_log.save
22
+
23
+ # Отправка лога на сервер
24
+ Rails.application.config.sender_logs.rails_g_log(cur_log.log_data,
25
+ cur_log.count_log, cur_log.id, cur_log.status)
26
+
22
27
  opt = false
23
28
  return
24
29
  end
25
30
  end
26
31
 
27
32
  if opt && str.strip.present? && status.present?
28
- HotCatchLog.create(log_data: format_log(str), status: status)
33
+ l = HotCatchLog.create(log_data: format_log(str), status: status)
34
+
35
+ # Отправка лога на сервер
36
+ # args: log_data, count_log, id_log, status
37
+ Rails.application.config.sender_logs.rails_g_log(l.log_data, l.count_log,
38
+ l.id, l.status)
29
39
  end
30
40
 
31
41
  end
@@ -82,14 +82,14 @@ module Rails
82
82
 
83
83
  # Считали данные из основного лог-файла
84
84
  logs = ""
85
- File.open('hot_catch_buf_file'){ |file| logs = file.read}
86
- File.open('hot_catch_buf_file', 'w'){ |file| file.print "" }
85
+ File.open('tmp/hot_catch_buf_file'){ |file| logs = file.read}
86
+ File.open('tmp/hot_catch_buf_file', 'w'){ |file| file.print "" }
87
87
  logs = logs.split("BEGIN").delete_if{|x| !x.present? }
88
88
 
89
89
  # Считали данные из дополнительного лог-файла и записали начало последнего лога
90
90
  add_log = ""
91
- File.open('hot_catch_buf_file2'){ |file| add_log = file.read} if File.exist?('hot_catch_buf_file2')
92
- File.open('hot_catch_buf_file2', 'w'){ |file| file.print logs[-1] }
91
+ File.open('tmp/hot_catch_buf_file2'){ |file| add_log = file.read} if File.exist?('tmp/hot_catch_buf_file2')
92
+ File.open('tmp/hot_catch_buf_file2', 'w'){ |file| file.print logs[-1] }
93
93
  logs.delete_at(-1)
94
94
 
95
95
  # Собираем начало лога и конец
@@ -1,3 +1,3 @@
1
1
  module HotCatch
2
- VERSION = '0.1.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hot_catch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Davhot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-09 00:00:00.000000000 Z
11
+ date: 2017-08-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Description of Testgem.
14
14
  email:
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  version: '0'
49
49
  requirements: []
50
50
  rubyforge_project:
51
- rubygems_version: 2.5.1
51
+ rubygems_version: 2.6.12
52
52
  signing_key:
53
53
  specification_version: 4
54
54
  summary: Summary of Testgem.