chili_logger 0.0.6 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/.byebug_history +78 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +9 -1
  5. data/README.md +1024 -35
  6. data/assets/images/chili-logger-flow.png +0 -0
  7. data/assets/images/chiligum-logging-system.gif +0 -0
  8. data/assets/images/rabbit-basic-functioning.png +0 -0
  9. data/assets/images/rabbit-topic-explanation.webp +0 -0
  10. data/chili_logger-0.0.6.gem +0 -0
  11. data/chili_logger-0.0.7.gem +0 -0
  12. data/chili_logger-0.0.8.gem +0 -0
  13. data/chili_logger-0.0.9.1.gem +0 -0
  14. data/chili_logger-0.0.9.gem +0 -0
  15. data/lib/brokers/rabbit_broker.rb +5 -10
  16. data/lib/{errors/logging_error_handler/sqs_handler.rb → brokers/sqs_broker.rb} +4 -14
  17. data/lib/chili_logger.rb +33 -14
  18. data/lib/chili_logger/version.rb +1 -1
  19. data/lib/current_log_accessor.rb +77 -53
  20. data/lib/errors/logging_error_handler/logging_error_handler.rb +23 -14
  21. data/lib/{logs_coverage → helpers/logs_coverage}/chili_logger_coverage.yml +0 -0
  22. data/lib/{logs_coverage → helpers/logs_coverage}/coverage_writer.rb +20 -14
  23. data/lib/{logs_coverage → helpers/logs_coverage}/levantamento_provis/303/263rio.yaml +0 -0
  24. data/lib/helpers/values/default.rb +92 -0
  25. data/lib/helpers/values/type_uniformizer/desc.rb +64 -0
  26. data/lib/helpers/values/type_uniformizer/main_content.rb +62 -0
  27. data/lib/helpers/values/type_uniformizer/user.rb +43 -0
  28. data/lib/message_writer/aws_ops_metadata.rb +1 -1
  29. data/lib/message_writer/message_writer.rb +21 -18
  30. data/log/chili-logger-coverage.yml +50 -1
  31. metadata +19 -7
  32. data/lib/errors/error_messages/rabbitmq_error.rb +0 -9
@@ -1,16 +1,18 @@
1
1
  require 'yaml'
2
2
  require 'fileutils'
3
+ require 'helpers/values/default'
3
4
 
4
5
  # class for centralizing Creative's logging logic
5
6
  class ChiliLogger
6
7
  # class for keeping logs coverage up to date
7
8
  class CoverageWriter
8
9
  def self.write(desc, backtrace)
10
+ default = ChiliLogger::Values::Default.new
9
11
  coverage = read_file
10
12
 
11
- type = desc[:type]
12
- service = desc[:service]
13
- action = desc[:action]
13
+ type = desc[:type] || default.type
14
+ service = desc[:service] || default.service
15
+ action = desc[:action] || default.action
14
16
  keys = [type, service, action]
15
17
 
16
18
  coverage = add_nested_value(coverage, keys, backtrace)
@@ -18,17 +20,6 @@ class ChiliLogger
18
20
  write_file(coverage)
19
21
  end
20
22
 
21
- def self.add_nested_value(object, keys, final_value)
22
- return final_value if keys.empty?
23
-
24
- current_key = keys.first
25
- nested_object = object[current_key] || {}
26
- nested_keys = keys.drop(1)
27
- object[current_key] = add_nested_value(nested_object, nested_keys, final_value)
28
-
29
- object
30
- end
31
-
32
23
  def self.read_file
33
24
  file_exists = File.file?(file_path)
34
25
 
@@ -41,6 +32,19 @@ class ChiliLogger
41
32
  true
42
33
  end
43
34
 
35
+ # private class methods
36
+
37
+ def self.add_nested_value(object, keys, final_value)
38
+ return final_value if keys.empty?
39
+
40
+ current_key = keys.first
41
+ nested_object = object[current_key] || {}
42
+ nested_keys = keys.drop(1)
43
+ object[current_key] = add_nested_value(nested_object, nested_keys, final_value)
44
+
45
+ object
46
+ end
47
+
44
48
  def self.dir_name
45
49
  'log'
46
50
  end
@@ -52,5 +56,7 @@ class ChiliLogger
52
56
  def self.file_path
53
57
  File.join('.', dir_name, file_name)
54
58
  end
59
+
60
+ private_class_method :add_nested_value, :dir_name, :file_name, :file_path
55
61
  end
56
62
  end
