chili_logger 0.0.11 → 0.1.5

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: d917b27026162b0d5c123b44dbdd9d3ae5ad6be1517785a8513967fbc9dad3ef
4
- data.tar.gz: e9e7806e3ce5bc7e701aa1984d7f5bf5ece59392427a7a34496c6f20c9291127
3
+ metadata.gz: 12fce704cc86d96f2c9a4f140d57d8f12c57b048a6abfec1ef15e1841a7df40f
4
+ data.tar.gz: 96027ffc004a5d4270eea836f4830f6a9e33da4dea94b01f0d87044860441024
5
5
  SHA512:
6
- metadata.gz: 2960caed15b4d69719bd351801a25b89e73e5c2874a2c17263a72baed7711073d01debaae85dcff517c4128e48646b9d3b479fe99515f741267a3db986fd59df
7
- data.tar.gz: 0b8116d232569e56b148499663ea6993ef4bd222a835125a96e5b4407ea2a4137ffb0f3d15940824644c419f8a9e20625087572ecf57d261cb6efd09c86a1631
6
+ metadata.gz: e23c0ba18ea16203c6084ad2c1c5ba7bb8846800cd2a087cc58836f098b36f7098557ea7a1757a0c1a8156afbbf08ac25e5f2de8417b782dff8f3073bcd2da24
7
+ data.tar.gz: 76df33e958cf2ff840a0ea28bc674bd5c702f7843c209eac75063edf23178c71a8d3763b580ba3ccda4dc2da744b19be42c2e9e3283ac5df6402231e3b865560
@@ -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
@@ -1,5 +1,12 @@
1
+
1
2
  AllCops:
2
3
  TargetRubyVersion: 2.3.3
4
+ Exclude:
5
+ - spec/**/**.rb
6
+ - Gemfile
7
+ - chili_logger.gemspec
8
+ - local_tests_cheatsheet.rb
9
+ - bin/**/*
3
10
  Metrics/LineLength:
4
11
  Max: 120
5
12
  Metrics/BlockLength:
@@ -8,3 +15,5 @@ Metrics/ModuleLength:
8
15
  Max: 180
9
16
  Style/FrozenStringLiteralComment:
10
17
  Enabled: false
18
+ Style/DateTime:
19
+ Enabled: false
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.11)
5
- aws-sdk (~> 2.9, >= 2.9.0)
4
+ chili_logger (0.1.4)
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
@@ -162,6 +167,7 @@ Also a tagging attribute, tells what type of log it is. Chiligum has 3 main type
162
167
  - `transaction` is used for logging transactions made by users, such as creating a campaign, uploading a file to the gallery, etc...
163
168
  - `uncaught_error` is used for logging internal server errors and to help us find problematic points in our applications;
164
169
  - `monitoring` is used to monitor the health of our servers, providing information such as CPU usage, uptime, etc...
170
+ - `log` is used to record steps of processing flowing among the platform layers, these records help debug and find errors.
165
171
 
166
172
  ChiliLogger is concerned only with `transaction` and `uncaught_errors`, since they are the only ones that can be generated in the application itself. Monitoring logs are created by other monitoring services, like ElasticSearch Beats or AWS Cloudwatch.
167
173
 
@@ -264,7 +270,28 @@ gem 'chili_logger'
264
270
 
265
271
  #### Basic Initialization
266
272
 
267
- Create a initializer file and configure ChiliLogger passing all the relevant information. It is recommended to deactivate ChiliLogger when running tests:
273
+ 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).
274
+
275
+ 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:
276
+
277
+ ```ruby
278
+ # config/initializers/chili_logger.rb
279
+ require 'chili_logger'
280
+
281
+ is_deactivated = Rails.env.test?
282
+
283
+ ChiliLogger.instance.config({
284
+ deactivated: is_deactivated,
285
+ log_env: Rails.env, # sets the log's env attribute
286
+ log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
287
+ server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
288
+ cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute, usually it is set to 'aws' or nil
289
+ })
290
+ ```
291
+ 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.
292
+
293
+ #### Customizing the Message Brokers
294
+ 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
295
 
