loggun 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +10 -0
- data/lib/loggun.rb +15 -1
- data/lib/loggun/config.rb +2 -2
- data/lib/loggun/formatter.rb +5 -3
- data/lib/loggun/helpers.rb +6 -6
- data/lib/loggun/http_helpers.rb +48 -0
- data/lib/loggun/modifiers/incoming_http.rb +1 -4
- data/lib/loggun/version.rb +1 -1
- data/loggun.gemspec +1 -1
- metadata +5 -5
- data/lib/loggun/modifiers/incoming_http/log_http_actions.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a8a4d4015105e9d55b653cab3305ab2b1ddeeda0e1429bd5e2449c5841771fe
|
4
|
+
data.tar.gz: e6a84389338458849f87dfb2ffcdea13403537553fa547b170c4debb5901f433
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/loggun.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/loggun/config.rb
CHANGED
data/lib/loggun/formatter.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require '
|
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&.
|
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]
|
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
|
|
data/lib/loggun/helpers.rb
CHANGED
@@ -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
|
64
|
-
|
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
|
81
|
+
attrs[:value] = args unless args.empty?
|
82
82
|
|
83
83
|
with_type(type) do
|
84
|
-
|
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
|
175
|
-
Loggun.
|
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
|
13
|
-
around_action :log_http_actions
|
10
|
+
include Loggun::HttpHelpers
|
14
11
|
end
|
15
12
|
end
|
16
13
|
end
|
data/lib/loggun/version.rb
CHANGED
data/loggun.gemspec
CHANGED
@@ -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', '~>
|
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.
|
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-
|
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: '
|
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: '
|
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
|