chili_logger 0.0.9.1 → 0.1.3

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: bcee860ae763961cedf6bd91456a7932a00ca2eae59f276380c2a90dd9479e12
4
- data.tar.gz: cb5e2cc3075bbda8cba9baa7f29ae05f5f35b79eb3f7a986bc382dcba7052f03
3
+ metadata.gz: fc07fd046be4d08dd0bf3e972ab83f5db6468186ca7d41c2ec868dd698f308d7
4
+ data.tar.gz: 850be7ee87e9e30fd203236d274755d02c75976ec8a1e467a94c83c50fd9894c
5
5
  SHA512:
6
- metadata.gz: 2492803a054170942b232fd1186827ed1b4f635b6a5fc7de7883321be454756b841502afe744112e11b398fd1b27eaf4813fc5d10db12018e22e81d7534da443
7
- data.tar.gz: f9e3ebd7f7414863fa78ab5affac2b9940fcd40a07a85489afdb3782869d1ce098d373ac861cdb4eed7259aa4b0575331a2f2bdae024953068d0cc9344e835cb
6
+ metadata.gz: 322027d12c0e96b227d67e0c60cca022c65945da4477fd8f7dfac8749dd194111db11cd5a8ca09e44334807ab949b510bf8b169ea0258ba1472a854d7bb5d00c
7
+ data.tar.gz: c79b0c42deadf14c4119ab6084151977c0e31f829ce9d3323cdf92502d9a79f9977cbfe4ed944cded842c6f2b48825774afce8390cce1ef40589e49423aa7631
@@ -1,126 +1,68 @@
1
1
  continue
2
- options
2
+ sqs_queue_attrs.attributes['ApproximateNumberOfMessages'].to_i.positive?
3
+ sqs_queue_attrs.attributes['ApproximateNumberOfMessages'].to_i
4
+ sqs_queue_attrs
3
5
  continue
4
- options
6
+ sqs_queue_attrs
5
7
  continue
6
- @desc_uniformizer.desc([])
7
- @desc_uniformizer.desc(options[:desc])
8
- @desc_uniformizer.desc
9
- @desc_uniformizer.desc(options[:desc])
10
- options
8
+ sqs_queue_attrs
11
9
  continue
12
- options
10
+ sqs_queue_attrs
13
11
  continue
14
- main_content.merge(uniformized_content)
15
- main_content
12
+ remaining_msgs
13
+ remaining_msgs.positive?
16
14
  continue
17
- main_content
18
- uniformized_content
15
+ remaining_msgs.positive?
16
+ remaining_msgs
19
17
  continue
20
- desc
18
+ remaining_msgs
21
19
  continue
22
- uniformized_content
23
- continue
24
- uniformized_content
20
+ remaining_msgs
25
21
  quit
26
- val.first.is_a?(Hash)
27
- val.first.is_a?(Hash) ? {} : record
28
- modified_record(val.first)
29
- val
30
- uniformized_val
22
+ remaining_msgs
23
+ messages
31
24
  quit
25
+ msgs.count
26
+ msgs
32
27
  continue
33
- error.inspect.is_a?(String)
34
- error.inspect
35
- error.class.ancestors.include?(Exception)
36
- error
37
- new_error
38
- errors
39
- all_errors
40
- continue
41
- new_error.is_a?(String)
42
- continue
43
- new_error.is_a?(String)
44
- new_error.class
45
- new_error
46
- continue
47
- new_error
48
- continue
49
- new_error
50
- continue
51
- error
52
- new_error
28
+ @sqs_config[:region]
29
+ @sqs_config['region']
30
+ @sqs_config
31
+ quit
32
+ queue_url(@queue_name)
33
+ quit
34
+ queue_url(@queue_name)
53
35
  quit
