RTALogger 1.1.1 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ module RTALogger
2
+ class LogFilterBase
3
+ def initialize
4
+ @title = self.class.to_s.split('::').last.underscore
5
+ @enable = true
6
+ end
7
+
8
+ attr_accessor :title
9
+ attr_accessor :enable
10
+ attr_accessor :default_regex
11
+
12
+ def match_conditions(log_record)
13
+ return true if !@enable
14
+ return log_record.present?
15
+ end
16
+
17
+ def load_config(config_json)
18
+ @title = config_json['title'] if config_json['title'].present?
19
+ @enable = config_json['enable'].nil? ? true : config_json['enable'].present?
20
+ @default_regex = config_json['default_regex'] if config_json['default_regex'].present?
21
+ end
22
+
23
+ def apply_run_time_config(config_json)
24
+ @enable = config_json['enable'].nil? ? true : config_json['enable'].present?
25
+ @default_regex = config_json['default_regex'] if config_json['default_regex'].present?
26
+ end
27
+
28
+ def to_builder
29
+ jb = Jbuilder.new do |json|
30
+ json.type self.class.to_s.split('::').last.underscore.sub('log_filter_', '')
31
+ json.title @title
32
+ json.enable @enable
33
+ json.default_regex @default_regex
34
+ end
35
+
36
+ jb
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'log_filter_base'
2
+
3
+ module RTALogger
4
+ class LogFilterContext < LogFilterBase
5
+ def match_conditions(log_record)
6
+ return true if !@enable
7
+ result = super
8
+ return result unless result
9
+
10
+ return default_regex.present? ? (Regexp.new(@default_regex).match(log_record.context_id.to_s)) : result
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'log_filter_base'
2
+
3
+ module RTALogger
4
+ class LogFilterMessage < LogFilterBase
5
+ def match_conditions(log_record)
6
+ return true if !@enable
7
+ result = super
8
+ return result unless result
9
+
10
+ return default_regex.present? ? (Regexp.new(@default_regex).match(log_record.full_message)) : result
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'log_filter_base'
2
+
3
+ module RTALogger
4
+ class LogFilterTopic < LogFilterBase
5
+ def match_conditions(log_record)
6
+ return true if !@enable
7
+ result = super
8
+ return result unless result
9
+
10
+ return default_regex.present? ? (Regexp.new(@default_regex).match(log_record.topic_title)) : result
11
+ end
12
+ end
13
+ end
@@ -20,14 +20,13 @@ module RTALogger
20
20
 
21
21
  def initialize
22
22
  @enable = true
23
- @name = 'default_log_manager'
23
+ @title = 'default_log_manager'
24
24
  @app_name = ENV.fetch('RTA_LOGGER_APP_NAME', 'unknown_app')
25
- @severity_level = ENV.fetch('RTA_LOGGER_SEVERITY_LEVEL', WARN)
25
+ @severity_level = ENV.fetch('RTA_LOGGER_SEVERITY_LEVEL', INFO)
26
26
  @config_file_name = ''
27
27
  @topic_semaphore = Mutex.new
28
28
  @log_semaphore = Mutex.new
29
29
  self.buffer_size = ENV.fetch('RTA_LOGGER_BUFFER_SIZE', 100)
30
- @flush_size = @buffer_size * 20 / 100
31
30
  self.flush_wait_time = ENV.fetch('RTA_LOGGER_FLUSH_WAIT_SECONDS', 15)
32
31
  @topics = {}
33
32
  @log_records = []
@@ -42,7 +41,7 @@ module RTALogger
42
41
  @flush_scheduler.run
43
42
  end
44
43
 
45
- attr_reader :name
44
+ attr_reader :title
46
45
  attr_accessor :enable
47
46
  attr_accessor :app_name
48
47
  attr_reader :propagator
@@ -57,6 +56,7 @@ module RTALogger
57
56
 
58
57
  def buffer_size=(size)
59
58
  @buffer_size = size < 100 ? 100 : size
59
+ @flush_size = @buffer_size * 20 / 100
60
60
  end
61
61
 
62
62
  def flush_wait_time
@@ -67,8 +67,8 @@ module RTALogger
67
67
  @flush_wait_time = time_in_seconds < 10 ? 10 : time_in_seconds
68
68
  end
69
69
 
70
- def config_use_json_file(file_name, manager_name = '')
71
- config_json = load_config_from_json_file(file_name, manager_name)
70
+ def config_use_json_file(file_name, title = '')
71
+ config_json = load_config_from_json_file(file_name, title)
72
72
  @config_file_name = file_name if config_json
73
73
  apply_config(config_json)
74
74
  rescue StandardError => e
@@ -76,8 +76,8 @@ module RTALogger
76
76
  @propagator.add_log_repository(LogFactory.create_repository(:console))
