loggun 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8815d61a25fc71e36d6648e2e65fd7d6d73ebffd56e0b4631c95e589a5b9ced4
4
- data.tar.gz: a6d4aea0a8971708fc3eb78eb42c50d99afb6fcbfec1e4dad5753c6552865566
3
+ metadata.gz: 3a8a4d4015105e9d55b653cab3305ab2b1ddeeda0e1429bd5e2449c5841771fe
4
+ data.tar.gz: e6a84389338458849f87dfb2ffcdea13403537553fa547b170c4debb5901f433
5
5
  SHA512:
6
- metadata.gz: aa4ee9f1f9d4a2fe3dae09083a9554c30d28d3821a5de8230db81efeef204e36e075c79335d4e7b587ead7a20da17cdf81b695acd6f00d27c751109ea2753902
7
- data.tar.gz: 52768e02e7021307b455d5c5b7e2a55cda78152e61d387a2eff6cb1878060ea9b885d09b09c6b5f3c586eb9d33a0e8b6724e2827769f98d8c241f69920d16034
6
+ metadata.gz: f10eb810cb421dde460b1c97c777496447d22197dfdbf35bf6eae341a1b9b2a6270044135887a062882c0f8bf4ccc21efbfe3231598ee7f060ca5be220385a6f
7
+ data.tar.gz: 3362704777d4d6814ba58e1a72aee945d5f1f8105d03ff20930e3ecd02d559d5878f90681a77aefd14231aef67dbb79b41cd1883a5cbc6e54a8fa3817c1a49c9
data/README.md CHANGED
@@ -92,6 +92,16 @@ end
92
92
  `error_info` - лямбда, позволяющая добавить в лог информацию об ошибке, содержащейся в неуспешном ответе экшена.
93
93
  Например `-> { JSON.parse(response.body)['error_code'] }`
94
94
 
95
+ Для Rails 6 и выше данный модификатор может работать некорректно.
96
+ В этом случае можно указать в требуемом базовом контроллере строку:
97
+ ```ruby
98
+ include Loggun::HttpHelpers
99
+ ```
100
+ Это делает настройки `enable` и `controllers` модификатора безсполезными,
101
+ однако позволяет гарантированно логировать входящие http запросы.
102
+
103
+ Настройки `success_condition` и `error_info` продолжают использоваться и могу быть установлены требуемым образом.
104
+
95
105
  Помимо указанных модификаторов существует возможность добавить собственный.
96
106
  Необходимо уснаследовать его от `Loggun::Modifiers::Base` и указать в методе `apply` все необходимые действия.
