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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d17979e59d30220cfa00c0855182bb6bfce4b8ddd7730b1eeec26cc51850154
4
- data.tar.gz: e449a27cfd787627bd9dfce4c6c715392a1dc4fa27b7e98b7ba6d66386dacbcd
3
+ metadata.gz: 688ad256a9393d84391b99ea64eab656a10031f946d726299fee0a287145205f
4
+ data.tar.gz: f02d91a66cb33636bb68320f778d5da42154581da5af72667c71bd934e2b1269
5
5
  SHA512:
6
- metadata.gz: b4cffd66bf203dd0d9f7e93a89cca8e32fae55f3a8cd6a6241eb7d677c60a9c7d7d1114fe84103b72c6ca08eeacecb01fc7e01b0c286f43e37c6b9b13911e43c
7
- data.tar.gz: f8ee1541a231494dd027b935e40b2657655514cc5154ac64a032fb40a49a0df86a40fdb1c545d070070a30434b51f6f716fc674eefb864a4775a4f8a1929a3de
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
- ## Include in ApplicationController
53
+ ## Controller Logging with LogCleaner
54
54
 
55
- ```Include LogCleaner::RequestLogger to clean logs for every request:
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 (log/development.log or log/production.log) to see masked fields.
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
- ## Adding Custom Logs if you want then
251
+ Note: You can change the key or value according to your own requirements.
102
252
 
103
- You can log custom messages while keeping sensitive fields masked, Add this line for any action or any place:
253
+ ## Pro Tip (Best Practice)
104
254
 
105
- ```
106
- LogCleaner.log("Custom Info: User signup started", current_user: current_user.id)
107
- ```
108
- Example log output:
255
+ Only controller logs needed?
256
+ #### LogCleaner::RequestLogger
109
257
 
110
- ```
111
- [INFO] Custom Info: User signup started {"current_user": 1}
112
- ```
258
+ Only model validation logs needed?
259
+ #### LogCleaner::ActiveRecordLogger
113
260
 
114
- Note: You can change the key or value according your own requirments.
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?
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Gem Version
4
4
  module LogCleaner
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
data/lib/log_cleaner.rb CHANGED
@@ -1,37 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "securerandom"
5
- require "logger"
6
- require "time"
3
+ # lib/log_cleaner.rb
7
4
 
8
- require_relative "log_cleaner/version"
9
- require_relative "log_cleaner/config"
10
- require_relative "log_cleaner/request_store"
11
- require_relative "log_cleaner/logger"
12
- require_relative "log_cleaner/request_middleware"
13
- require_relative "log_cleaner/request_logger"
14
- require_relative "log_cleaner/active_record_logger"
15
- require_relative "log_cleaner/engine"
16
-
17
- # LogCleaner is a Ruby library that provides structured logging
18
- # and request-level log management for applications.
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
- # It allows you to:
21
- # - Automatically capture and clean logs for HTTP requests.
22
- # - Track logs per request using RequestStore.
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
- # Example usage:
28
+ # == ActiveRecord Logging
27
29
  #
28
- # LogCleaner.configure do |config|
29
- # config.log_level = :info
30
- # config.clean_sensitive_data = true
30
+ # class User < ApplicationRecord
31
+ # include LogCleaner::ActiveRecordLogger
31
32
  # end
32
33
  #
33
- # The library also provides middleware for Rack/Rails applications
34
- # to capture request-specific logs and a custom logger for structured output.
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shubham Chauhan