ant-logger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 79e7fb8ef1bfebac41fd2efbf61449eddc677710fb7fcd8770d13f0b7bfa675f
4
+ data.tar.gz: 2c6616ad37a49a6711da08408e4a099a3f4f311b4b3680c5ff4cb0caac43e0c5
5
+ SHA512:
6
+ metadata.gz: dd89dc3c61ec1b3230f03eaae55ff24cb766b996fa5bcfeea62ccab39cbc9fd75cd515c4313d3caf978485e5ea24c0c403a89e4e2de878e644da80f3075cc900
7
+ data.tar.gz: 49aa287b29c47a848bc47210538cae54a38ef559c70fbeb420b45c3419b3e84851a3b4b01f8eaf36d775005833ac5e350f0979aee5d77524004578c5d38608a6
@@ -0,0 +1,13 @@
1
+ require 'ant/core'
2
+ require 'json'
3
+
4
+ require_relative 'logger/config'
5
+ require_relative 'logger/format'
6
+ require_relative 'logger/log_methods'
7
+
8
+
9
+ module Ant
10
+ module Logger
11
+ include LogMethods
12
+ end
13
+ end
data/lib/ant/logger.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'log_decorator'
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ require_relative 'format'
6
+
7
+ module Ant
8
+ module Logger
9
+ class Config
10
+ include Ant::DRY::ResourceInjector
11
+ include Ant::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['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
+ logger.sev_threshold = SEVERITIES[resource('severity')]
45
+ logger.datetime_format = resource('date_format')
46
+ logger.formatter = format_builder(resource('log_format'))
47
+ logger
48
+ end
49
+ end
50
+
51
+ def merge_params(msg, data, debug, severity)
52
+ blacklist = resource('blacklist') + ['debug']
53
+ params = {}
54
+ data.each { |k, v| params[k] = v unless blacklist.include?(k.to_s) }
55
+ params[:debug] = debug if resource('severity') == 'debug'
56
+ [msg, params]
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
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,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Logger
5
+ module LogMethods
6
+ extend Ant::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)
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:, tagging_group:, tagging_sev:, tagging_org:)
42
+ log_fatal('Alert Triggered',
43
+ description: description,
44
+ group: tagging_group,
45
+ severity: tagging_sev,
46
+ response_team: tagging_org)
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, severity)
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
+ global_config
59
+ end
60
+
61
+ LogMethods.global_config = Config.from_config({})
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ant
4
+ module Logger
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ant-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gilberto Vargas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ant-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/ant/log_decorator.rb
119
+ - lib/ant/logger.rb
120
+ - lib/ant/logger/config.rb
121
+ - lib/ant/logger/format.rb
122
+ - lib/ant/logger/log_methods.rb
123
+ - lib/ant/logger/version.rb
124
+ homepage: https://github.com/tachomex/ant
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.0.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Implements a ant-logger with a format based on cute_logger
147
+ test_files: []