54
- main_content[:errors] ||= @default.log_errors
55
- errors
56
- continue
57
- user[:cognito_id]
58
- new_user[:cognito_id]
59
- continuenew_user[:cognito_id]
60
- continue
61
- new_user
62
- continue
63
- user
64
- Thread.current[:current_log_user]
65
- Thread.current[:current_log_user] ||= default_user
66
- continue
67
- Thread.current[:current_log_user] ||= default_user
68
- continue
69
- Thread.current[:current_log_user] ||= default_user
70
- continue
71
- Thread.current[:current_log_user] ||= default_user
72
- user
73
- continue
74
- log_user
75
- continue
76
- Thread.current[:current_log_user] ||= default_user
77
- Thread.current[:current_log_user]
78
- user
79
- continue
80
- e.class
81
- e.is_a?(Bunny:TCPConnectionFailed)
82
- e
83
- @sqs.send_message('queue_message')
84
- @sqs.get_queue_url(queue_name: 'mockeesd').queue_url
85
- @sqs.get_queue_url(queue_name: 'mock').queue_url
86
- @sqs.get_queue_url(queue_name: queue_name).queue_url
87
- @sqs
88
- response
89
- continue
90
- cloud_metadata[@cloud_provider]
91
- @cloud_provider
92
- continue
93
- @cloud_provider
94
- continue
95
- @cloud_provider
96
- continue
97
- @cloud_provider
98
- continue
99
- @cloud_provider
100
- continue
101
- @cloud_provider
102
36
  continue
103
- @cloud_provider
37
+ queue_url(@queue_name)
104
38
  continue
105
- @cloud_provider
39
+ mock_broker.class.ancestors
40
+ mock_broker.ancestors
106
41
  continue
107
- cloud_metadata[nil]
108
- @cloud_provider
42
+ stubbed_sqs_poller.poll
43
+ stubbed_sqs_poller.poll { |msg| }
44
+ stubbed_sqs_poller.poll {}
45
+ stubbed_sqs_poller.poll
46
+ stubbed_sqs_poller
47
+ Aws::SQS::QueuePoller.new(stub_responses: true).poll
48
+ ENV['AWS_REGION'] = 'us-east-1'
49
+ stubbed_sqs_poller.poll
109
50
  continue
110
- @ip
111
- @user
112
- @port
51
+ ChiliLogger.instance.instance_variable_get('@unpublished_logs_manager')
52
+ mock_unpub_logs_mngr == ChiliLogger.instance.instance_variable_get('@unpublished_logs_manager')
53
+ mock_unpub_logs_mngr
113
54
  continue
114
- msg[type.to_sym]
115
- msg[type]
116
- pp msg
117
- p msg
118
- pp msg
119
- msg
120
- type
55
+ file
56
+ throw :stop_polling
121
57
  continue
122
- mock_current_log.clear_log_info
123
- mock_current_log
58
+ error
59
+ @rabbit_exchange.publish(log.to_json, routing_key: routing_key)
60
+ routing_key = log['desc']
61
+ log = JSON.parse(sqs_msg.body)['body']['log']
62
+ @rabbit_exchange.publish(log.to_json, routing_key: routing_key)
63
+ routing_key = log['desc']
64
+ log = JSON.parse(sqs_msg.body)['body']['log']
65
+ @rabbit_exchange
124
66
  continue
125
67
  n
126
68
  item_name
data/.gitignore CHANGED
@@ -11,3 +11,5 @@ local_tests_cheatsheet.rb
11
11
 
12
12
  # rspec failure tracking
13
13
  .rspec_status
14
+
15
+ chili_logger-*.gem
data/Gemfile CHANGED
@@ -4,7 +4,8 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'activerecord', '~> 5.2'
7
- gem 'aws-sdk', '2.9.2'
7
+ gem 'aws-sdk-secretsmanager', '~> 1.36.0'
8
+ gem 'aws-sdk-sqs', '~> 1.30.0'
8
9
  gem 'bunny'
9
10
  gem 'byebug'
10
11
  gem 'rake', '~> 12.0'
@@ -13,4 +14,4 @@ gem 'rubocop', '~> 0.60.0', require: false
13
14
 
14
15
  gem 'httparty', '~> 0.18.1'
15
16
 
16
- gem "simplecov", "~> 0.17.1"
17
+ gem 'simplecov', '~> 0.17.1'
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chili_logger (0.0.9.1)
5
- aws-sdk (~> 2.9, >= 2.9.0)
4
+ chili_logger (0.1.3)
5
+ aws-sdk-secretsmanager (~> 1.36.0)
6
+ aws-sdk-sqs (~> 1.30.0)
6
7
  bunny
7
8
  httparty
8
9
 
@@ -20,24 +21,29 @@ GEM
20
21
  i18n (>= 0.7, < 2)
21
22
  minitest (~> 5.1)
22
23
  tzinfo (~> 1.1)
23
- amq-protocol (2.3.1)
24
+ amq-protocol (2.3.2)
24
25
  arel (9.0.0)