269
296
  ```ruby
270
297
  # config/initializers/chili_logger.rb
@@ -278,7 +305,7 @@ ChiliLogger.instance.config({
278
305
  log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
279
306
  server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
280
307
  cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute
281
- msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker
308
+ msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker; optional, defaults to :rabbitmq
282
309
  msg_broker_config: {
283
310
  user: ENV['RABBIT_USER'],
284
311
  password: ENV['RABBIT_PASSWORD'],
@@ -286,7 +313,7 @@ ChiliLogger.instance.config({
286
313
  port: ENV['RABBIT_PORT'],
287
314
  exchange_name: ENV['RABBIT_EXCHANGE'],
288
315
  },
289
- fallback_broker: :aws_sqs, # Chiligum's fallback option in case of Message Broker being down
316
+ fallback_broker: :aws_sqs, # ChiliLogger's fallback option in case of Message Broker being down; optional, defaults to :aws_sqs
290
317
  fallback_broker_config: {
291
318
  region: ENV['CHILI_LOGGER_SQS_HANDLER_REGION'],
292
319
  access_key_id: ENV['CHILI_LOGGER_SQS_HANDLER_KEY_ID'],
@@ -295,11 +322,7 @@ ChiliLogger.instance.config({
295
322
  }
296
323
  })
297
324
  ```
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.
325
+ For the time being, only `:rabbitmq` is supported as the `msg_broker_name` and only `:aws_sqs` is supported as the `fallback_broker`.
303
326
 
304
327
  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
328
 
@@ -415,7 +438,7 @@ ChiliLogger.instance.publish_instant_log(desc: desc, user: user, main_content: m
415
438
  ```
416
439
 
417
440
  #### 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:
441
+ 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
442
 
420
443
  ```ruby
421
444
  ChiliLogger.instance.publish_instant_log
@@ -448,19 +471,19 @@ ChiliLogger.instance.publish_instant_log
448
471
  #### ChiliLogger's Singleton pattern
449
472
  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
473
  ```ruby
451
- ChiliLogger.publish_instant_log(desc, agent, log)
474
+ ChiliLogger.publish_instant_log(deasc: desc, user: user, main_content: main_content)
452
475
  # undefined method `publish_instant_log' for ChiliLogger:Class
453
476
 
454
- ChiliLogger.new.publish_instant_log(desc, agent, log)
477
+ ChiliLogger.new.publish_instant_log(deasc: desc, user: user, main_content: main_content)
455
478
  # NoMethodError: private method `new' called for ChiliLogger:Class
456
479
 
457
- ChiliLogger.instance.publish_instant_log(desc, agent, log)
480
+ ChiliLogger.instance.publish_instant_log(deasc: desc, user: user, main_content: main_content)
458
481
  # works fine
459
482
  ```
460
483
 
461
484
 
462
485
  ## 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.
486
+ 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
487
 
465
488
  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
489
 
@@ -472,8 +495,9 @@ Once a log is started, it can be accessed and modified by using ChiliLogger's `s
472
495
  class VideosController
473
496
  def create
474
497
  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
498
+ user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
499
+ company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
500
+ current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user) # Step One
477
501
 
478
502
  Video.create(title: 'Lateralus') # Step Two happens in the Video model
479
503
 
@@ -501,15 +525,12 @@ end
501
525
  "service": "videos",
502
526
  "action": "create",
503
527
  "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
- }
528
+ "user": {
529
+ "cognito_id": 'not_specified',
530
+ "email": 'not_specified',
531
+ "company_cognito_id": 'not_specified',
532
+ "company_name": 'not_specified',
533
+ "ghost_user_cognito_id": 'not_specified'
513
534
  },