@@ -0,0 +1,92 @@
1
+ # class for centralizing Creative's logging logic
2
+ class ChiliLogger
3
+ # module responsible for uniformization of values in ChiliLogger
4
+ module Values
5
+ # class for keeping all default values in a single place
6
+ class Default
7
+ def msg_broker
8
+ :rabbitmq
9
+ end
10
+
11
+ def msg_broker_config
12
+ {}
13
+ end
14
+
15
+ def user
16
+ {
17
+ cognito_id: undefined_string,
18
+ email: undefined_string,
19
+ company_cognito_id: undefined_string,
20
+ company_name: undefined_string,
21
+ ghost_user_cognito_id: undefined_string
22
+ }
23
+ end
24
+
25
+ def env
26
+ undefined_string
27
+ end
28
+
29
+ def layer
30
+ undefined_string
31
+ end
32
+
33
+ def type
34
+ undefined_string
35
+ end
36
+
37
+ def service
38
+ undefined_string
39
+ end
40
+
41
+ def action
42
+ undefined_string
43
+ end
44
+
45
+ def desc
46
+ { type: type, service: service, action: action }
47
+ end
48
+
49
+ def main_content
50
+ { modified_records: modified_records, errors: log_errors }
51
+ end
52
+
53
+ def modified_records
54
+ {}
55
+ end
56
+
57
+ def log_errors
58
+ []
59
+ end
60
+
61
+ def log_error
62
+ sentence1 = 'ChiliLogger received an unsupported type of error, so it replaced it with this generic string.'
63
+ sentence2 = ' Errors must be either a String or a descendant of the Exception class.'
64
+ sentence1 + sentence2
65
+ end
66
+
67
+ def server_url
68
+ undefined_string
69
+ end
70
+
71
+ def log
72
+ {}
73
+ end
74
+
75
+ def sqs_message
76
+ {
77
+ error_type: undefined_string,
78
+ error_message: undefined_string,
79
+ log: undefined_string
80
+ }
81
+ end
82
+
83
+ def invalid_string
84
+ 'invalid_format'
85
+ end
86
+
87
+ def undefined_string
88
+ 'not_specified'
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,64 @@
1
+ # class for centralizing Creative's logging logic
2
+ class ChiliLogger
3
+ # module responsible for uniformization of values in ChiliLogger
4
+ module Values
5
+ # class to enforce consistent typing of values across all classes
6
+ # this is important because of Elasticsearch. Check README, section ##read-before-contributing to know more about it
7
+ class TypeUniformizer
8
+ # responsible for uniformizing log's description primitive types
9
+ class Desc
10
+ def initialize
11
+ @default = ChiliLogger::Values::Default.new
12
+ end
13
+
14
+ def desc(desc)
15
+ return {} unless desc.is_a?(Hash)
16
+
17
+ uniformized_desc = {}
18
+ valid_desc_attrs.each do |attr_name|
19
+ # only overwrite if key was explicitly set in desc
20
+ next unless desc.key?(attr_name)
21
+
22
+ desc_attr = desc[attr_name]
23
+ desc_attr = @default.invalid_string unless valid_desc_attr_formats.include?(desc_attr.class.name)
24
+ desc_attr ||= @default.undefined_string # if type == nil
25
+ uniformized_desc[attr_name] = desc_attr
26
+ end
27
+
28
+ uniformized_desc
29
+ end
30
+
31
+ def type(type)
32
+ return @default.undefined_string if type.nil?
33
+ return @default.invalid_string unless type.is_a?(String)
34
+
35
+ type
36
+ end
37
+
38
+ def service(service)
39
+ return @default.undefined_string if service.nil?
40
+ return @default.invalid_string unless service.is_a?(String)
41
+
42
+ service
43
+ end
44
+
45
+ def action(action)
46
+ return @default.undefined_string if action.nil?
47
+ return @default.invalid_string unless action.is_a?(String)
48
+
49
+ action
50
+ end
51
+
52
+ private
53
+
54
+ def valid_desc_attrs
55
+ %i[type service action]
56
+ end
57
+
58
+ def valid_desc_attr_formats
59
+ %w[String NilClass]
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,62 @@
1
+ # class for centralizing Creative's logging logic
2
+ class ChiliLogger
3
+ # module responsible for uniformization of values in ChiliLogger
4
+ module Values
5
+ # class to enforce consistent typing of values across all classes, this is important because of Elasticsearch
6
+ # Check README, section ##read-before-contributing, to know more about it
7
+ class TypeUniformizer
8
+ # responsible for uniformizing the primitive types of a log's main_content
9
+ class MainContent
10
+ def initialize
11
+ @default = ChiliLogger::Values::Default.new
12
+ end
13
+
14
+ def main_content(content)
15
+ return {} unless content.is_a?(Hash)
16
+
17
+ uniformized_content = {}
18
+
19
+ if content.key?(:modified_records) # only overwrite if :modified_records was explicitly set in main_content
20
+ mod_recs = content[:modified_records]
21
+ uniformized_content[:modified_records] = modified_records(mod_recs)
22
+ end
23
+ # only overwrite if :errors was explicitly set in main_content
24
+ uniformized_content[:errors] = errors(content[:errors]) if content.key?(:errors)
25
+
26
+ uniformized_content
27
+ end
28
+
29
+ def modified_records(records)
30
+ return {} unless records.is_a?(Hash)
31
+
32
+ uniformized_recs = {}
33
+ # modified records should be a hash where each key points to an array of valid modified records
34
+ records.each do |key, val|
35
+ val_as_array = val.is_a?(Array) ? val : [].push(val)
36
+ uniformized_val = val_as_array.map { |record| modified_record(record) }
37
+ uniformized_recs[key.to_s] = uniformized_val
38
+ end
39
+
40
+ uniformized_recs
41
+ end
42
+
43
+ def modified_record(record)
44
+ record.is_a?(Hash) ? record : {}
45
+ end
46
+
47
+ def errors(errors)
48
+ return [] unless errors.is_a?(Array)
49
+
50
+ errors.map { |err| error(err) }
51
+ end
52
+
53
+ # a received error must be either a string or Exception descendent(it will be converted to a string)
54
+ def error(error)
55
+ error = error.inspect if error.class.ancestors.include?(Exception) # stringifies Exception descendents
56
+ error = @default.log_error unless error.is_a?(String)
57
+ error
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,43 @@
1
+ # class for centralizing Creative's logging logic
2
+ class ChiliLogger
3
+ # module responsible for uniformization of values in ChiliLogger
4
+ module Values
5
+ # class to enforce consistent typing of values across all classes
6
+ # this is important because of Elasticsearch. Check README, section ##read-before-contributing to know moreabout it
7
+ class TypeUniformizer
8
+ # responsible for uniformizing log's primitive types
9
+ class User
10
+ def initialize
11
+ @default = ChiliLogger::Values::Default.new
12
+ end
13
+
14
+ def user(user)
15
+ return {} unless user.is_a?(Hash)
16
+
17
+ uniformized_user = {}
18
+ valid_user_attrs.each do |attr_name|
19
+ # don't enforce typing compliance of attrs that weren't explicitly set in user
20
+ next unless user.key?(attr_name)
21
+
22
+ user_attr = user[attr_name]
23
+ user_attr = @default.invalid_string unless valid_user_attr_formats.include?(user_attr.class.name)
24
+ user_attr ||= @default.undefined_string # if type == nil
25
+ uniformized_user[attr_name] = user_attr.to_s
26
+ end
27
+
28
+ uniformized_user
29
+ end
30
+
31
+ private
32
+
33
+ def valid_user_attrs
34
+ %i[cognito_id email company_cognito_id company_name ghost_user_cognito_id]
35
+ end
36
+
37
+ def valid_user_attr_formats
38
+ %w[String NilClass Fixnum Bignum]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,5 @@
1
1
  require 'httparty'