25
26
  ast (2.4.1)
26
27
  aws-eventstream (1.1.0)
27
- aws-sdk (2.9.2)
28
- aws-sdk-resources (= 2.9.2)
29
- aws-sdk-core (2.9.2)
30
- aws-sigv4 (~> 1.0)
28
+ aws-partitions (1.341.0)
29
+ aws-sdk-core (3.103.0)
30
+ aws-eventstream (~> 1, >= 1.0.2)
31
+ aws-partitions (~> 1, >= 1.239.0)
32
+ aws-sigv4 (~> 1.1)
31
33
  jmespath (~> 1.0)
32
- aws-sdk-resources (2.9.2)
33
- aws-sdk-core (= 2.9.2)
34
- aws-sigv4 (1.1.4)
35
- aws-eventstream (~> 1.0, >= 1.0.2)
34
+ aws-sdk-secretsmanager (1.36.0)
35
+ aws-sdk-core (~> 3, >= 3.71.0)
36
+ aws-sigv4 (~> 1.1)
37
+ aws-sdk-sqs (1.30.0)
38
+ aws-sdk-core (~> 3, >= 3.99.0)
39
+ aws-sigv4 (~> 1.1)
40
+ aws-sigv4 (1.2.1)
41
+ aws-eventstream (~> 1, >= 1.0.2)
36
42
  bunny (2.15.0)
37
43
  amq-protocol (~> 2.3, >= 2.3.1)
38
44
  byebug (11.0.1)
39
45
  concurrent-ruby (1.1.6)
40
- diff-lcs (1.4.2)
46
+ diff-lcs (1.4.4)
41
47
  docile (1.3.2)
42
48
  httparty (0.18.1)
43
49
  mime-types (~> 3.0)
@@ -95,7 +101,8 @@ PLATFORMS
95
101
 
96
102
  DEPENDENCIES
97
103
  activerecord (~> 5.2)
98
- aws-sdk (= 2.9.2)
104
+ aws-sdk-secretsmanager (~> 1.36.0)
105
+ aws-sdk-sqs (~> 1.30.0)
99
106
  bunny
100
107
  byebug
101
108
  chili_logger!
data/README.md CHANGED
@@ -3,60 +3,63 @@
3
3
  ChiliLogger is a gem developed by Chiligum Creatives for internal use. It is used to monitor our ruby applications and generate logs with some uniformization.
4
4
 
5
5
  ## Table of Contents