77
77
  end
78
78
 
79
- def config_use_json_string(config_string, manager_name = '')
80
- config_json = load_config_from_json_string(config_string, manager_name)
79
+ def config_use_json_string(config_string, title = '')
80
+ config_json = load_config_from_json_string(config_string, title)
81
81
  apply_config(config_json)
82
82
  rescue StandardError => e
83
83
  @propagator.drop_all_repositories
@@ -114,10 +114,22 @@ module RTALogger
114
114
  @topic_semaphore.synchronize { @topics.keys.each { |topic| @topics[topic].severity_level = severity_level } }
115
115
  end
116
116
 
117
+ def topic_by_title(title)
118
+ result = nil
119
+ @topic_semaphore.synchronize do
120
+ @topics.keys.each do |topic_key|
121
+ result = @topics[topic_key.to_sym] if topic_key.to_s.casecmp(title).zero?
122
+ break if result
123
+ end
124
+ end
125
+
126
+ return result
127
+ end
128
+
117
129
  def to_builder
118
130
  @topic_semaphore.synchronize do
119
131
  jb = Jbuilder.new do |json|
120
- json.name name
132
+ json.title title
121
133
  json.enable enable
122
134
  json.app_name app_name
123
135
  json.config_file_name config_file_name
@@ -126,7 +138,8 @@ module RTALogger
126
138
  json.flush_size flush_size
127
139
  json.flush_wait_time flush_wait_time
128
140
  json.repositories do
129
- json.array! @propagator.repositories.collect { |repository| repository.to_builder.attributes! }
141
+ # json.array! @propagator.repositories.collect { |repository| repository.to_builder.attributes! }
142
+ json.array! @propagator.to_builder
130
143
  end
131
144
  json.topics do
132
145
  json.array! topics.keys.collect { |topic_key| @topics[topic_key].to_builder.attributes! }
@@ -141,33 +154,51 @@ module RTALogger
141
154
  to_builder.target!
142
155
  end
143
156
 
157
+ def apply_run_time_config(config_json)
158
+ return unless config_json
159
+ @enable = config_json['enable'] unless config_json['enable'].nil?
160
+ @default_severity_level = parse_severity_level_to_s(config_json['severity_level']) unless config_json['severity_level'].nil?
161
+ self.buffer_size = config_json['buffer_size'] unless config_json['buffer_size'].nil?
162
+ self.flush_wait_time = config_json['flush_wait_time'] unless config_json['flush_wait_time'].nil?
163
+ @propagator.apply_run_time_config(config_json)
164
+ apply_run_time_config_topics(config_json)
165
+ end
166
+
144
167
  private
145
168
 
146
- def load_config_from_json_file(config_file_name, manager_name = '')
169
+ def apply_run_time_config_topics(config_json)
170
+ config_json['topics']&.each do |topic_config|
171
+ next if topic_config['title'].nil?
172
+ topic = topic_by_title(topic_config['title'])
173
+ topic.apply_run_time_config(topic_config) if topic.present?
174
+ end
175
+ end
176
+
177
+ def load_config_from_json_file(config_file_name, title = '')
147
178
  config_file = File.open config_file_name
148
179
  config_json = ::JSON.load(config_file)
149
- config_json = extract_config(config_json, manager_name)
180
+ config_json = extract_config(config_json, title)
150
181
  config_json
151
182
  end
152
183
 
153
- def load_config_from_json_string(config_string, manager_name = '')
184
+ def load_config_from_json_string(config_string, title = '')
154
185
  config_json = ::JSON.parse(config_string)
155
- config_json = extract_config(config_json, manager_name)
186
+ config_json = extract_config(config_json, title)
156
187
  config_json
157
188
  end
158
189
 
159
- def extract_config(json_data, manager_name = '')
190
+ def extract_config(json_data, title = '')
160
191
  config_json = json_data['rta_logger']
161
192
  raise 'RTALogger configuration not found!' unless config_json
162
193
  raise 'Log_Managers section does not exists json configuration' unless config_json['log_managers']
163
194
  raise 'No config manager defined in json configuration' unless config_json['log_managers'].count.positive?
164
- manager_name = config_json['default_manager'] if manager_name.empty?
165
- unless manager_name.to_s.strip.empty?
166
- config_json = config_json['log_managers'].find { |item| item['manager_name'] == manager_name }
195
+ title = config_json['default_manager'] if title.empty?
196
+ unless title.to_s.strip.empty?
197
+ config_json = config_json['log_managers'].find { |item| item['title'] == title }
167
198
  end
168
199
  config_json ||= config_json['log_managers'][0]
169
200
  raise 'Unable to extract RTA Log Manager configuration!' unless config_json