514
535
  "transaction": {
515
536
  "modified_records": {
@@ -526,7 +547,7 @@ end
526
547
  }
527
548
  ```
528
549
 
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.
550
+ 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
551
 
531
552
  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
553
 
@@ -537,8 +558,9 @@ The controller code above could be improved by adding some logic to log unexpect
537
558
  class VideosController
538
559
  def create
539
560
  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)
561
+ user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
562
+ company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
563
+ current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user)
542
564
 
543
565
  Video.create(title: 'Lateralus')
544
566
 
@@ -581,7 +603,7 @@ current_log.user
581
603
  # { cognito_id: 'not_specified', email: 'not_specified', company_cognito_id: 'not_specified',
582
604
  # company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
583
605
 
584
- current_log.update_user({ email: 'new_email' } })
606
+ current_log.update_user(email: 'new_email')
585
607
  current_log.user
586
608
  # { cognito_id: 'not_specified', email: 'new_email', company_cognito_id: 'not_specified',
587
609
  # company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
@@ -788,7 +810,20 @@ current_log.errors
788
810
  ```
789
811
 
790
812
  #### clear_log_info
791
- sets agent, desc, and main_content to their default values.
813
+ sets user, desc, and main_content to their default values.
814
+
815
+
816
+ ### Republishing Unpublished Logs
817
+ >
818
+ > 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.
819
+ >
820
+
821
+ [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:
822
+
823
+ ```ruby
824
+ ChiliLogger.instance.republish_unpublished_logs
825
+ ```
826
+ 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
827
 
793
828
 
794
829
  ## Snippets for Quicker Logging
@@ -824,8 +859,8 @@ class ApplicationController < ActionController::Base
824
859
  # sets papertrail Whodunit based on user defined in user_for_paper_trail
825
860
  before_action :set_paper_trail_whodunnit
826
861
 
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
862
+ # customizes method implemented by PaperTrail and that sets PaperTrail.request.whodunnit
863
+ # PaperTrail.request.whodunnit will be available in all parts of application while a request is being processed
829
864
  def user_for_paper_trail
830
865
  current_user
831
866
  end
@@ -865,7 +900,6 @@ class ApplicationController < ActionController::Base
865
900
 
866
901
  # if unexpected errors happen, will change log type and publish it for debugging/audit
867
902
  def publish_error_log(error)
868
- error = [error.inspect, Rails.backtrace_cleaner.clean(error.backtrace)]
869
903
  current_log = ChiliLogger.instance.current_log
870
904
 
871
905
  current_log.add_error(error)
@@ -889,7 +923,7 @@ class ApplicationController < ActionController::Base
889
923
  multi_user_record = MultiUser::User.where(external_id: current_user&.cognito_id).first
890
924
  log_user = {
891
925
  email: current_user&.email,
892
- cognito_id: multi_user_record&.id,
926
+ cognito_id: multi_user_record&.external_id,
893
927
  company_name: current_user&.company&.name,
894
928
  company_cognito_id: multi_user_record&.organization&.id
895
929
  }
@@ -930,12 +964,10 @@ class ApplicationRecord < ActiveRecord::Base
930
964
  after_update -> { add_modified_record_to_log('update') }, on: :update
931
965
  before_destroy -> { add_modified_record_to_log('destroy') }, on: :destroy
932
966
 
933
- has_paper_trail # if you are using paper_trail
934
-
935
967
  # enriches logs by automatically adding modified_records to them
936
968
  def add_modified_record_to_log(action_verb, modified_record = self)
937
969
  # only adds to log if record was created, changed or destroyed
938
- return if !new_record? && changes.empty? && action_verb != 'destroy'
970
+ return if !new_record? && !saved_changes? && action_verb != 'destroy'
939
971
 
940
972
  current_log = ChiliLogger.instance.current_log
941
973
  current_log.update_type('transaction_error') unless modified_record.errors.messages.empty?
@@ -945,7 +977,7 @@ class ApplicationRecord < ActiveRecord::Base
945
977
  # ChiliLogger requires modified_records to be hashes.
946
978
  # This method converts ActiveRecords instances to hashes and adds some extra useful data
947
979
  def to_denormalized_hash(record_hash = as_json)
948
- record_hash[:last_changes] = versions.last.changeset if versions.last #if you are using paper_trail
980
+ record_hash[:last_changes] = saved_changes
949
981
  record_hash[:errors] = errors
950
982
 
951
983
  record_hash