97
107
  ```ruby
@@ -5,6 +5,8 @@ require 'loggun/config'
5
5
  require 'loggun/modifiers'
6
6
  require 'loggun/modifiers/base'
7
7
  require 'loggun/helpers'
8
+ require 'loggun/http_helpers'
9
+ require 'logger'
8
10
 
9
11
  module Loggun
10
12
  class Error < StandardError; end
@@ -12,10 +14,22 @@ module Loggun
12
14
  class << self
13
15
  include Loggun::Helpers
14
16
 
15
- attr_accessor :application
17
+ attr_writer :logger
16
18
 
17
19
  %i[unknown fatal error warn info debug].each do |method|
18
20
  alias_method method, "log_#{method}"
19
21
  end
22
+
23
+ def logger
24
+ @logger ||= default_logger
25
+ end
26
+
27
+ private
28
+
29
+ def default_logger
30
+ logger = Logger.new(STDOUT)
31
+ logger.formatter = Config.instance.formatter
32
+ logger
33
+ end
20
34
  end
21
35
  end
@@ -56,8 +56,8 @@ module Loggun
56
56
  end
57
57
 
58
58
  def setup_formatter(app)
59
- Loggun.application = app
60
- Loggun.application.logger.formatter = instance.formatter
59
+ Loggun.logger = app.logger
60
+ Loggun.logger.formatter = instance.formatter
61
61
  end
62
62
  end
63
63
 
@@ -1,4 +1,5 @@
1
- require 'date'
1
+ require 'time'
2
+ require 'json'
2
3
 
3
4
  module Loggun
4
5
  class Formatter
@@ -17,13 +18,14 @@ module Loggun
17
18
  end
18
19
 
19
20
  data[:message] = message.to_s.tr("\r\n", ' ').strip
20
- data[:severity] = severity&.present? ? severity.to_s : 'INFO'
21
+ data[:severity] = severity&.to_s || 'INFO'
21
22
  data[:tags_text] = tags_text
22
23
  data[:type] = Loggun.type || DEFAULT_VALUE.dup
23
24
  data[:transaction_id] = Loggun.transaction_id
24
25
  data[:parent_transaction] = parent_transaction if parent_transaction
25
26
 
26
- if data[:transaction_id]&.to_i != Process.pid && data[:type] != DEFAULT_VALUE
27
+ if data[:transaction_id] && data[:type] != DEFAULT_VALUE &&
28
+ data[:transaction_id].to_i != Process.pid
27
29
  data[:type] = "#{data[:type]}##{data[:transaction_id]}"
28
30
  end
29
31
 
@@ -60,8 +60,8 @@ module Loggun
60
60
  %i[unknown fatal error warn info debug].each do |method|
61
61
  define_method("log_#{method}") do |*args, **attrs, &block|
62
62
  type = args.shift
63
- next application.logger.send(method, type, &block) if args.empty? &&
64
- attrs.empty?
63
+ next logger.send(method, type, &block) if args.empty? &&
64
+ attrs.empty?
65
65
 
66
66
  method_name = caller_locations.first.label.split(' ').last
67
67
  type = log_type(type, method_name)
@@ -78,10 +78,10 @@ module Loggun
78
78
  attrs[:hidden] = { error: { backtrace: error.backtrace } }
79
79
  end
80
80
  end
81
- attrs[:value] = args if args.present?
81
+ attrs[:value] = args unless args.empty?
82
82
 
83
83
  with_type(type) do
84
- application.logger.send(method, **attrs, &block)
84
+ logger.send(method, **attrs, &block)
85
85
  end
86
86
  end
87
87
  end
@@ -171,8 +171,8 @@ module Loggun
171
171
  "#{SecureRandom.uuid[0..7]}_#{DateTime.now.strftime('%Q')}"
172
172
  end
173
173
 
174
- def application
175
- Loggun.application
174
+ def logger
175
+ Loggun.logger
176
176
  end
177
177
  end
178
178
  end
@@ -0,0 +1,48 @@
1
+ module Loggun
2
+ module HttpHelpers
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ around_action :log_http_actions
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def log_http_actions
12
+ log_action :start
13
+ yield
14
+ log_action :response
15
+ rescue StandardError => e
16
+ log_action :response, fail: e
17
+ raise e
18
+ end
19
+
20
+ def log_action(action = :start, fail: nil)
21
+ api = request.path[/\w+/]
22
+ api_version = request.path[/v./]
23
+ type = "http_request.#{api}.#{action}"
24
+ data = { path: clean_pathname }
25
+ data[:api_version] = api_version if api_version
26
+
27
+ return Loggun.info type, data if action == :start
28
+
29
+ success = fail.nil? && instance_exec(&modifier_config.success_condition)
30
+ data[:success] = success
31
+ unless success
32
+ error = instance_exec(&modifier_config.error_info)
33
+ data[:error] = error if error
34
+ end
35
+ Loggun.info type, data
36
+ end
37
+
38
+ def clean_pathname
39
+ filtered_params = params.to_unsafe_h
40
+ filtered_params.delete('action')
41
+ request.path.gsub(/(#{filtered_params.values.join('|')})/, '').gsub(/\/api\/v./, '')
42
+ end
43
+
44
+ def modifier_config
45
+ Loggun::Config.instance.modifiers.incoming_http
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,3 @@
1
- require_relative 'incoming_http/log_http_actions'
2
-
3
1
  module Loggun
4
2
  module Modifiers
5
3
  class IncomingHttp < Loggun::Modifiers::Base
@@ -9,8 +7,7 @@ module Loggun
9
7
  controllers = Loggun::Config.instance.modifiers.incoming_http.controllers
10
8
  controllers.each do |controller|
11
9
  controller.constantize.class_eval do
12
- include LogHttpActions
13
- around_action :log_http_actions
10
+ include Loggun::HttpHelpers
14
11
  end
15
12
  end
16
13
  end
@@ -1,3 +1,3 @@
1
1
  module Loggun
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency 'clockwork', '~> 2.0'
33
33
  spec.add_development_dependency 'http', '~> 4.0'
34
34
  spec.add_development_dependency 'rails', '~> 6.0'
35
- spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
36
  spec.add_development_dependency 'rspec', '~> 3.0'
37
37
  spec.add_development_dependency 'rspec-rails', '~> 3.0'
38
38
  spec.add_development_dependency 'sidekiq', '~> 6.0'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loggun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksandr Noskov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-03-29 00:00:00.000000000 Z
12
+ date: 2020-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -73,14 +73,14 @@ dependencies:
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: '10.0'
76
+ version: '13.0'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: '10.0'
83
+ version: '13.0'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: rspec
86
86
  requirement: !ruby/object:Gem::Requirement
@@ -144,13 +144,13 @@ files:
144
144
  - lib/loggun/config.rb
145
145
  - lib/loggun/formatter.rb
146
146
  - lib/loggun/helpers.rb
147
+ - lib/loggun/http_helpers.rb
147
148
  - lib/loggun/modifiers.rb
148
149
  - lib/loggun/modifiers/base.rb
149
150
  - lib/loggun/modifiers/clockwork.rb
150
151
  - lib/loggun/modifiers/clockwork/manager.rb
151
152
  - lib/loggun/modifiers/clockwork/methods.rb
152
153
  - lib/loggun/modifiers/incoming_http.rb
153
- - lib/loggun/modifiers/incoming_http/log_http_actions.rb
154
154
  - lib/loggun/modifiers/outgoing_http.rb
155
155
  - lib/loggun/modifiers/outgoing_http/block_logger.rb
156
156
  - lib/loggun/modifiers/rails.rb
@@ -1,36 +0,0 @@
1
- module LogHttpActions
2
-
3
- private
4
-
5
- def log_http_actions
6
- log_action :start
7
- yield
8
- log_action :response
9
- end
10
-
11
- def log_action(action = :start)
12
- api = request.path[/\w+/]
13
- api_version = request.path[/v./]
14
- type = "http_request.#{api}.#{action}"
15
- data = { path: clean_pathname, api_version: api_version }
16
- return Loggun.info type, data if action == :start
17
-
18
- success = instance_exec(&modifier_config.success_condition)
19
- data[:success] = success
20
- unless success
21
- error = instance_exec(&modifier_config.error_info)
22
- data[:error] = error if error
23
- end
24
- Loggun.info type, data
25
- end
26
-
27
- def clean_pathname
28
- filtered_params = params.to_unsafe_h
29
- filtered_params.delete('action')
30
- request.path.gsub(/(#{filtered_params.values.join('|')})/, '').gsub(/\/api\/v./, '')
31
- end
32
-
33
- def modifier_config
34
- Loggun::Config.instance.modifiers.incoming_http
35
- end
36
- end