170
- @name = manager_name if config_json
201
+ @title = title if config_json
171
202
  config_json
172
203
  end
173
204
 
@@ -178,15 +209,10 @@ module RTALogger
178
209
  @default_severity_level = parse_severity_level_to_i(config_json['severity_level']) if config_json['severity_level']
179
210
  self.buffer_size = config_json['buffer_size'] if config_json['buffer_size']
180
211
  self.flush_wait_time = config_json['flush_wait_seconds'] if config_json['flush_wait_seconds']
181
- @propagator.drop_all_repositories
182
- apply_config_repos(config_json)
212
+ @propagator.load_repositories(config_json)
183
213
  apply_config_topics(config_json)
184
214
  end
185
215
 
186
- def apply_config_repos(config_json)
187
- config_json['repositories']&.each { |item| @propagator.load_log_repository(item) }
188
- end
189
-
190
216
  def apply_config_topics(config_json)
191
217
  config_json['topics']&.each do |topic|
192
218
  next unless topic['title']
@@ -6,7 +6,7 @@ module RTALogger
6
6
  def initialize
7
7
  @semaphore = Mutex.new
8
8
  @records = []
9
- @repositories = []
9
+ @repositories = {}
10
10
  end
11
11
 
12
12
  attr_reader :repositories
@@ -16,31 +16,87 @@ module RTALogger
16
16
  end
17
17
 
18
18
  def add_log_repository(repository)
19
+ return if repository.nil? || repository.title.to_s.empty?
19
20
  return unless repository.is_a? RTALogger::LogRepository
20
- @repositories.push(repository) unless @repositories.include?(repository)
21
+ @semaphore.synchronize { @repositories[repository.title.to_sym] = repository unless @repositories[repository.title.to_sym].present? }
21
22
  end
22
23
 
23
24
  def load_log_repository(config_json)
24
25
  type = config_json['type']
25
26
  return if type.to_s.strip.empty?
26
- enable = config_json['enable'].nil? ? true : config_json['enable']
27
- return unless enable
28
27
 
29
28
  repository = ::RTALogger::LogFactory.create_repository(type, config_json)
30
29
  add_log_repository(repository)
31
30
  end
32
31
 
32
+ def load_repositories(config_json)
33
+ return if config_json.nil?
34
+
35
+ @semaphore.synchronize do
36
+ @repositories.clear
37
+ config_json['repositories']&.each do |repository_config|
38
+ type = repository_config['type']
39
+ next if type.to_s.strip.empty?
40
+
41
+ repository = ::RTALogger::LogFactory.create_repository(type, repository_config)
42
+ @repositories[repository.title.to_sym] = repository unless @repositories[repository.title.to_sym].present?
43
+ end
44
+ end
45
+ end
46
+
33
47
  def drop_all_repositories
34
48
  @semaphore.synchronize { @repositories.clear }
35
49
  end
36
50
 
51
+ def repository_by_title(title)
52
+ result = nil
53
+ @semaphore.synchronize do
54
+ @repositories.keys.each do |repository_key|
55
+ result = @repositories[repository_key.to_sym] if repository_key.to_s.casecmp(title).zero?
56
+ break if result
57
+ end
58
+ end
59
+
60
+ return result
61
+ end
62
+
63
+ def apply_run_time_config(config_json)
64
+ return unless config_json
65
+ apply_run_time_config_repositories(config_json)
66
+ end
67
+
37
68
  def propagate
38
69
  @semaphore.synchronize do
39
- @repositories.each do |repository|
40
- repository.add_log_records(@records)
70
+ @repositories.keys.each do |repository_key|
71
+ @repositories[repository_key.to_sym].add_log_records(@records)
41
72
  end
42
73
  @records.clear
43
74
  end
44
75
  end
76
+
77
+ def to_builder
78
+ result = nil
79
+ @semaphore.synchronize do
80
+ result = @repositories&.keys.collect { |repository_key| @repositories[repository_key].to_builder.attributes! }
81
+ end
82
+
83
+ return result
84
+ end
85
+
86
+ private
87
+
88
+ def apply_run_time_config_repositories(config_json)
89
+ config_json['repositories']&.each do |repository_config|
90
+ next if repository_config['title'].nil?
91
+ repository = repository_by_title(repository_config['title'])
92
+ if repository.present?
93
+ repository.apply_run_time_config(repository_config)
94
+ else
95
+ repository = ::RTALogger::LogFactory.create_repository(repository_config['type'], config_json)
96
+ add_log_repository(repository)
97
+ end
98
+ end
99
+ end
100
+
45
101
  end
46
102
  end
@@ -16,6 +16,10 @@ module RTALogger
16
16
  attr_reader :message
17
17
  attr_reader :occurred_at
