chili_logger 0.0.9 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.byebug_history +29 -113
- data/.gitignore +2 -0
- data/Gemfile +3 -2
- data/Gemfile.lock +20 -13
- data/README.md +114 -92
- data/chili_logger.gemspec +2 -1
- data/lib/brokers/main_broker.rb +47 -0
- data/lib/brokers/rabbit_broker.rb +18 -37
- data/lib/brokers/sqs_broker.rb +43 -19
- data/lib/chili_logger.rb +16 -29
- data/lib/chili_logger/version.rb +1 -1
- data/lib/current_log_accessor.rb +1 -5
- data/lib/errors/logging_error_handler/logging_error_handler.rb +23 -8
- data/lib/helpers/logs_coverage/coverage_writer.rb +4 -2
- data/lib/helpers/values/default.rb +2 -2
- data/lib/helpers/values/secrets/aws_secrets_manager.rb +63 -0
- data/lib/helpers/values/secrets/secrets.rb +29 -0
- data/lib/unpublished_logs_manager/unpublished_logs_manager.rb +46 -0
- data/log/chili-logger-coverage.yml +8 -2
- metadata +22 -13
- data/chili_logger-0.0.6.gem +0 -0
- data/chili_logger-0.0.7.gem +0 -0
- data/chili_logger-0.0.8.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61d8df3241131a360ca82cbc122ccde86989a46b8b67227e9f686136b792115b
|
4
|
+
data.tar.gz: 5250e4ba9b5f27b8ed56ef0d6bb619b306783c390c17d27ee34dada2e29a862b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00fbfa5e42ac2ec4190665535d7e30d6faf47528a0be7924714a15a6dda60dcf65d9c629ddec9b1201d69164201df4cde86fd07a3191b4fabad189ba65046ebe
|
7
|
+
data.tar.gz: e1a02abebf9d9f437d707647d94c26bd03f22853029f3053c94cf05ec7ebaa1920482bf0cef6eb6499cdae9f4301dfe17c361a38387e3e7c447de47d64fbf3a9
|
data/.byebug_history
CHANGED
@@ -1,126 +1,42 @@
|
|
1
1
|
continue
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
continue
|
6
|
-
@desc_uniformizer.desc([])
|
7
|
-
@desc_uniformizer.desc(options[:desc])
|
8
|
-
@desc_uniformizer.desc
|
9
|
-
@desc_uniformizer.desc(options[:desc])
|
10
|
-
options
|
11
|
-
continue
|
12
|
-
options
|
13
|
-
continue
|
14
|
-
main_content.merge(uniformized_content)
|
15
|
-
main_content
|
16
|
-
continue
|
17
|
-
main_content
|
18
|
-
uniformized_content
|
19
|
-
continue
|
20
|
-
desc
|
21
|
-
continue
|
22
|
-
uniformized_content
|
23
|
-
continue
|
24
|
-
uniformized_content
|
2
|
+
@sqs_config[:region]
|
3
|
+
@sqs_config['region']
|
4
|
+
@sqs_config
|
25
5
|
quit
|
26
|
-
|
27
|
-
val.first.is_a?(Hash) ? {} : record
|
28
|
-
modified_record(val.first)
|
29
|
-
val
|
30
|
-
uniformized_val
|
6
|
+
queue_url(@queue_name)
|
31
7
|
quit
|
32
|
-
|
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
|
8
|
+
queue_url(@queue_name)
|
53
9
|
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
10
|
continue
|
61
|
-
|
11
|
+
queue_url(@queue_name)
|
62
12
|
continue
|
63
|
-
|
64
|
-
|
65
|
-
Thread.current[:current_log_user] ||= default_user
|
13
|
+
mock_broker.class.ancestors
|
14
|
+
mock_broker.ancestors
|
66
15
|
continue
|
67
|
-
|
16
|
+
stubbed_sqs_poller.poll
|
17
|
+
stubbed_sqs_poller.poll { |msg| }
|
18
|
+
stubbed_sqs_poller.poll {}
|
19
|
+
stubbed_sqs_poller.poll
|
20
|
+
stubbed_sqs_poller
|
21
|
+
Aws::SQS::QueuePoller.new(stub_responses: true).poll
|
22
|
+
ENV['AWS_REGION'] = 'us-east-1'
|
23
|
+
stubbed_sqs_poller.poll
|
68
24
|
continue
|
69
|
-
|
25
|
+
ChiliLogger.instance.instance_variable_get('@unpublished_logs_manager')
|
26
|
+
mock_unpub_logs_mngr == ChiliLogger.instance.instance_variable_get('@unpublished_logs_manager')
|
27
|
+
mock_unpub_logs_mngr
|
70
28
|
continue
|
71
|
-
|
72
|
-
|
29
|
+
file
|
30
|
+
throw :stop_polling
|
73
31
|
continue
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
-
continue
|
103
|
-
@cloud_provider
|
104
|
-
continue
|
105
|
-
@cloud_provider
|
106
|
-
continue
|
107
|
-
cloud_metadata[nil]
|
108
|
-
@cloud_provider
|
109
|
-
continue
|
110
|
-
@ip
|
111
|
-
@user
|
112
|
-
@port
|
113
|
-
continue
|
114
|
-
msg[type.to_sym]
|
115
|
-
msg[type]
|
116
|
-
pp msg
|
117
|
-
p msg
|
118
|
-
pp msg
|
119
|
-
msg
|
120
|
-
type
|
121
|
-
continue
|
122
|
-
mock_current_log.clear_log_info
|
123
|
-
mock_current_log
|
32
|
+
error
|
33
|
+
@rabbit_exchange.publish(log.to_json, routing_key: routing_key)
|
34
|
+
routing_key = log['desc']
|
35
|
+
log = JSON.parse(sqs_msg.body)['body']['log']
|
36
|
+
@rabbit_exchange.publish(log.to_json, routing_key: routing_key)
|
37
|
+
routing_key = log['desc']
|
38
|
+
log = JSON.parse(sqs_msg.body)['body']['log']
|
39
|
+
@rabbit_exchange
|
124
40
|
continue
|
125
41
|
n
|
126
42
|
item_name
|
data/.gitignore
CHANGED
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', '
|
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
|
17
|
+
gem 'simplecov', '~> 0.17.1'
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
chili_logger (0.
|
5
|
-
aws-sdk (~>
|
4
|
+
chili_logger (0.1.2)
|
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.
|
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-
|
28
|
-
|
29
|
-
|
30
|
-
aws-
|
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-
|
33
|
-
aws-sdk-core (
|
34
|
-
|
35
|
-
|
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.
|
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 (
|
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,62 @@
|
|
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](
|
7
|
-
* [How it Works](
|
8
|
-
* [Overview](
|
9
|
-
* [Anatomy of a Chiligum Log](
|
10
|
-
* [Example Log](
|
11
|
-
* [Environment](
|
12
|
-
* [Layer](
|
13
|
-
* [Type](
|
14
|
-
* [Service](
|
15
|
-
* [Action](
|
16
|
-
* [Description](
|
17
|
-
* [User](
|
18
|
-
* [Main Content](
|
19
|
-
* [Ops Metadata](
|
20
|
-
* [Timestamp](
|
21
|
-
* [A Primer on RabbitMQ and ElasticSearch](
|
22
|
-
* [RabbitMQ](
|
23
|
-
* [ElasticSearch](
|
24
|
-
* [Putting It All Together](
|
25
|
-
* [Installation](
|
26
|
-
* [Basic
|
27
|
-
* [
|
28
|
-
|
29
|
-
* [publish_instant_log
|
30
|
-
* [
|
31
|
-
* [
|
32
|
-
* [
|
33
|
-
|
34
|
-
* [
|
35
|
-
|
36
|
-
|
37
|
-
* [
|
38
|
-
* [
|
39
|
-
* [
|
40
|
-
* [
|
41
|
-
* [
|
42
|
-
* [
|
43
|
-
* [
|
44
|
-
* [
|
45
|
-
* [
|
46
|
-
* [
|
47
|
-
* [
|
48
|
-
* [
|
49
|
-
* [
|
50
|
-
* [
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
* [
|
57
|
-
|
58
|
-
* [Logging
|
59
|
-
* [
|
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
|
+
* [Snippets For Quicker Logging](#snippets-for-quicker-logging)
|
53
|
+
* [Papertrail Optional Use](#papertrail-optional-use)
|
54
|
+
* [Logging Transactions in HTP Requests](#logging-transactions-in-http-requests)
|
55
|
+
* [Controllers](#controllers-logging-logic)
|
56
|
+
* [Models](#models-logging-logic)
|
57
|
+
* [Logging Rake Tasks](#logging-rake-tasks)
|
58
|
+
* [Logging Uncaught task Errors](#logging-uncaught-task-errors)
|
59
|
+
* [Logging Transactions in Tasks](#logging-transactions-in-tasks)
|
60
|
+
* [Logging Heartbeats in Tasks](#logging-heartbeats-in-tasks)
|
61
|
+
* [Coverage](#Coverage)
|
60
62
|
|
61
63
|
## READ BEFORE CONTRIBUTING
|
62
64
|
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:
|
@@ -264,7 +266,28 @@ gem 'chili_logger'
|
|
264
266
|
|
265
267
|
#### Basic Initialization
|
266
268
|
|
267
|
-
|
269
|
+
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).
|
270
|
+
|
271
|
+
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:
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
# config/initializers/chili_logger.rb
|
275
|
+
require 'chili_logger'
|
276
|
+
|
277
|
+
is_deactivated = Rails.env.test?
|
278
|
+
|
279
|
+
ChiliLogger.instance.config({
|
280
|
+
deactivated: is_deactivated,
|
281
|
+
log_env: Rails.env, # sets the log's env attribute
|
282
|
+
log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
|
283
|
+
server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
|
284
|
+
cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute, usually it is set to 'aws' or nil
|
285
|
+
})
|
286
|
+
```
|
287
|
+
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.
|
288
|
+
|
289
|
+
#### Customizing the Message Brokers
|
290
|
+
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
291
|
|
269
292
|
```ruby
|
270
293
|
# config/initializers/chili_logger.rb
|
@@ -278,7 +301,7 @@ ChiliLogger.instance.config({
|
|
278
301
|
log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
|
279
302
|
server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
|
280
303
|
cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute
|
281
|
-
msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker
|
304
|
+
msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker; optional, defaults to :rabbitmq
|
282
305
|
msg_broker_config: {
|
283
306
|
user: ENV['RABBIT_USER'],
|
284
307
|
password: ENV['RABBIT_PASSWORD'],
|
@@ -286,7 +309,7 @@ ChiliLogger.instance.config({
|
|
286
309
|
port: ENV['RABBIT_PORT'],
|
287
310
|
exchange_name: ENV['RABBIT_EXCHANGE'],
|
288
311
|
},
|
289
|
-
fallback_broker: :aws_sqs, #
|
312
|
+
fallback_broker: :aws_sqs, # ChiliLogger's fallback option in case of Message Broker being down; optional, defaults to :aws_sqs
|
290
313
|
fallback_broker_config: {
|
291
314
|
region: ENV['CHILI_LOGGER_SQS_HANDLER_REGION'],
|
292
315
|
access_key_id: ENV['CHILI_LOGGER_SQS_HANDLER_KEY_ID'],
|
@@ -295,11 +318,7 @@ ChiliLogger.instance.config({
|
|
295
318
|
}
|
296
319
|
})
|
297
320
|
```
|
298
|
-
|
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.
|
321
|
+
For the time being, only `:rabbitmq` is supported as the `msg_broker_name` and only `:aws_sqs` is supported as the `fallback_broker`.
|
303
322
|
|
304
323
|
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
324
|
|
@@ -415,7 +434,7 @@ ChiliLogger.instance.publish_instant_log(desc: desc, user: user, main_content: m
|
|
415
434
|
```
|
416
435
|
|
417
436
|
#### publish_instant_log default argument values
|
418
|
-
Passing `desc`, `
|
437
|
+
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
438
|
|
420
439
|
```ruby
|
421
440
|
ChiliLogger.instance.publish_instant_log
|
@@ -448,19 +467,19 @@ ChiliLogger.instance.publish_instant_log
|
|
448
467
|
#### ChiliLogger's Singleton pattern
|
449
468
|
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
469
|
```ruby
|
451
|
-
ChiliLogger.publish_instant_log(desc,
|
470
|
+
ChiliLogger.publish_instant_log(deasc: desc, user: user, main_content: main_content)
|
452
471
|
# undefined method `publish_instant_log' for ChiliLogger:Class
|
453
472
|
|
454
|
-
ChiliLogger.new.publish_instant_log(desc,
|
473
|
+
ChiliLogger.new.publish_instant_log(deasc: desc, user: user, main_content: main_content)
|
455
474
|
# NoMethodError: private method `new' called for ChiliLogger:Class
|
456
475
|
|
457
|
-
ChiliLogger.instance.publish_instant_log(desc,
|
476
|
+
ChiliLogger.instance.publish_instant_log(deasc: desc, user: user, main_content: main_content)
|
458
477
|
# works fine
|
459
478
|
```
|
460
479
|
|
461
480
|
|
462
481
|
## 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
|
482
|
+
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
483
|
|
465
484
|
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
485
|
|
@@ -472,8 +491,9 @@ Once a log is started, it can be accessed and modified by using ChiliLogger's `s
|
|
472
491
|
class VideosController
|
473
492
|
def create
|
474
493
|
desc = { type: 'transaction', service: 'videos', action: 'create' }
|
475
|
-
|
476
|
-
|
494
|
+
user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
|
495
|
+
company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
|
496
|
+
current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user) # Step One
|
477
497
|
|
478
498
|
Video.create(title: 'Lateralus') # Step Two happens in the Video model
|
479
499
|
|
@@ -501,15 +521,12 @@ end
|
|
501
521
|
"service": "videos",
|
502
522
|
"action": "create",
|
503
523
|
"desc": "development.creatives.transaction.videos.create",
|
504
|
-
"
|
505
|
-
"
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
"
|
510
|
-
"id": 75,
|
511
|
-
"name": 'Chiligum'
|
512
|
-
}
|
524
|
+
"user": {
|
525
|
+
"cognito_id": 'not_specified',
|
526
|
+
"email": 'not_specified',
|
527
|
+
"company_cognito_id": 'not_specified',
|
528
|
+
"company_name": 'not_specified',
|
529
|
+
"ghost_user_cognito_id": 'not_specified'
|
513
530
|
},
|
514
531
|
"transaction": {
|
515
532
|
"modified_records": {
|
@@ -526,7 +543,7 @@ end
|
|
526
543
|
}
|
527
544
|
```
|
528
545
|
|
529
|
-
Notice that the log is started in VideosController#create with some initial info about the
|
546
|
+
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
547
|
|
531
548
|
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
549
|
|
@@ -537,8 +554,9 @@ The controller code above could be improved by adding some logic to log unexpect
|
|
537
554
|
class VideosController
|
538
555
|
def create
|
539
556
|
desc = { type: 'transaction', service: 'videos', action: 'create' }
|
540
|
-
|
541
|
-
|
557
|
+
user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
|
558
|
+
company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
|
559
|
+
current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user)
|
542
560
|
|
543
561
|
Video.create(title: 'Lateralus')
|
544
562
|
|
@@ -581,7 +599,7 @@ current_log.user
|
|
581
599
|
# { cognito_id: 'not_specified', email: 'not_specified', company_cognito_id: 'not_specified',
|
582
600
|
# company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
|
583
601
|
|
584
|
-
current_log.update_user(
|
602
|
+
current_log.update_user(email: 'new_email')
|
585
603
|
current_log.user
|
586
604
|
# { cognito_id: 'not_specified', email: 'new_email', company_cognito_id: 'not_specified',
|
587
605
|
# company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
|
@@ -788,7 +806,7 @@ current_log.errors
|
|
788
806
|
```
|
789
807
|
|
790
808
|
#### clear_log_info
|
791
|
-
sets
|
809
|
+
sets user, desc, and main_content to their default values.
|
792
810
|
|
793
811
|
|
794
812
|
## Snippets for Quicker Logging
|
@@ -824,8 +842,8 @@ class ApplicationController < ActionController::Base
|
|
824
842
|
# sets papertrail Whodunit based on user defined in user_for_paper_trail
|
825
843
|
before_action :set_paper_trail_whodunnit
|
826
844
|
|
827
|
-
# customizes method implemented by PaperTrail and that sets PaperTrail.request.
|
828
|
-
# PaperTrail.request.
|
845
|
+
# customizes method implemented by PaperTrail and that sets PaperTrail.request.whodunnit
|
846
|
+
# PaperTrail.request.whodunnit will be available in all parts of application while a request is being processed
|
829
847
|
def user_for_paper_trail
|
830
848
|
current_user
|
831
849
|
end
|
@@ -865,7 +883,6 @@ class ApplicationController < ActionController::Base
|
|
865
883
|
|
866
884
|
# if unexpected errors happen, will change log type and publish it for debugging/audit
|
867
885
|
def publish_error_log(error)
|
868
|
-
error = [error.inspect, Rails.backtrace_cleaner.clean(error.backtrace)]
|
869
886
|
current_log = ChiliLogger.instance.current_log
|
870
887
|
|
871
888
|
current_log.add_error(error)
|
@@ -885,10 +902,11 @@ class ApplicationController < ActionController::Base
|
|
885
902
|
def log_user
|
886
903
|
return {} unless current_user
|
887
904
|
|
888
|
-
|
905
|
+
# use .where instead of .find, because .find raises error if no record is found
|
906
|
+
multi_user_record = MultiUser::User.where(external_id: current_user&.cognito_id).first
|
889
907
|
log_user = {
|
890
908
|
email: current_user&.email,
|
891
|
-
cognito_id: multi_user_record&.
|
909
|
+
cognito_id: multi_user_record&.external_id,
|
892
910
|
company_name: current_user&.company&.name,
|
893
911
|
company_cognito_id: multi_user_record&.organization&.id
|
894
912
|
}
|
@@ -934,7 +952,7 @@ class ApplicationRecord < ActiveRecord::Base
|
|
934
952
|
# enriches logs by automatically adding modified_records to them
|
935
953
|
def add_modified_record_to_log(action_verb, modified_record = self)
|
936
954
|
# only adds to log if record was created, changed or destroyed
|
937
|
-
return if !new_record? &&
|
955
|
+
return if !new_record? && !saved_changes? && action_verb != 'destroy'
|
938
956
|
|
939
957
|
current_log = ChiliLogger.instance.current_log
|
940
958
|
current_log.update_type('transaction_error') unless modified_record.errors.messages.empty?
|
@@ -998,6 +1016,10 @@ module Rake
|
|
998
1016
|
rescue StandardError => error
|
999
1017
|
current_log = ChiliLogger.instance.current_log
|
1000
1018
|
|
1019
|
+
current_task = Rake.application.top_level_tasks.first
|
1020
|
+
current_log.update_service(current_task.split(':').first)
|
1021
|
+
current_log.update_action(current_task.split(':').last)
|
1022
|
+
|
1001
1023
|
current_log.add_error(error.inspect)
|
1002
1024
|
current_log.update_type('uncaught_error')
|
1003
1025
|
current_log.publish
|
@@ -1017,14 +1039,14 @@ namespace :excel do
|
|
1017
1039
|
task validation: :environment do
|
1018
1040
|
loop do
|
1019
1041
|
# start storing infos for validation log
|
1020
|
-
transaction_desc = {
|
1042
|
+
transaction_desc = { type: 'transaction', service: 'excel', action: 'validation' }
|
1021
1043
|
log_user = { email: 'automated_task', cognito_id: 'automated_task'
|
1022
1044
|
current_log = ChiliLogger.instance.start_new_log(desc: log_desc, user: log_user)
|
1023
1045
|
|
1024
1046
|
usual task code...
|
1025
1047
|
|
1026
1048
|
# 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
|
1049
|
+
current_log.publish unless current_log.modified_records.empty?
|
1028
1050
|
|
1029
1051
|
sleep 5
|
1030
1052
|
end
|
@@ -1041,7 +1063,7 @@ Unfortunately, due to the way most of our tasks are implemented - as infinite lo
|
|
1041
1063
|
namespace :excel do
|
1042
1064
|
task validation: :environment do
|
1043
1065
|
loop do
|
1044
|
-
heartbeat_desc = {
|
1066
|
+
heartbeat_desc = { type: 'monitoring', service: 'excel', action: 'validation' }
|
1045
1067
|
log_user = { email: 'automated_task', cognito_id: 'automated_task'
|
1046
1068
|
ChiliLogger.instance.publish_instant_log(desc: heartbeat_desc, user: log_user)
|
1047
1069
|
|