log_cleaner 0.1.0 → 0.1.1
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 +164 -15
- data/lib/log_cleaner/version.rb +1 -1
- data/lib/log_cleaner.rb +35 -26
- 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: 688ad256a9393d84391b99ea64eab656a10031f946d726299fee0a287145205f
|
|
4
|
+
data.tar.gz: f02d91a66cb33636bb68320f778d5da42154581da5af72667c71bd934e2b1269
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 601adcd0f2654e6589a735d6df6c1b03713f937fa4c30ef32d77bea6854319ec4d7aced4cca8d0182e5ae9da957930f3310ea50f3fd3f3ece1605deffd60af01
|
|
7
|
+
data.tar.gz: 90f465671d6bbd940e1743587a32133d4dca332e3d4f04330ff78342a93698e2a874b8172532d6f149d52c8e07c3a5c4ae45aa7af4ba5fcff7fa886951837240
|
data/README.md
CHANGED
|
@@ -50,24 +50,174 @@ end
|
|
|
50
50
|
|
|
51
51
|
✅ This ensures sensitive data is masked automatically in logs.
|
|
52
52
|
|
|
53
|
-
##
|
|
53
|
+
## Controller Logging with LogCleaner
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
LogCleaner provides structured, masked logging for controller requests.
|
|
56
|
+
You can enable it per controller or globally for all controllers.
|
|
57
|
+
|
|
58
|
+
### Option 1: Enable Logging for a Specific Controller (Recommended)
|
|
59
|
+
|
|
60
|
+
Use this when you want logging only for selected controllers.
|
|
61
|
+
|
|
62
|
+
#### Example"
|
|
63
|
+
```
|
|
64
|
+
# app/controllers/users_controller.rb
|
|
65
|
+
class UsersController < ApplicationController
|
|
66
|
+
include LogCleaner::RequestLogger
|
|
67
|
+
|
|
68
|
+
def create
|
|
69
|
+
# controller logic
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Option 2: Enable Logging Globally (All Controllers)
|
|
75
|
+
|
|
76
|
+
Use this when you want every controller action logged automatically.
|
|
77
|
+
|
|
78
|
+
ApplicationController Setup
|
|
79
|
+
```
|
|
56
80
|
# app/controllers/application_controller.rb
|
|
57
81
|
class ApplicationController < ActionController::Base
|
|
58
82
|
include LogCleaner::RequestLogger
|
|
59
83
|
end
|
|
60
84
|
```
|
|
61
85
|
|
|
86
|
+
✅ All controllers inheriting from ApplicationController will now be logged.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
#### Example Log Output:
|
|
90
|
+
```
|
|
91
|
+
**************************************************
|
|
92
|
+
{
|
|
93
|
+
"timestamp": "2026-01-18T12:05:11Z",
|
|
94
|
+
"level": "info",
|
|
95
|
+
"request_id": "req-a12bc9ef",
|
|
96
|
+
"event": "controller_request",
|
|
97
|
+
"controller": "users",
|
|
98
|
+
"action": "create",
|
|
99
|
+
"status": 201,
|
|
100
|
+
"duration_ms": 134.72,
|
|
101
|
+
"params": {
|
|
102
|
+
"email": "[FILTERED]",
|
|
103
|
+
"password": "[FILTERED]"
|
|
104
|
+
},
|
|
105
|
+
"url": "http://localhost:3000/users",
|
|
106
|
+
"method": "POST",
|
|
107
|
+
"ip": "127.0.0.1",
|
|
108
|
+
"user_id": 1
|
|
109
|
+
}
|
|
110
|
+
**************************************************
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### You can attach custom log data inside any controller action.
|
|
114
|
+
|
|
115
|
+
#### Example 1:
|
|
116
|
+
```
|
|
117
|
+
def create
|
|
118
|
+
log_cleaner_info(event: "user_signup_started", step: "validation")
|
|
119
|
+
# controller logic
|
|
120
|
+
end
|
|
121
|
+
```
|
|
122
|
+
#### Log Output:
|
|
123
|
+
```
|
|
124
|
+
**************************************************
|
|
125
|
+
{
|
|
126
|
+
"event": "user_signup_started",
|
|
127
|
+
"step": "validation",
|
|
128
|
+
"request_id": "req-a12bc9ef",
|
|
129
|
+
"timestamp": "2026-01-18T12:06:30Z",
|
|
130
|
+
"level": "info"
|
|
131
|
+
}
|
|
132
|
+
**************************************************
|
|
133
|
+
```
|
|
134
|
+
#### Example 2:
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
LogCleaner.log("Custom Info: User signup started", current_user: current_user.id)
|
|
138
|
+
```
|
|
139
|
+
#### Log output:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
[INFO] Custom Info: User signup started {"current_user": 1}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## ActiveRecord Logging (Model Validation Errors)
|
|
146
|
+
|
|
147
|
+
LogCleaner can automatically log ActiveRecord validation errors in a structured and masked format.
|
|
148
|
+
You can enable this per model or globally for all models, depending on your needs.
|
|
149
|
+
|
|
150
|
+
### Option 1: Enable Logging for a Specific Model (Recommended)
|
|
151
|
+
|
|
152
|
+
Include LogCleaner::ActiveRecordLogger only in the models where you want validation logs.
|
|
153
|
+
```
|
|
154
|
+
# app/models/user.rb
|
|
155
|
+
class User < ApplicationRecord
|
|
156
|
+
include LogCleaner::ActiveRecordLogger
|
|
157
|
+
|
|
158
|
+
validates :email, presence: true
|
|
159
|
+
validates :password, presence: true
|
|
160
|
+
end
|
|
161
|
+
```
|
|
162
|
+
✅ Best choice when you want fine-grained control
|
|
163
|
+
✅ Avoids noisy logs for all models
|
|
164
|
+
|
|
165
|
+
### Option 2: Enable Logging Globally (All Models)
|
|
166
|
+
|
|
167
|
+
If you want validation logging for every ActiveRecord model, include it in ApplicationRecord.
|
|
168
|
+
```
|
|
169
|
+
# app/models/application_record.rb
|
|
170
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
171
|
+
primary_abstract_class
|
|
172
|
+
|
|
173
|
+
include LogCleaner::ActiveRecordLogger
|
|
174
|
+
end
|
|
175
|
+
```
|
|
176
|
+
⚠️ This will log validation failures for all models.
|
|
177
|
+
⚠️ Use carefully in large applications.
|
|
178
|
+
|
|
179
|
+
#### Example Operation
|
|
180
|
+
```user = User.new(email: nil, password: nil)
|
|
181
|
+
user.save
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
Since validations fail, LogCleaner will automatically log the error.
|
|
186
|
+
|
|
187
|
+
#### Example Log Output
|
|
188
|
+
```
|
|
189
|
+
**************************************************
|
|
190
|
+
{
|
|
191
|
+
"timestamp": "2026-01-18T10:12:44Z",
|
|
192
|
+
"level": "error",
|
|
193
|
+
"event": "model_validation_failed",
|
|
194
|
+
"model": "User",
|
|
195
|
+
"attributes": {
|
|
196
|
+
"id": null,
|
|
197
|
+
"email": null,
|
|
198
|
+
"password": "[FILTERED]"
|
|
199
|
+
},
|
|
200
|
+
"errors": {
|
|
201
|
+
"email": ["can't be blank"],
|
|
202
|
+
"password": ["can't be blank"]
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
**************************************************
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
✅ Sensitive fields are masked.
|
|
209
|
+
✅ Errors are structured and searchable.
|
|
210
|
+
✅ Works automatically via callbacks
|
|
211
|
+
|
|
62
212
|
## Restart Rails Server
|
|
63
213
|
|
|
64
|
-
```After making changes, restart your Rails server:
|
|
214
|
+
``` After making changes, restart your Rails server:
|
|
65
215
|
rails server
|
|
66
216
|
```
|
|
67
217
|
|
|
68
218
|
## Verify Logs
|
|
69
219
|
|
|
70
|
-
Check your server logs
|
|
220
|
+
Check your server logs on rails console, or log/development.log or log/production.log to see masked fields.
|
|
71
221
|
|
|
72
222
|
Example output:
|
|
73
223
|
|
|
@@ -98,21 +248,20 @@ Example output:
|
|
|
98
248
|
**************************************************
|
|
99
249
|
```
|
|
100
250
|
|
|
101
|
-
|
|
251
|
+
Note: You can change the key or value according to your own requirements.
|
|
102
252
|
|
|
103
|
-
|
|
253
|
+
## Pro Tip (Best Practice)
|
|
104
254
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
```
|
|
108
|
-
Example log output:
|
|
255
|
+
Only controller logs needed?
|
|
256
|
+
#### → LogCleaner::RequestLogger
|
|
109
257
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
```
|
|
258
|
+
Only model validation logs needed?
|
|
259
|
+
#### → LogCleaner::ActiveRecordLogger
|
|
113
260
|
|
|
114
|
-
|
|
261
|
+
Need full request lifecycle logging?
|
|
262
|
+
#### → LogCleaner::RequestMiddleware
|
|
115
263
|
|
|
264
|
+
This separation keeps your logs clean, intentional, and scalable.
|
|
116
265
|
|
|
117
266
|
## Contributing
|
|
118
267
|
|
|
@@ -144,4 +293,4 @@ This README is **fully complete**:
|
|
|
144
293
|
|
|
145
294
|
If you want, I can also **prepare a `docs/` folder with screenshots and example logs** that match this README so you can attach them for visual documentation.
|
|
146
295
|
|
|
147
|
-
Do you want me to do that next?
|
|
296
|
+
Do you want me to do that next?
|
data/lib/log_cleaner/version.rb
CHANGED
data/lib/log_cleaner.rb
CHANGED
|
@@ -1,37 +1,46 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
require "securerandom"
|
|
5
|
-
require "logger"
|
|
6
|
-
require "time"
|
|
3
|
+
# lib/log_cleaner.rb
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
#
|
|
18
|
-
#
|
|
5
|
+
# = LogCleaner
|
|
6
|
+
#
|
|
7
|
+
# LogCleaner provides structured JSON logging for Rails applications with
|
|
8
|
+
# automatic masking of sensitive fields such as passwords, emails, and tokens.
|
|
9
|
+
#
|
|
10
|
+
# == Installation
|
|
11
|
+
#
|
|
12
|
+
# gem "log_cleaner"
|
|
13
|
+
#
|
|
14
|
+
# bundle install
|
|
15
|
+
#
|
|
16
|
+
# == Configuration
|
|
17
|
+
#
|
|
18
|
+
# LogCleaner.configure do |c|
|
|
19
|
+
# c.mask_fields = [:password, :authenticity_token]
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# == Controller Logging
|
|
19
23
|
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
# - Integrate with ActiveRecord for database query logging.
|
|
24
|
-
# - Configure logging behavior using a central configuration object.
|
|
24
|
+
# class ApplicationController < ActionController::Base
|
|
25
|
+
# include LogCleaner::RequestLogger
|
|
26
|
+
# end
|
|
25
27
|
#
|
|
26
|
-
#
|
|
28
|
+
# == ActiveRecord Logging
|
|
27
29
|
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
# config.clean_sensitive_data = true
|
|
30
|
+
# class User < ApplicationRecord
|
|
31
|
+
# include LogCleaner::ActiveRecordLogger
|
|
31
32
|
# end
|
|
32
33
|
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
34
|
+
# == Middleware
|
|
35
|
+
#
|
|
36
|
+
# config.middleware.use LogCleaner::RequestMiddleware
|
|
37
|
+
#
|
|
38
|
+
# == Documentation
|
|
39
|
+
#
|
|
40
|
+
# * RubyDoc: https://rubydoc.info/gems/log_cleaner
|
|
41
|
+
# * GitHub: https://github.com/shubham-chauhan-dev/log_cleaner
|
|
42
|
+
# * RubyGems: https://rubygems.org/gems/log_cleaner
|
|
43
|
+
#
|
|
35
44
|
module LogCleaner
|
|
36
45
|
class << self
|
|
37
46
|
attr_accessor :config
|