2
- require 'logs_coverage/coverage_writer'
2
+ require 'helpers/logs_coverage/coverage_writer'
3
3
  require 'message_writer/aws_ops_metadata'
4
4
 
5
5
  # class for centralizing Creative's logging logic
@@ -1,4 +1,4 @@
1
- require 'logs_coverage/coverage_writer'
1
+ require 'helpers/logs_coverage/coverage_writer'
2
2
  require 'message_writer/aws_ops_metadata'
3
3
 
4
4
  # class for centralizing Creative's logging logic
@@ -6,21 +6,23 @@ class ChiliLogger
6
6
  # class for writing log messages
7
7
  class MessageWriter
8
8
  def initialize(config)
9
- @env = config[:env] || 'not_specified'
10
- @layer = config[:layer] || 'not_specified'
11
- @server_url = config[:server_url] || 'not_specified'
9
+ @default = ChiliLogger::Values::Default.new
10
+ @env = config[:env] || @default.env
11
+ @layer = config[:layer] || @default.layer
12
+ @server_url = config[:server_url] || @default.server_url
12
13
  @cloud_provider = config[:cloud_provider]
13
14
  end
14
15
 
15
- def write(desc = {}, agent = 'not_specified', main_content = {})
16
+ def write(**options)
16
17
  return if ChiliLogger.instance.deactivated
17
18
 