6
- * [READ BEFORE CONTRIBUTING](##read-before-contributing)
7
- * [How it Works](##how-it-works)
8
- * [Overview](###overview)
9
- * [Anatomy of a Chiligum Log](###anatomy-of-a-chiligum-log)
10
- * [Example Log](####example-log)
11
- * [Environment](####environment)
12
- * [Layer](####layer)
13
- * [Type](####type)
14
- * [Service](####service)
15
- * [Action](####action)
16
- * [Description](####description)
17
- * [User](####user)
18
- * [Main Content](####main-content)
19
- * [Ops Metadata](####ops-metadata)
20
- * [Timestamp](####timestamp)
21
- * [A Primer on RabbitMQ and ElasticSearch](###a-primer-on-rabbitmq-and-elasticsearch)
22
- * [RabbitMQ](####rabbitmq)
23
- * [ElasticSearch](####elasticsearch)
24
- * [Putting It All Together](####putting-it-all-together)
25
- * [Installation](##installation)
26
- * [Basic Usage](##basic-usage)
27
- * [publish_instant_log params](####publish_instant_log-params)
28
- * [publish_instant_log usage example](####publish_instant_log-usage-example)
29
- * [publish_instant_log default argument values](####publish_instant_log-default-argument-values)
30
- * [ChiliLogger's Singleton pattern](####ChiliLoggers-singleton-pattern)
31
- * [Advanced Usage](##advanced-usage)
32
- * [Code Example](####advanced-code-example)
33
- * [Error Logging](####error-logging)
34
- * [Customizing a Started Log](####customizing-a-started-log)
35
- * [Accessing the current_log](####accessing-the-current_log)
36
- * [user](####user)
37
- * [update_user](####update_user)
38
- * [desc](####desc)
39
- * [update_type](####update_type)
40
- * [update_service](####update_service)
41
- * [update_action](####update_action)
42
- * [update_desc](####update_desc)
43
- * [main_content](####main_content)
44
- * [update_main_content](####update_main_content)
45
- * [add_to_main_content](####add_to_main_content)
46
- * [modified_records](####modified_records)
47
- * [update_modified_records](####update_modified_records)
48
- * [add_modified_record](####add_modified_record)
49
- * [clear_log_info](####clear_log_info)
50
- * [Snippets For Quicker Logging](##snippets-for-quicker-logging)
51
- * [Papertrail Optional Use](###papertrail-optional-use)
52
- * [Logging Transactions in HTP Requests](###logging-transactions-in-http-requests)
53
- * [Controllers](####controllers-logging-logic)
54
- * [Models](####models-logging-logic)
55
- * [Logging Rake Tasks](###logging-rake-tasks)
56
- * [Logging Uncaught task Errors](####logging-uncaught-task-errors)
57
- * [Logging Transactions in Tasks](####logging-transactions-in-tasks)
58
- * [Logging Heartbeats in Tasks](####logging-heartbeats-in-tasks)
59
- * [Coverage](##Coverage)
6
+ * [READ BEFORE CONTRIBUTING](#read-before-contributing)
7
+ * [How it Works](#how-it-works)
8
+ * [Overview](#overview)
9
+ * [Anatomy of a Chiligum Log](#anatomy-of-a-chiligum-log)
10
+ * [Example Log](#example-log)
11
+ * [Environment](#environment)
12
+ * [Layer](#layer)
13
+ * [Type](#type)
14
+ * [Service](#service)
15
+ * [Action](#action)
16
+ * [Description](#description)
17
+ * [User](#user)
18
+ * [Main Content](#main-content)
19
+ * [Ops Metadata](#ops-metadata)
20
+ * [Timestamp](#timestamp)
21
+ * [A Primer on RabbitMQ and ElasticSearch](#a-primer-on-rabbitmq-and-elasticsearch)
22
+ * [RabbitMQ](#rabbitmq)
23
+ * [ElasticSearch](#elasticsearch)
24
+ * [Putting It All Together](#putting-it-all-together)
25
+ * [Installation](#installation)
26
+ * [Basic Initialization](#basic-initialization)
27
+ * [Customizing the Message Brokers](#customizing-the-message-brokers)
28
+ * [Basic Usage](#basic-usage)
29
+ * [publish_instant_log params](#publish_instant_log-params)
30
+ * [publish_instant_log usage example](#publish_instant_log-usage-example)
31
+ * [publish_instant_log default argument values](#publish_instant_log-default-argument-values)
32
+ * [ChiliLogger's Singleton pattern](#ChiliLoggers-singleton-pattern)
33
+ * [Advanced Usage](#advanced-usage)
34
+ * [Code Example](#advanced-code-example)
35
+ * [Error Logging](#error-logging)
36
+ * [Customizing a Started Log](#customizing-a-started-log)
37
+ * [Accessing the current_log](#accessing-the-current_log)
38
+ * [user](#user)
39
+ * [update_user](#update_user)
40
+ * [desc](#desc)
41
+ * [update_type](#update_type)
42
+ * [update_service](#update_service)
43
+ * [update_action](#update_action)
44
+ * [update_desc](#update_desc)
45
+ * [main_content](#main_content)
46
+ * [update_main_content](#update_main_content)
47
+ * [add_to_main_content](#add_to_main_content)
48
+ * [modified_records](#modified_records)
49
+ * [update_modified_records](#update_modified_records)
50
+ * [add_modified_record](#add_modified_record)
51
+ * [clear_log_info](#clear_log_info)
52
+ * [Republishing Unpublished Logs](#republishing-unpublished-logs)
53
+ * [Snippets For Quicker Logging](#snippets-for-quicker-logging)
54
+ * [Papertrail Optional Use](#papertrail-optional-use)
55
+ * [Logging Transactions in HTP Requests](#logging-transactions-in-http-requests)
56
+ * [Controllers](#controllers-logging-logic)
57
+ * [Models](#models-logging-logic)
58
+ * [Logging Rake Tasks](#logging-rake-tasks)
59
+ * [Logging Uncaught task Errors](#logging-uncaught-task-errors)
60
+ * [Logging Transactions in Tasks](#logging-transactions-in-tasks)
61
+ * [Logging Heartbeats in Tasks](#logging-heartbeats-in-tasks)
62
+ * [Coverage](#Coverage)
60
63
 
61
64
  ## READ BEFORE CONTRIBUTING
62
65
  The main thing you need to know before changing Chililogger's source code is this: we send our logs in JSON format and [Logstash parses and converts them to Elasticsearch objects](####elasticsearch), requiring all fields to have consistent primitive types. So suppose you send these logs:
@@ -102,9 +105,11 @@ So if you are going to change anything in ChiliLogger, you have to keep this in
102
105
 
103
106
  ![ChiliLogger usual Flow](assets/images/chili-logger-flow.png)
104
107
 
108
+ Every time we use ChiliLogger to publish a log, it will try to publish it to what has been configured as its `msg_broker`. If the msg_broker is down or some other unexpected error occurs, ChiliLogger will send the log and error message to what has been configured as its `fallback_broker`. This way, no logs are lost and [ChiliLogger can later try to republish these unpublished logs](#republishing-unpublished-logs).
109
+
105
110
  For the time being, only `RabbitMQ` is supported as the main message broker. Only `AWS SQS` is available as a fallback broker.
106
111
 
107
- If both the main message broker and the fallback message broker are down, ChiliLogger wil discard the log to keep the application running without raising any error. That's because raising errors would make the app crash and make it impossible to be used while the brokers are down.
112
+ If both the main message broker and the fallback message broker are down, ChiliLogger wil discard the log to keep the application running without raising any error. That's because raising errors would make the app crash and make it impossible to be used while the brokers are down. For this reason, it is important to use a main message broker and a fallback broker with high availability.
108
113
 
109
114
 
110
115
  ### Anatomy of a Chiligum Log
@@ -264,7 +269,28 @@ gem 'chili_logger'
264
269
 
265
270
  #### Basic Initialization
266
271
 
267
- Create a initializer file and configure ChiliLogger passing all the relevant information. It is recommended to deactivate ChiliLogger when running tests:
272
+ ChiliLogger currently relies on aws-sdk and, for that reason, requires you to intall and [configure it in your machine](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html).
273
+
274
+ To configure ChiliLogger, create a initializer file and configure it by passing all the relevant data. It is recommended to deactivate ChiliLogger when running tests:
275
+
276
+ ```ruby
277
+ # config/initializers/chili_logger.rb
278
+ require 'chili_logger'
279
+
280
+ is_deactivated = Rails.env.test?
281
+
282
+ ChiliLogger.instance.config({
283
+ deactivated: is_deactivated,
284
+ log_env: Rails.env, # sets the log's env attribute
285
+ log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
286
+ server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
287
+ cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute, usually it is set to 'aws' or nil
288
+ })
289
+ ```
290
+ The attributes `log_env` and `log_layer` set the fields ['env'](###environment) and ['layer'](###layer) in all logs that will be published by ChiliLogger. `server_url` and `cloud_provided` are not mandatory, but they provide additional ops data and are recommended when running in staging or production environments.
291
+
292
+ #### Customizing the Message Brokers
293
+ Usually, we want to allow the gem to use the default configuration for the [Main Message Broker and the Fallback Message Broker](#overview). However, if you need to customize it for testing or special cases, you can do it by passing the appropriate data to ChiliLogger when initializing it:
268
294
 
269
295
  ```ruby
270
296
  # config/initializers/chili_logger.rb
@@ -278,7 +304,7 @@ ChiliLogger.instance.config({
278
304
  log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
279
305
  server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
280
306
  cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute
281
- msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker
307
+ msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker; optional, defaults to :rabbitmq
282
308
  msg_broker_config: {
283
309
  user: ENV['RABBIT_USER'],
284
310
  password: ENV['RABBIT_PASSWORD'],
@@ -286,7 +312,7 @@ ChiliLogger.instance.config({
286
312
  port: ENV['RABBIT_PORT'],
287
313
  exchange_name: ENV['RABBIT_EXCHANGE'],
288
314
  },
289
- fallback_broker: :aws_sqs, # Chiligum's fallback option in case of Message Broker being down
315
+ fallback_broker: :aws_sqs, # ChiliLogger's fallback option in case of Message Broker being down; optional, defaults to :aws_sqs
290
316
  fallback_broker_config: {
291
317
  region: ENV['CHILI_LOGGER_SQS_HANDLER_REGION'],
292
318
  access_key_id: ENV['CHILI_LOGGER_SQS_HANDLER_KEY_ID'],
@@ -295,11 +321,7 @@ ChiliLogger.instance.config({
295
321
  }
296
322
  })
297
323
  ```
298
- The attributes `log_env` and `log_layer` set the fields ['env'](###environment) and ['layer'](###layer) in all logs that will be published by ChiliLogger.
299
-
300
- When configuring ChiliLogger, you MUST set `msg_broker_name` and `msg_broker_config`. All logs are published to the configured message broker and, without it, ChiliLogger can't work. For the time being, only `:rabbitmq` is supported.
301
-
302
- You ALSO MUST set `fallback_broker` and `fallback_broker_config`. This is the fallback option if the Message Broker is not available and, without it, ChiliLogger would break the application every time the Message Broker was down. For the time being, only `:aws_sqs` is supported.
324
+ For the time being, only `:rabbitmq` is supported as the `msg_broker_name` and only `:aws_sqs` is supported as the `fallback_broker`.
303
325
 
304
326
  Please note that if ChiliLogger tries to publish a log and both the configured Message Broker and the Error Handler are simultaneously down, the log will be discarded. This behavior makes sure logging problems never cause the app to break.
305
327
 
@@ -415,7 +437,7 @@ ChiliLogger.instance.publish_instant_log(desc: desc, user: user, main_content: m
415
437
  ```
416
438
 
417
439
  #### publish_instant_log default argument values
418
- Passing `desc`, `agent`, and `main_content` is optional, since all of them have default values. This is so ChiliLogger is resiliant and doesn't break if any of these arguments is forgotten. But notice that their default values are not very descriptive and it results in low-quality, almost useless logs:
440
+ Passing `desc`, `user`, and `main_content` is optional, since all of them have default values. This is so ChiliLogger is resiliant and doesn't break if any of these arguments is forgotten. But notice that their default values are not very descriptive and it results in low-quality, almost useless logs:
419
441
 
420
442
  ```ruby
421
443
  ChiliLogger.instance.publish_instant_log
@@ -448,19 +470,19 @@ ChiliLogger.instance.publish_instant_log
448
470
  #### ChiliLogger's Singleton pattern
449
471
  Notice that ChiliLogger uses the Singleton pattern. So, to call its methods, you must first access the instance method. Calling ChiliLogger.new will return an error:
450
472
  ```ruby
451
- ChiliLogger.publish_instant_log(desc, agent, log)
473
+ ChiliLogger.publish_instant_log(deasc: desc, user: user, main_content: main_content)
452
474
  # undefined method `publish_instant_log' for ChiliLogger:Class
453
475
 
454
- ChiliLogger.new.publish_instant_log(desc, agent, log)
476
+ ChiliLogger.new.publish_instant_log(deasc: desc, user: user, main_content: main_content)
455
477
  # NoMethodError: private method `new' called for ChiliLogger:Class
456
478
 
457
- ChiliLogger.instance.publish_instant_log(desc, agent, log)
479
+ ChiliLogger.instance.publish_instant_log(deasc: desc, user: user, main_content: main_content)
458
480
  # works fine
459
481
  ```
460
482
 
461
483
 
462
484
  ## Advanced Usage
463
- It will often happen that the information needed for a log is distributed in many parts of the application and can't be accessed in a single moment nor in a single place of the code base. For instance, you may want to set the log's agent with information about the current_user that's only available in the Controllers Layer. And you might want to add data to the log every time a DB record is modified, which is easier to do in the Models Layer. For these use cases, ChiliLogger's `publish_instant_log` will not be suitable and you should use more advanced features.
485
+ It will often happen that the information needed for a log is distributed in many parts of the application and can't be accessed in a single moment nor in a single place of the code base. For instance, you may want to set the log's user with information about the current_user that's only available in the Controllers Layer. And you might want to add data to the log every time a DB record is modified, which is easier to do in the Models Layer. For these use cases, ChiliLogger's `publish_instant_log` will not be suitable and you should use more advanced features.
464
486
 
465
487
  ChiliLogger takes advantage of Ruby's Thread class and allows you to start a log and keep adding information to it during the current thread's lifespan. This is particularly useful when logging requests to a controller, since these requests will run from beginning to end in a single thread.
466
488
 
@@ -472,8 +494,9 @@ Once a log is started, it can be accessed and modified by using ChiliLogger's `s
472
494
  class VideosController
473
495
  def create
474
496
  desc = { type: 'transaction', service: 'videos', action: 'create' }
475
- agent = { user: current_user.as_json, company: current_user.as_json }
476
- current_log = ChiliLogger.instance.start_new_log(desc, agent) # Step One
497
+ user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
498
+ company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
499
+ current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user) # Step One
477
500
 
478
501
  Video.create(title: 'Lateralus') # Step Two happens in the Video model
479
502
 
@@ -501,15 +524,12 @@ end
501
524
  "service": "videos",
502
525
  "action": "create",
503
526
  "desc": "development.creatives.transaction.videos.create",
504
- "agent": {
505
- "user": {
506
- "id": 88,
507
- "email": 'exemple@test.com'
508
- },
509
- "company": {
510
- "id": 75,
511
- "name": 'Chiligum'
512
- }
527
+ "user": {
528
+ "cognito_id": 'not_specified',
529
+ "email": 'not_specified',
530
+ "company_cognito_id": 'not_specified',
531
+ "company_name": 'not_specified',
532
+ "ghost_user_cognito_id": 'not_specified'
513
533
  },
514
534
  "transaction": {
515
535
  "modified_records": {
@@ -526,7 +546,7 @@ end
526
546
  }
527
547
  ```
528
548
 
529
- Notice that the log is started in VideosController#create with some initial info about the agent user and the log description; following, the same log has its main_content customized in Video.add_modified_record_to_log, by accessing ChiliLogger's `current_log` and calling its `add_modified_record` method. And, afterwards, the same log is finished and published with `current_log.publish`, again in VideosController#create.
549
+ Notice that the log is started in VideosController#create with some initial info about the user and the log description; following, the same log has its main_content customized in Video.add_modified_record_to_log, by accessing ChiliLogger's `current_log` and calling its `add_modified_record` method. And, afterwards, the same log is finished and published with `current_log.publish`, again in VideosController#create.
530
550
 
531
551
  Check the [Snippets For Quick Logging](##snippets-for-quick-logging) section to see how this functionality can be used to quickly set an all-embracing logging system for your application.
532
552
 
@@ -537,8 +557,9 @@ The controller code above could be improved by adding some logic to log unexpect
537
557
  class VideosController
538
558
  def create
539
559
  desc = { type: 'transaction', service: 'videos', action: 'create' }
540
- agent = { user: current_user.as_json, company: current_user.as_json } # .as_json converts ActiveReacords into Hash
541
- current_log = ChiliLogger.instance.start_new_log(desc, agent)
560
+ user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
561
+ company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
562
+ current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user)
542
563
 
543
564
  Video.create(title: 'Lateralus')
544
565
 
@@ -581,7 +602,7 @@ current_log.user
581
602
  # { cognito_id: 'not_specified', email: 'not_specified', company_cognito_id: 'not_specified',
582
603
  # company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
583
604
 
584
- current_log.update_user({ email: 'new_email' } })
605
+ current_log.update_user(email: 'new_email')
585
606
  current_log.user
586
607
  # { cognito_id: 'not_specified', email: 'new_email', company_cognito_id: 'not_specified',
587
608
  # company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
@@ -788,7 +809,20 @@ current_log.errors
788
809
  ```
789
810
 
790
811
  #### clear_log_info
791
- sets agent, desc, and main_content to their default values.
812
+ sets user, desc, and main_content to their default values.
813
+
814
+
815
+ ### Republishing Unpublished Logs
816
+ >
817
+ > This functionality was introduced in version 0.1.2 of ChiliLogger. If you are using ChiliLogger 0.0.12 or below, you must first upgrade it.
818
+ >
819
+
820
+ [As previously explained](#overview), if logs can't be published to the main `message broker`, then these unpublished logs will be sent to the `fallback broker`. We can easily tell ChiliLogger to try and republish these logs any time, just by running the following:
821
+
822
+ ```ruby
823
+ ChiliLogger.instance.republish_unpublished_logs
824
+ ```
825
+ When running the above snippet, ChiliLogger will fetch the unpublished logs and try to republish them to the main `message broker`. If the main message broker is still unavailable, ChiliLogger will once again send the logs to `fallback broker`.
792
826
 
793
827
 
794
828
  ## Snippets for Quicker Logging
@@ -824,8 +858,8 @@ class ApplicationController < ActionController::Base
824
858
  # sets papertrail Whodunit based on user defined in user_for_paper_trail
825
859
  before_action :set_paper_trail_whodunnit
826
860
 
827
- # customizes method implemented by PaperTrail and that sets PaperTrail.request.whodunit
828
- # PaperTrail.request.whodunit will be available in all parts of application while a request is being processed
861
+ # customizes method implemented by PaperTrail and that sets PaperTrail.request.whodunnit
862
+ # PaperTrail.request.whodunnit will be available in all parts of application while a request is being processed
829
863
  def user_for_paper_trail
830
864
  current_user
831
865
  end
@@ -865,7 +899,6 @@ class ApplicationController < ActionController::Base
865
899
 
866
900
  # if unexpected errors happen, will change log type and publish it for debugging/audit
867
901
  def publish_error_log(error)
868
- error = [error.inspect, Rails.backtrace_cleaner.clean(error.backtrace)]
869
902
  current_log = ChiliLogger.instance.current_log
870
903
 
871
904
  current_log.add_error(error)
@@ -885,10 +918,11 @@ class ApplicationController < ActionController::Base
885
918
  def log_user
886
919
  return {} unless current_user
887
920
 
888
- multi_user_record = MultiUser::User.find(current_user&.cognito_id)
921
+ # use .where instead of .find, because .find raises error if no record is found
922
+ multi_user_record = MultiUser::User.where(external_id: current_user&.cognito_id).first
889
923
  log_user = {
890
924
  email: current_user&.email,
891
- cognito_id: multi_user_record&.id,
925
+ cognito_id: multi_user_record&.external_id,
892
926
  company_name: current_user&.company&.name,
893
927
  company_cognito_id: multi_user_record&.organization&.id
894
928
  }
@@ -934,7 +968,7 @@ class ApplicationRecord < ActiveRecord::Base
934
968
  # enriches logs by automatically adding modified_records to them
935
969
  def add_modified_record_to_log(action_verb, modified_record = self)
936
970
  # only adds to log if record was created, changed or destroyed
937
- return if !new_record? && changes.empty? && action_verb != 'destroy'
971
+ return if !new_record? && !saved_changes? && action_verb != 'destroy'
938
972
 
939
973
  current_log = ChiliLogger.instance.current_log
940
974
  current_log.update_type('transaction_error') unless modified_record.errors.messages.empty?
@@ -998,6 +1032,10 @@ module Rake
998
1032
  rescue StandardError => error
999
1033
  current_log = ChiliLogger.instance.current_log
1000
1034
 
1035
+ current_task = Rake.application.top_level_tasks.first
1036
+ current_log.update_service(current_task.split(':').first)
1037
+ current_log.update_action(current_task.split(':').last)
1038
+
1001
1039
  current_log.add_error(error.inspect)
1002
1040
  current_log.update_type('uncaught_error')
1003
1041
  current_log.publish
@@ -1017,14 +1055,14 @@ namespace :excel do
1017
1055
  task validation: :environment do
1018
1056
  loop do
1019
1057
  # start storing infos for validation log
1020
- transaction_desc = { log_type: 'transaction', service: 'excel', action: 'validation' }
1058
+ transaction_desc = { type: 'transaction', service: 'excel', action: 'validation' }
1021
1059
  log_user = { email: 'automated_task', cognito_id: 'automated_task'
1022
1060
  current_log = ChiliLogger.instance.start_new_log(desc: log_desc, user: log_user)
1023
1061
 
1024
1062
  usual task code...
1025
1063
 
1026
1064
  # only publish log if current log had modified_records added to it - so we don't clutter DB with meaningless logs
1027
- current_log.publish if current_log.modified_records
1065
+ current_log.publish unless current_log.modified_records.empty?
1028
1066
 
1029
1067
  sleep 5
1030
1068
  end
@@ -1041,7 +1079,7 @@ Unfortunately, due to the way most of our tasks are implemented - as infinite lo
1041
1079
  namespace :excel do
1042
1080
  task validation: :environment do
1043
1081
  loop do
1044
- heartbeat_desc = { log_type: 'monitoring', service: 'excel', action: 'validation' }
1082
+ heartbeat_desc = { type: 'monitoring', service: 'excel', action: 'validation' }
1045
1083
  log_user = { email: 'automated_task', cognito_id: 'automated_task'
1046
1084
  ChiliLogger.instance.publish_instant_log(desc: heartbeat_desc, user: log_user)
1047
1085