cx_log 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +75 -8
- data/lib/cx_log/log.rb +18 -2
- data/lib/cx_log/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77157e52dd674aaf049dcd64095bc28d917c0cdeedbc8075a6414fbc7843e50f
|
4
|
+
data.tar.gz: 1d833fa429d8f00fc7380d86dd1b9ff5d61847b5739613ef316654c1edd42979
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7261d32b7baf464ad4f0da80f04f09758db05721353367f534f8a28b99fe58d08a9197dd4d39ad57dfdd52aa7e9bcef006e91f626d65aabcfa4fca384cfd543e
|
7
|
+
data.tar.gz: 03a2cf1022dee28cc7d6758cb82a4afcc35f9af0e13c7d0a5a68aa3c17f6bedde8fbbe9e2a0435d19da33ccedd9b951117c63a259fd40885a3c2c5a28265b5c6
|
data/README.md
CHANGED
@@ -1,24 +1,91 @@
|
|
1
1
|
# CxLog
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cx_log`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
Trying to debug an issue by looking at multiple logs is often cumbersome.
|
4
|
+
CxLog allows you to create a single log line for a context (request, background job, etc.) in your application.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
-
|
11
8
|
Install the gem and add to the application's Gemfile by executing:
|
12
9
|
|
13
|
-
|
10
|
+
```bash
|
11
|
+
$ bundle add cx_log
|
12
|
+
```
|
14
13
|
|
15
14
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
15
|
|
17
|
-
|
16
|
+
```bash
|
17
|
+
$ gem install cx_log
|
18
|
+
```
|
18
19
|
|
19
20
|
## Usage
|
20
21
|
|
21
|
-
|
22
|
+
### Basic Usage
|
23
|
+
CxLog generates structured logs. All logs will have a `message` attribute (that can be overwritten) using `CxLog.add`.
|
24
|
+
In order to add an attribute the log, use `CxLog.add`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
CxLog.log(message: 'test', foo: 'bar')
|
28
|
+
```
|
29
|
+
Flushing the log will generate `{"message":"test","foo":"bar"}`.
|
30
|
+
|
31
|
+
CxLog supports multiple values per key:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
CxLog.log(message: 'test', foo: 'bar')
|
35
|
+
# some code
|
36
|
+
CxLog.add(foo: 'baz')
|
37
|
+
```
|
38
|
+
|
39
|
+
Flushing the log will generate `{"message":"test","foo":["bar","baz"]}`.
|
40
|
+
|
41
|
+
### Rails
|
42
|
+
If you are using Rails, you can use the CxLog middleware to automatically generate context logs.
|
43
|
+
|
44
|
+
Add the following to your `config/application.rb`:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
config.middleware.use CxLog::Middleware
|
48
|
+
```
|
49
|
+
|
50
|
+
### Log Format
|
51
|
+
By default, CxLog generates logs in JSON format.
|
52
|
+
You can use your own or choose one that is shipped with the gem
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
CxLog.options = {
|
56
|
+
formatter: CxLog::Formatters::KeyValueFormatter.new
|
57
|
+
}
|
58
|
+
```
|
59
|
+
|
60
|
+
If you are using Rails middleware, you can set the formatter in an initializer:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
config.middleware.use CxLog::Middleware, formatter: CxLog::Formatters::KeyValueFormatter.new
|
64
|
+
```
|
65
|
+
|
66
|
+
### Filter Parameters
|
67
|
+
CxLog supports filtering parameters by default. But, you can overwrite the list of filter parameters by
|
68
|
+
setting the `filter_parameters` option:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
CxLog.options = {
|
72
|
+
filter_parameters: ['password', 'secret']
|
73
|
+
}
|
74
|
+
```
|
75
|
+
|
76
|
+
If you are using Rails middleware, you can set the filter parameters in an initializer:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
config.middleware.use CxLog::Middleware, filter_parameters: ['password', 'secret']
|
80
|
+
```
|
81
|
+
|
82
|
+
The log will replace the value of sensitive parameters with `[FILTERED]`:
|
83
|
+
```
|
84
|
+
{"message":"","request_id":"b41e3733-3ca6-46e9-a968-73e995443ec2",
|
85
|
+
"controller":"posts","action":"index","method":"GET","post_count":1,
|
86
|
+
"password":"[FILTERED]"}
|
87
|
+
```
|
88
|
+
|
22
89
|
|
23
90
|
## Development
|
24
91
|
|
data/lib/cx_log/log.rb
CHANGED
@@ -34,7 +34,8 @@ module CxLog
|
|
34
34
|
|
35
35
|
def default_options
|
36
36
|
{
|
37
|
-
formatter: CxLog::Formatters::Json.new
|
37
|
+
formatter: CxLog::Formatters::Json.new,
|
38
|
+
filter_parameters: %i[passw secret token key _key salt cert]
|
38
39
|
}
|
39
40
|
end
|
40
41
|
|
@@ -65,8 +66,23 @@ module CxLog
|
|
65
66
|
|
66
67
|
def flush(logger)
|
67
68
|
log_level = @context.key?(:error) ? :error : :info
|
68
|
-
logger.public_send(log_level, @options[:formatter].call(
|
69
|
+
logger.public_send(log_level, @options[:formatter].call(sanitized_context))
|
69
70
|
clear
|
70
71
|
end
|
72
|
+
|
73
|
+
def sanitized_context
|
74
|
+
@context.to_a.map do |key, value|
|
75
|
+
if sensitive_key?(key)
|
76
|
+
[key, "[FILTERED]"]
|
77
|
+
else
|
78
|
+
[key, value]
|
79
|
+
end
|
80
|
+
end.to_h
|
81
|
+
end
|
82
|
+
|
83
|
+
def sensitive_key?(key)
|
84
|
+
regex = Regexp.union(@options[:filter_parameters].map(&:to_s))
|
85
|
+
key.to_s.match?(regex)
|
86
|
+
end
|
71
87
|
end
|
72
88
|
end
|
data/lib/cx_log/version.rb
CHANGED