18
- desc ||= {}
19
- main_content ||= {}
19
+ desc = options[:desc] || @default.desc
20
+ user = options[:user] || @default.user
21
+ main_content = options[:main_content] || @default.main_content
20
22
 
21
- main_content.merge!( backtrace: clean_backtrace(caller) )
23
+ main_content[:backtrace] = clean_backtrace(caller)
22
24
  update_logs_coverage(desc, main_content)
23
- message_hash(desc, agent, main_content)
25
+ message_hash(desc: desc, user: user, main_content: main_content)
24
26
  end
25
27
 
26
28
  def clean_backtrace(backtrace)
@@ -31,9 +33,9 @@ class ChiliLogger
31
33
  def write_description_tag(desc)
32
34
  env = desc[:env] || @env
33
35
  layer = desc[:layer] || @layer
34
- type = desc[:type] || 'not_specified'
35
- service = desc[:service] || 'not_specified'
36
- action = desc[:action] || 'not_specified'
36
+ type = desc[:type] || @default.type
37
+ service = desc[:service] || @default.service
38
+ action = desc[:action] || @default.action
37
39
 
38
40
  "#{env}.#{layer}.#{type}.#{service}.#{action}"
39
41
  end
@@ -47,17 +49,18 @@ class ChiliLogger
47
49
  end
48
50
 
49
51
  # rubocop:disable Metrics/MethodLength
50
- def message_hash(desc, agent, main_content)
52
+ def message_hash(**options)
53
+ desc = options[:desc]
51
54
  {
52
55
  desc: write_description_tag(desc),
53
56
  env: @env,
54
57
  layer: @layer,
55
- type: desc[:type] || 'not_specified',
56
- service: desc[:service] || 'not_specified',
57
- action: desc[:action] || 'not_specified',
58
- user_info: agent || 'not_specified',
58
+ type: desc[:type] || @default.type,
59
+ service: desc[:service] || @default.service,
60
+ action: desc[:action] || @default.action,
61
+ user: options[:user] || @default.undefined_string,
59
62
  timestamp: DateTime.now,
60
- "#{desc[:type]}": main_content || {},
63
+ "#{desc[:type]}": options[:main_content] || {},
61
64
  ops_metadata: write_ops_metadata
62
65
  }
63
66
  end
@@ -2,7 +2,7 @@
2
2
  test:
3
3
  console:
4
4
  publish_test:
5
- - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/chili_logger-0.0.2/lib/chili_logger.rb:42:in
5
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/chili_logger-0.0.8/lib/chili_logger.rb:61:in
6
6
  `publish_instant_log'"
7
7
  - "(pry):28:in `__pry__'"
8
8
  - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in
@@ -48,3 +48,52 @@ test:
48
48
  - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/init.rb:20:in `setup'"
49
49
  - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb.rb:378:in `start'"
50
50
  - "/usr/share/rvm/rubies/ruby-2.3.3/bin/irb:11:in `<main>'"
51
+ transaction:
52
+ console:
53
+ publish_test:
54
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/chili_logger-0.0.8/lib/chili_logger.rb:61:in
55
+ `publish_instant_log'"
56
+ - "(pry):53:in `__pry__'"
57
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in
58
+ `eval'"
59
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in
60
+ `evaluate_ruby'"
61
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:659:in
62
+ `handle_line'"
63
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:261:in
64
+ `block (2 levels) in eval'"
65
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in
66
+ `catch'"
67
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in
68
+ `block in eval'"
69
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in
70
+ `catch'"
71
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in
72
+ `eval'"
73
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:77:in `block
74
+ in repl'"
75
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:67:in `loop'"
76
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:67:in `repl'"
77
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:38:in `block
78
+ in start'"
79
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/input_lock.rb:61:in
80
+ `__with_ownership'"
81
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/input_lock.rb:78:in
82
+ `with_ownership'"
83
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:38:in `start'"
84
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:15:in `start'"
85
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_class.rb:191:in
86
+ `start'"
87
+ - "/home/lucas/.irbrc:8:in `<top (required)>'"
88
+ - "/usr/share/rvm/scripts/irbrc.rb:40:in `load'"
89
+ - "/usr/share/rvm/scripts/irbrc.rb:40:in `<top (required)>'"
90
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in
91
+ `require'"
92
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in
93
+ `require'"
94
+ - "/usr/share/rvm/rubies/ruby-2.3.3/.irbrc:11:in `<top (required)>'"
95
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/init.rb:231:in `load'"
96
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/init.rb:231:in `run_config'"
97
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/init.rb:20:in `setup'"
98
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb.rb:378:in `start'"
99
+ - "/usr/share/rvm/rubies/ruby-2.3.3/bin/irb:11:in `<main>'"