audit_logger 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -31
- data/audit_logger-1.0.0.gem +0 -0
- data/lib/audit_logger.rb +15 -7
- data/lib/audit_logger/version.rb +1 -1
- data/lib/generators/audit_logger/templates/audit.rb +2 -4
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 464f54d48725e3da79fbf9d83dda1397eaac1a68
|
4
|
+
data.tar.gz: 42f2675fa6dd5ff6edd94d1cbc0fca5214b2af84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2df1dd6d58866b78b675a28c0257d56b1852ba564bb38bbb9d1efdc5f7977ab0231aa19adb1ebe082d06b0c887198942a38fa3070c0d5cd8bab4ab28f2d96cc5
|
7
|
+
data.tar.gz: efd6d228c92f85f76e94d8e8678da6056541a0d1ee6914e7cda43d7b7028dadeecc709d4669e194d74d38eadc3f563895122818ff71e236bfaa7c8b2fed88d3e
|
data/README.md
CHANGED
@@ -24,35 +24,41 @@ After running bundle install, run the generator:
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
unless Rails.env.test?
|
27
|
-
|
27
|
+
::ERROR_LOG = AuditLogger::Audit.new('error', timestamp: true, pid: true, severity: true, thread: true)
|
28
28
|
|
29
|
-
::
|
30
|
-
|
31
|
-
# ::
|
32
|
-
# ::AUDIT_STDOUT = Audit::AuditLogger.new(STDOUT)
|
33
|
-
# ::PRODUCT_LOG = AuditLogger::Audit.new("#{log_path_with_env}_product.log")
|
29
|
+
# ::AUDIT_NULL = AuditLogger::Audit.new(File::NULL)
|
30
|
+
# ::AUDIT_STDOUT = AuditLogger::Audit.new(STDOUT)
|
31
|
+
# ::PRODUCT_LOG = AuditLogger::Audit.new('product')
|
34
32
|
end
|
35
33
|
```
|
36
34
|
|
37
|
-
By default all files will be generated in `log/audit/` ( which is created by generator too )
|
38
|
-
|
35
|
+
By default all files will be generated in `Rails.root/log/audit/` folder ( which is created by generator too ).
|
36
|
+
|
37
|
+
If you want to change folder name you should redefine `AuditLogger::Audit#folder_name` method.
|
38
|
+
All exception which will be rescued will be inserted into `ERROR_LOG`.
|
39
39
|
|
40
40
|
## Setup own logger
|
41
41
|
To create new logger you need instantiate `AuditLogger::Audit`
|
42
42
|
First argument is name of the logger file.
|
43
43
|
|
44
44
|
```ruby
|
45
|
-
::PRODUCT_LOG = AuditLogger::Audit.new(
|
45
|
+
::PRODUCT_LOG = AuditLogger::Audit.new('product')
|
46
46
|
```
|
47
47
|
Also if you want, you can insert `File::NULL` or `STDOUT` as first argument for sent output into `/dev/null/` or into console accordingly.
|
48
48
|
|
49
|
-
Additional arguments
|
49
|
+
Additional arguments of initialization:
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
```ruby
|
52
|
+
options_hash = {timestamp: true, thread: false, pid: false, severity: false}
|
53
|
+
::PRODUCT_LOG = AuditLogger::Audit.new('#{file_path}', options_hash)
|
54
|
+
```
|
55
|
+
|
56
|
+
where:
|
57
|
+
|
58
|
+
timestamp: true - by default, options which shows date and time
|
59
|
+
severity: false - by default, options which shows severity ( +WARN+, +ERROR+, +FATAL+, or +UNKNOWN+ )
|
60
|
+
pid: false - by default, options which shows PID of a proces +$$+
|
61
|
+
thread: false - by default, options which shows Thread.current.object_id
|
56
62
|
|
57
63
|
This option influence on otput which will be showed in the log file.
|
58
64
|
|
@@ -61,7 +67,7 @@ end
|
|
61
67
|
Lets add products logger into `config/initializers/audit.rb` and enable all available parametrs:
|
62
68
|
|
63
69
|
```ruby
|
64
|
-
::PRODUCT_LOG = Audit
|
70
|
+
::PRODUCT_LOG = AuditLogger::Audit.new('product', timestamp: true, pid: true, severity: true, thread: true)
|
65
71
|
```
|
66
72
|
|
67
73
|
and use logger inside the rake task: `lib/tasks/products.rake`
|
@@ -83,12 +89,27 @@ end
|
|
83
89
|
|
84
90
|
Logger output:
|
85
91
|
|
86
|
-
# log/audit/
|
92
|
+
# log/audit/development/product.log
|
87
93
|
[ 2015-05-25 15:05:07 | INFO | pid: 3443 | thread: 70101873590780 | <start_of>: This is rake task ]
|
88
94
|
[ 2015-05-25 15:05:07 | INFO | pid: 3443 | thread: 70101873590780 | Output some information ]
|
89
95
|
[ 2015-05-25 15:05:07 | INFO | pid: 3443 | thread: 70101873590780 | </end_of>: This is rake task ]
|
90
96
|
|
91
97
|
|
98
|
+
You can use this logger in migrations, models, ets:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class AddAdditionalFieldsToUser < ActiveRecord::Migration
|
102
|
+
def up
|
103
|
+
DB_MIGRATION_LOG.log_block "#{self.class.name}: create some additional fields" do
|
104
|
+
```
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
def replace_active_cart(current_cart)
|
108
|
+
if self.cart.present?
|
109
|
+
PAYMENT_LOG.info "Cart assigned to Order##{self.cart.order.id}. Current cart##{current_cart.id}"
|
110
|
+
# do something
|
111
|
+
```
|
112
|
+
|
92
113
|
## Error Handling:
|
93
114
|
Method audit can accept second argument: `log_exception_only`
|
94
115
|
|
@@ -122,18 +143,18 @@ end
|
|
122
143
|
|
123
144
|
relaunch rake task and you will see next log:
|
124
145
|
|
125
|
-
# log/audit/
|
146
|
+
# log/audit/development/product.log
|
126
147
|
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | <start_of>: This is rake task ]
|
127
148
|
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | ERROR OCCURRED. See details in the Error Log. ]
|
128
149
|
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | </end_of>: This is rake task ]
|
129
150
|
|
130
|
-
# log/audit/
|
131
|
-
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | <start_of>: This is rake task //
|
151
|
+
# log/audit/development/error.log
|
152
|
+
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | <start_of>: This is rake task // product.log ]
|
132
153
|
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | RuntimeError: Error B. Cause exception: ]
|
133
154
|
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | NotOurError: Error A. Call stack: ]
|
134
155
|
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | -> ../lib/tasks/products.rake:44:in `block (3 levels) in <top (required)>' ]
|
135
156
|
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | -> ../lib/tasks/products.rake:41:in `block (2 levels) in <top (required)>' ]
|
136
|
-
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | </end_of>: This is rake task //
|
157
|
+
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | </end_of>: This is rake task // product.log ]
|
137
158
|
|
138
159
|
|
139
160
|
## Exception resque:
|
@@ -146,7 +167,7 @@ PRODUCT_LOG.audit 'This is rake task', do_raise: false do
|
|
146
167
|
```
|
147
168
|
|
148
169
|
If you set `do_raise` option into `false` state you will have same log as in previous example ( fully logged ),
|
149
|
-
but in terminal output you will see nothin. This option needed when you iterate something and don't want to stop full loop if one case
|
170
|
+
but in terminal output you will see nothin. This option is needed when you iterate something and don't want to stop full loop if one case fail with exception
|
150
171
|
|
151
172
|
Also you can use `LOGGER#audit_with_resque` method for such purpose instead of `LOGGER#audit`.
|
152
173
|
|
@@ -192,18 +213,18 @@ end
|
|
192
213
|
|
193
214
|
relaunch rake task and you will see next log:
|
194
215
|
|
195
|
-
# log/audit/
|
216
|
+
# log/audit/development/product.log
|
196
217
|
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | <start_of>: Product creation ]
|
197
218
|
[ 2015-05-25 15:17:00 | ERROR | pid: 6013 | thread: 70285626049020 | ERROR OCCURRED. See details in the Error Log. ]
|
198
219
|
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | </end_of>: Product creation ]
|
199
220
|
|
200
|
-
# log/audit/
|
201
|
-
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | <start_of>: Product creation //
|
221
|
+
# log/audit/development/error.log
|
222
|
+
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | <start_of>: Product creation // product.log ]
|
202
223
|
[ 2015-05-25 15:17:00 | ERROR | pid: 6013 | thread: 70285626049020 | ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "title" violates not-null constraint DETAIL: Failing row contains (1, null, 2015-05-25 12:17:00.852781, 2015-05-25 12:17:00.852781, null). : INSERT INTO "products" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id". Cause exception: ]
|
203
224
|
[ 2015-05-25 15:17:00 | ERROR | pid: 6013 | thread: 70285626049020 | PG::NotNullViolation: ERROR: null value in column "title" violates not-null constraint DETAIL: Failing row contains (1, null, 2015-05-25 12:17:00.852781, 2015-05-25 12:17:00.852781, null).. Call stack: ]
|
204
225
|
[ 2015-05-25 15:17:00 | ERROR | pid: 6013 | thread: 70285626049020 | -> ../lib/tasks/products.rake:77:in `block (3 levels) in <top (required)>' ]
|
205
226
|
[ 2015-05-25 15:17:00 | ERROR | pid: 6013 | thread: 70285626049020 | -> ../lib/tasks/products.rake:75:in `block (2 levels) in <top (required)>' ]
|
206
|
-
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | </end_of>: Product creation //
|
227
|
+
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | </end_of>: Product creation // product.log ]
|
207
228
|
|
208
229
|
What about walidation errors?
|
209
230
|
Lets add some validation on to `Product` model:
|
@@ -215,18 +236,18 @@ end
|
|
215
236
|
```
|
216
237
|
|
217
238
|
And change `Product.create!` to `Product.create!(title: 'Small title')` and relaunch the rake task.
|
218
|
-
`log/audit/
|
239
|
+
`log/audit/development/product.log` will be the same as previous, but `error.log` will have more detailed information about error exception:
|
219
240
|
|
220
|
-
# log/audit/
|
221
|
-
[ 2015-05-25 15:19:58 | INFO | pid: 6729 | thread: 70207020597760 | <start_of>: Product creation //
|
241
|
+
# log/audit/development/error.log
|
242
|
+
[ 2015-05-25 15:19:58 | INFO | pid: 6729 | thread: 70207020597760 | <start_of>: Product creation // product.log ]
|
222
243
|
[ 2015-05-25 15:19:58 | ERROR | pid: 6729 | thread: 70207020597760 | ActiveRecord::RecordInvalid: Validation failed: Title is too short (minimum is 50 characters). Call stack: ]
|
223
244
|
[ 2015-05-25 15:19:58 | ERROR | pid: 6729 | thread: 70207020597760 | -> ../lib/tasks/products.rake:77:in `block (3 levels) in <top (required)>' ]
|
224
245
|
[ 2015-05-25 15:19:58 | ERROR | pid: 6729 | thread: 70207020597760 | -> ../lib/tasks/products.rake:75:in `block (2 levels) in <top (required)>' ]
|
225
|
-
[ 2015-05-25 15:19:58 | INFO | pid: 6729 | thread: 70207020597760 | </end_of>: Product creation //
|
246
|
+
[ 2015-05-25 15:19:58 | INFO | pid: 6729 | thread: 70207020597760 | </end_of>: Product creation // product.log ]
|
226
247
|
|
227
248
|
## Contributing
|
228
249
|
|
229
|
-
1. Fork it ( https://github.com/
|
250
|
+
1. Fork it ( https://github.com/DmytroVasin/audit_logger/fork )
|
230
251
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
231
252
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
232
253
|
4. Push to the branch (`git push origin my-new-feature`)
|
Binary file
|
data/lib/audit_logger.rb
CHANGED
@@ -16,7 +16,7 @@ module AuditLogger
|
|
16
16
|
:al_shift_size,
|
17
17
|
:al_thread
|
18
18
|
|
19
|
-
def initialize(
|
19
|
+
def initialize(file_name=STDOUT, opts = {})
|
20
20
|
@al_timestamp = opts[:timestamp] || true
|
21
21
|
@al_pid = opts[:pid] || false
|
22
22
|
@al_severity = opts[:severity] || false
|
@@ -25,7 +25,7 @@ module AuditLogger
|
|
25
25
|
@al_shift_size = opts[:shift_size] || 2*1024*1024
|
26
26
|
|
27
27
|
|
28
|
-
log_file = init_log_file(
|
28
|
+
log_file = init_log_file(file_name)
|
29
29
|
|
30
30
|
super(log_file, al_shift_age, al_shift_size)
|
31
31
|
end
|
@@ -52,14 +52,14 @@ module AuditLogger
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
def init_log_file(
|
56
|
-
if
|
55
|
+
def init_log_file(file_name)
|
56
|
+
if file_name == File::NULL || file_name == STDOUT
|
57
57
|
@log_file_name = 'IO'
|
58
|
-
|
58
|
+
file_name
|
59
59
|
else
|
60
|
-
|
60
|
+
@log_file_name = "#{file_name}.log"
|
61
|
+
File.open("#{default_audit_path}/#{log_file_name}", 'a').tap {|file|
|
61
62
|
file.sync = true
|
62
|
-
@log_file_name = File.basename(file.path)
|
63
63
|
}
|
64
64
|
end
|
65
65
|
end
|
@@ -106,5 +106,13 @@ module AuditLogger
|
|
106
106
|
def rails_root
|
107
107
|
@rails_root ||= Rails.root.to_s
|
108
108
|
end
|
109
|
+
|
110
|
+
def default_audit_path
|
111
|
+
"#{rails_root}/log/#{folder_name}/#{Rails.env}"
|
112
|
+
end
|
113
|
+
|
114
|
+
def folder_name
|
115
|
+
'audit'
|
116
|
+
end
|
109
117
|
end
|
110
118
|
end
|
data/lib/audit_logger/version.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
unless Rails.env.test?
|
2
|
-
|
3
|
-
|
4
|
-
::ERROR_LOG = AuditLogger::Audit.new("#{log_path_with_env}_error.log", timestamp: true, pid: true, severity: true, thread: true)
|
2
|
+
::ERROR_LOG = AuditLogger::Audit.new('error', timestamp: true, pid: true, severity: true, thread: true)
|
5
3
|
|
6
4
|
# ::AUDIT_NULL = AuditLogger::Audit.new(File::NULL)
|
7
5
|
# ::AUDIT_STDOUT = AuditLogger::Audit.new(STDOUT)
|
8
|
-
# ::PRODUCT_LOG = AuditLogger::Audit.new(
|
6
|
+
# ::PRODUCT_LOG = AuditLogger::Audit.new('product')
|
9
7
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: audit_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vasin Dmitriy
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- LICENSE.txt
|
82
82
|
- README.md
|
83
83
|
- Rakefile
|
84
|
+
- audit_logger-1.0.0.gem
|
84
85
|
- audit_logger.gemspec
|
85
86
|
- bin/console
|
86
87
|
- bin/setup
|