18
18
 
19
+ def full_message
20
+ message.join(' ')
21
+ end
22
+
19
23
  def app_name
20
24
  @log_topic.log_manager.app_name
21
25
  end
@@ -1,4 +1,5 @@
1
1
  require_relative 'string'
2
+ require_relative 'log_factory_filter'
2
3
 
3
4
  module RTALogger
4
5
  # base log repository class
@@ -6,8 +7,10 @@ module RTALogger
6
7
  def initialize
7
8
  @semaphore = Mutex.new
8
9
  @log_records = []
10
+ @title = self.class.to_s.split('::').last.underscore
9
11
  @enable = true
10
12
  @formatter = RTALogger::LogFactory.log_formatter_default
13
+ @filters = {}
11
14
  end
12
15
 
13
16
  # @@sub_classes = {}
@@ -25,31 +28,41 @@ module RTALogger
25
28
  # @@sub_classes[repository_name] = self
26
29
  # end
27
30
 
31
+ attr_accessor :title
28
32
  attr_accessor :enable
29
33
  attr_accessor :formatter
30
34
 
31
35
  def add_log_records(items)
32
36
  return 0 unless @enable
33
37
  @semaphore.synchronize do
34
- items.each { |item| @log_records.push(item.dup) }
38
+ items.each do |item|
39
+ @log_records.push(item.dup) if filters_accept(item)
40
+ end
35
41
  end
42
+
36
43
  flush_and_clear
37
44
  end
38
45
 
39
46
  def load_config(config_json)
40
47
  @enable = config_json['enable'].nil? ? true : config_json['enable']
41
-
48
+ @title = config_json['title'].nil? ? self.class.to_s.split('::').last.underscore : config_json['title']
42
49
  formatter_config = config_json['formatter']
43
50
  if formatter_config && formatter_config['type']
44
51
  @formatter = LogFactory.create_formatter(formatter_config['type'], formatter_config)
45
52
  end
53
+
54
+ apply_config_filters(config_json)
46
55
  end
47
56
 
48
57
  def to_builder
49
58
  jb = Jbuilder.new do |json|
50
59
  json.type self.class.to_s.split('::').last.underscore.sub('log_repository_', '')
60
+ json.title @title
51
61
  json.enable enable
52
62
  json.formatter @formatter.to_builder.attributes!
63
+ json.filters do
64
+ json.array! @filters.keys.collect { |filter_key| @filters[filter_key].to_builder.attributes! }
65
+ end
53
66
  end
54
67
 
55
68
  jb
@@ -59,12 +72,63 @@ module RTALogger
59
72
  to_builder.target!
60
73
  end
61
74
 
75
+ def filter_by_title(title)
76
+ result = nil
77
+ @semaphore.synchronize do
78
+ @filters.keys.each do |filter_key|
79
+ result = @filters[filter_key.to_sym] if filter_key.to_s.casecmp(title).zero?
80
+ break if result
81
+ end
82
+ end
83
+
84
+ return result
85
+ end
86
+
87
+ def apply_run_time_config(config_json)
88
+ return unless config_json
89
+ @enable = config_json['enable'] unless config_json['enable'].nil?
90
+ apply_run_time_config_filters(config_json)
91
+ end
92
+
62
93
  protected
63
94
 
95
+ def apply_config_filters(config_json)
96
+ config_json['filters']&.each do |filter_config|
97
+ next if filter_config['type'].nil? || filter_config['title'].nil?
98
+ filter = LogFactory.create_filter(filter_config['type'], filter_config)
99
+ @filters[filter_config['title'].to_sym] = filter if filter.present?
100
+ end
101
+ end
102
+
103
+ def apply_run_time_config_filters(config_json)
104
+ return unless config_json
105
+
106
+ config_json['filters']&.each do |filter_config|
107
+ next if filter_config['title'].nil?
108
+ filter = filter_by_title(filter_config['title'])
109
+ if filter.present?
110
+ filter.apply_run_time_config(filter_config)
111
+ else
112
+ filter = LogFactory.create_filter(filter_config['type'], filter_config)
113
+ @semaphore.synchronize { @filters[filter_config['title'].to_sym] = filter if filter.present? }
114
+ end
115
+ end
116
+ end
117
+
64
118
  def flush_and_clear
65
119
  @semaphore.synchronize { @log_records.clear }
66
120
  end
67
121
 
122
+ def filters_accept(log_record)
123
+ result = true
124
+ @filters&.keys.each do |filter_key|
125
+ result = @filters[filter_key.to_sym]&.match_conditions(log_record)
126
+ break unless result
127
+ end
128
+
129
+ return result
130
+ end
131
+
68
132
  attr_reader :log_records
69
133
  attr_reader :semaphore
70
134
  end