kybus-logger 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/kybus/log_decorator.rb +14 -0
- data/lib/kybus/logger.rb +3 -0
- data/lib/kybus/logger/config.rb +61 -0
- data/lib/kybus/logger/format.rb +35 -0
- data/lib/kybus/logger/log_methods.rb +66 -0
- data/lib/kybus/logger/version.rb +7 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 39fcb6ac69876e64bdf175f357c8602b3b24ddff49c6d31cc4158409641ccf93
|
4
|
+
data.tar.gz: 562466fb26205e9a85646b94554a8b04e82607a22767eceb180513c346c0e3f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c6f800d57771c31f8a931f5501886c599cef32b05cb450d46771a8b47cada9050ae548d44e4c664a3a449eb3c0980cd8ada24f336799f1432ff1f684d74bccf
|
7
|
+
data.tar.gz: e7514e3f9d425a41cd633a86e41c4ae51f6ccd274c43b031decdac894d5deb07966a9022ac1789bf4e39cf78ec33d5c3199bf8af0e928ba6f2e6092b494066dd
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'kybus/core'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require_relative 'logger/config'
|
7
|
+
require_relative 'logger/format'
|
8
|
+
require_relative 'logger/log_methods'
|
9
|
+
|
10
|
+
module Kybus
|
11
|
+
module Logger
|
12
|
+
include LogMethods
|
13
|
+
end
|
14
|
+
end
|
data/lib/kybus/logger.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
require_relative 'format'
|
6
|
+
|
7
|
+
module Kybus
|
8
|
+
module Logger
|
9
|
+
class Config
|
10
|
+
include Kybus::DRY::ResourceInjector
|
11
|
+
include Kybus::Logger::Format
|
12
|
+
SEVERITIES = {
|
13
|
+
'debug' => ::Logger::DEBUG,
|
14
|
+
'info' => ::Logger::INFO,
|
15
|
+
'warn' => ::Logger::WARN,
|
16
|
+
'error' => ::Logger::ERROR,
|
17
|
+
'fatal' => ::Logger::FATAL
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
def self.from_config(config)
|
21
|
+
new(config)
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(config)
|
25
|
+
@original_config = config
|
26
|
+
register('file', config['stdout'] ? STDOUT : config['file'] || 'application.log')
|
27
|
+
register('rotate_days', config['rotate_days'] || 7)
|
28
|
+
register('rotate_size', config['rotate_size'] || 100 * 1024**2) # 100Mb
|
29
|
+
register('date_format', config['date_format'] || '%Y-%m-%d %H:%M:%S')
|
30
|
+
register('severity', config['severity'] || 'info')
|
31
|
+
register('log_format', config['log_format'] ||
|
32
|
+
"%time,%sev,%pid,%tid,%mod,%json\n")
|
33
|
+
register('blacklist', config['blacklist'] || %w[pass password])
|
34
|
+
register('logger', logger)
|
35
|
+
end
|
36
|
+
|
37
|
+
def logger
|
38
|
+
@logger ||= begin
|
39
|
+
logger = ::Logger.new(
|
40
|
+
resource('file'),
|
41
|
+
resource('rotate_days'),
|
42
|
+
resource('rotate_size')
|
43
|
+
)
|
44
|
+
$stdout.sync = true if @original_config['stdout']
|
45
|
+
logger.sev_threshold = SEVERITIES[resource('severity')]
|
46
|
+
logger.datetime_format = resource('date_format')
|
47
|
+
logger.formatter = format_builder(resource('log_format'))
|
48
|
+
logger
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def merge_params(msg, data, debug)
|
53
|
+
blacklist = resource('blacklist') + ['debug']
|
54
|
+
params = {}
|
55
|
+
data.each { |k, v| params[k] = v unless blacklist.include?(k.to_s) }
|
56
|
+
params[:debug] = debug if resource('severity') == 'debug'
|
57
|
+
[msg, params]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kybus
|
4
|
+
module Logger
|
5
|
+
module Format
|
6
|
+
# Format params:
|
7
|
+
# - %time
|
8
|
+
# - %sev
|
9
|
+
# - %pid
|
10
|
+
# - %tid
|
11
|
+
# - %module: won't work well on singleton classes. Prints the name of the
|
12
|
+
# invoking module
|
13
|
+
# - %json: Data object encoded as json
|
14
|
+
# default format:
|
15
|
+
# "%time,%sev,%pid,%tid,%module,%json"
|
16
|
+
def format_builder(format_string)
|
17
|
+
proc do |frmt, sev, time, mod, data|
|
18
|
+
meta = {
|
19
|
+
'%sev': sev,
|
20
|
+
'%time': time,
|
21
|
+
'%mod': mod,
|
22
|
+
'%pid': Process.pid.to_s(16),
|
23
|
+
'%tid': Thread.current.object_id.to_s(16)
|
24
|
+
}
|
25
|
+
entry = frmt.dup
|
26
|
+
meta.each { |k, v| entry.gsub!(k.to_s, v.to_s) }
|
27
|
+
# json is slower than the meta records, so it is better to replace
|
28
|
+
# in a lazy way.
|
29
|
+
# TODO: Move lazy tags to a pluggin-based model.
|
30
|
+
entry.gsub('%json', data.to_json) if frmt.include?('%json')
|
31
|
+
end.curry.call(format_string)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kybus
|
4
|
+
module Logger
|
5
|
+
module LogMethods
|
6
|
+
extend Kybus::DRY::ResourceInjector
|
7
|
+
|
8
|
+
def self.global_config
|
9
|
+
LogMethods.resource(:global_config)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.global_config=(config)
|
13
|
+
LogMethods.register(:global_config, config)
|
14
|
+
end
|
15
|
+
|
16
|
+
def log_info(msg, data = {})
|
17
|
+
log_raw(:info, msg, data, data[:debug])
|
18
|
+
end
|
19
|
+
|
20
|
+
def log_warn(msg, data = {})
|
21
|
+
log_raw(:warn, msg, data, data[:debug])
|
22
|
+
end
|
23
|
+
|
24
|
+
def log_error(msg, data = {})
|
25
|
+
log_raw(:error, msg, data, data[:debug])
|
26
|
+
end
|
27
|
+
|
28
|
+
def log_fatal(msg, data = {})
|
29
|
+
log_raw(:fatal, msg, data, data[:debug])
|
30
|
+
end
|
31
|
+
|
32
|
+
def log_debug(msg, data = {})
|
33
|
+
log_raw(:debug, msg, data, data[:debug])
|
34
|
+
end
|
35
|
+
|
36
|
+
def log_metric(metric:, amount:, group: nil, time: nil)
|
37
|
+
log_info('Metric', metric: metric, value: amount,
|
38
|
+
group: group, time: time)
|
39
|
+
end
|
40
|
+
|
41
|
+
def log_alert(description:, group:, alert_severity:, notify_group:)
|
42
|
+
log_fatal('Alert Triggered',
|
43
|
+
description: description,
|
44
|
+
group: group,
|
45
|
+
severity: alert_severity,
|
46
|
+
notify_group: notify_group)
|
47
|
+
end
|
48
|
+
|
49
|
+
def log_raw(severity, msg, data, debug = {})
|
50
|
+
sev = Config::SEVERITIES[severity.to_s]
|
51
|
+
meta = config.merge_params(msg, data, debug)
|
52
|
+
config.logger.add(sev, meta, self.class.name)
|
53
|
+
end
|
54
|
+
|
55
|
+
def config
|
56
|
+
self.class.respond_to?(:resource) ? self.class.resource(:log_config) : LogMethods.global_config
|
57
|
+
rescue StandardError => _e
|
58
|
+
# :nocov:
|
59
|
+
Kybus::Logger::LogMethods.global_config
|
60
|
+
# :nocov:
|
61
|
+
end
|
62
|
+
|
63
|
+
LogMethods.global_config = Config.from_config({})
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kybus-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gilberto Vargas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: kybus-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mocha
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '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'
|
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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rdoc
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Make logs formatter and handling easier
|
112
|
+
email:
|
113
|
+
- tachoguitar@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- lib/kybus/log_decorator.rb
|
119
|
+
- lib/kybus/logger.rb
|
120
|
+
- lib/kybus/logger/config.rb
|
121
|
+
- lib/kybus/logger/format.rb
|
122
|
+
- lib/kybus/logger/log_methods.rb
|
123
|
+
- lib/kybus/logger/version.rb
|
124
|
+
homepage: https://github.com/tachomex/kybus
|
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: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubygems_version: 3.1.4
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: Implements a kybus-logger with a format based on cute_logger
|
147
|
+
test_files: []
|