kybus-logger 0.2.2 → 0.3.0

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: 39fcb6ac69876e64bdf175f357c8602b3b24ddff49c6d31cc4158409641ccf93
4
- data.tar.gz: 562466fb26205e9a85646b94554a8b04e82607a22767eceb180513c346c0e3f0
3
+ metadata.gz: 7e8425c5c0e962f2a2e4a9d60d8e36c545d16c94c93f4c7a35059f13f6644b1a
4
+ data.tar.gz: 5a9cd41cb837ae50d22d08bb409cb77844ab8a105503b5203c96c1f56e1dce35
5
5
  SHA512:
6
- metadata.gz: 9c6f800d57771c31f8a931f5501886c599cef32b05cb450d46771a8b47cada9050ae548d44e4c664a3a449eb3c0980cd8ada24f336799f1432ff1f684d74bccf
7
- data.tar.gz: e7514e3f9d425a41cd633a86e41c4ae51f6ccd274c43b031decdac894d5deb07966a9022ac1789bf4e39cf78ec33d5c3199bf8af0e928ba6f2e6092b494066dd
6
+ metadata.gz: c80780a263df96fe7edb480a25bcf1ea78c16b9f00087184a0483c00c2125d73be8f3d69ebfa06577718a3d2472e7ba48be32deca856066c3fd009e11a64e5ed
7
+ data.tar.gz: ec534f16743c6cdfeae02261d2e99a4b8f68e0edf8568e85401115dee54e7a4135b624f908dc91613ab58a8c909ac028cee52388905c17ec7ad0ce38c6d1d251
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'logger'
4
-
5
4
  require_relative 'format'
6
5
 
7
6
  module Kybus
@@ -21,26 +20,39 @@ module Kybus
21
20
  new(config)
22
21
  end
23
22
 
23
+ def init_log_file
24
+ log_output = if @original_config['stdout'].nil? || @original_config['stdout']
25
+ $stdout
26
+ else
27
+ @original_config['file'] || 'application.log'
28
+ end
29
+ register('file', log_output)
30
+ register('rotate_days', @original_config['rotate_days'] || 7)
31
+ register('rotate_size', @original_config['rotate_size'] || (100 * (1024**2))) # 100Mb
32
+ end
33
+
24
34
  def initialize(config)
25
35
  @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
36
+ init_log_file
29
37
  register('date_format', config['date_format'] || '%Y-%m-%d %H:%M:%S')
30
38
  register('severity', config['severity'] || 'info')
31
- register('log_format', config['log_format'] ||
32
- "%time,%sev,%pid,%tid,%mod,%json\n")
39
+ register('log_format', config['log_format'] || "%time,%sev,%pid,%tid,%mod,%json\n")
33
40
  register('blacklist', config['blacklist'] || %w[pass password])
34
41
  register('logger', logger)
35
42
  end
36
43
 
37
44
  def logger
38
45
  @logger ||= begin
39
- logger = ::Logger.new(
40
- resource('file'),
41
- resource('rotate_days'),
42
- resource('rotate_size')
43
- )
46
+ log_output = resource('file')
47
+ logger = if log_output == $stdout
48
+ ::Logger.new(log_output)
49
+ else
50
+ ::Logger.new(
51
+ log_output,
52
+ resource('rotate_days'),
53
+ resource('rotate_size')
54
+ )
55
+ end
44
56
  $stdout.sync = true if @original_config['stdout']
45
57
  logger.sev_threshold = SEVERITIES[resource('severity')]
46
58
  logger.datetime_format = resource('date_format')
@@ -13,21 +13,33 @@ module Kybus
13
13
  # - %json: Data object encoded as json
14
14
  # default format:
15
15
  # "%time,%sev,%pid,%tid,%module,%json"
16
- def format_builder(format_string)
17
- proc do |frmt, sev, time, mod, data|
18
- meta = {
16
+ class LogCreator
17
+ attr_reader :frmt
18
+
19
+ def initialize(frmt, sev, time, mod)
20
+ @frmt = frmt
21
+ @meta = {
19
22
  '%sev': sev,
20
23
  '%time': time,
21
24
  '%mod': mod,
22
25
  '%pid': Process.pid.to_s(16),
23
26
  '%tid': Thread.current.object_id.to_s(16)
24
27
  }
28
+ end
29
+
30
+ def log_data(data)
25
31
  entry = frmt.dup
26
- meta.each { |k, v| entry.gsub!(k.to_s, v.to_s) }
32
+ @meta.each { |k, v| entry.gsub!(k.to_s, v.to_s) }
27
33
  # json is slower than the meta records, so it is better to replace
28
34
  # in a lazy way.
29
- # TODO: Move lazy tags to a pluggin-based model.
35
+ # TODO: Move lazy tags to a plugin-based model.
30
36
  entry.gsub('%json', data.to_json) if frmt.include?('%json')
37
+ end
38
+ end
39
+
40
+ def format_builder(format_string)
41
+ proc do |frmt, sev, time, mod, data|
42
+ LogCreator.new(frmt, sev, time, mod).log_data(data)
31
43
  end.curry.call(format_string)
32
44
  end
33
45
  end
@@ -13,37 +13,34 @@ module Kybus
13
13
  LogMethods.register(:global_config, config)
14
14
  end
15
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])
16
+ %i[debug info warn error fatal].each do |level|
17
+ define_method("log_#{level}".to_s) do |msg, data = nil|
18
+ debug = {}
19
+ case data
20
+ when Hash
21
+ debug = data.delete(:debug)
22
+ when NilClass
23
+ data = {}
24
+ when String
25
+ data = [data]
26
+ when Exception
27
+ data = { message: data.message, class: data.class, stack: data.backtrace }
28
+ end
29
+ log_raw(level, msg, data, debug)
30
+ end
34
31
  end
35
32
 
36
33
  def log_metric(metric:, amount:, group: nil, time: nil)
37
- log_info('Metric', metric: metric, value: amount,
38
- group: group, time: time)
34
+ log_info('Metric', metric:, value: amount,
35
+ group:, time:)
39
36
  end
40
37
 
41
38
  def log_alert(description:, group:, alert_severity:, notify_group:)
42
39
  log_fatal('Alert Triggered',
43
- description: description,
44
- group: group,
40
+ description:,
41
+ group:,
45
42
  severity: alert_severity,
46
- notify_group: notify_group)
43
+ notify_group:)
47
44
  end
48
45
 
49
46
  def log_raw(severity, msg, data, debug = {})
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kybus
4
4
  module Logger
5
- VERSION = '0.2.2'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kybus-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilberto Vargas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-15 00:00:00.000000000 Z
11
+ date: 2024-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kybus-core
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '12.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '12.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rdoc
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -124,7 +124,8 @@ files:
124
124
  homepage: https://github.com/tachomex/kybus
125
125
  licenses:
126
126
  - MIT
127
- metadata: {}
127
+ metadata:
128
+ rubygems_mfa_required: 'true'
128
129
  post_install_message:
129
130
  rdoc_options: []
130
131
  require_paths:
@@ -140,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
141
  - !ruby/object:Gem::Version
141
142
  version: '0'
142
143
  requirements: []
143
- rubygems_version: 3.1.4
144
+ rubygems_version: 3.5.14
144
145
  signing_key:
145
146
  specification_version: 4
146
147
  summary: Implements a kybus-logger with a format based on cute_logger