audit_logger 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +233 -0
- data/Rakefile +1 -0
- data/audit_logger.gemspec +26 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/.DS_Store +0 -0
- data/lib/audit_logger/.DS_Store +0 -0
- data/lib/audit_logger/audit_message_styles.rb +40 -0
- data/lib/audit_logger/railtie.rb +7 -0
- data/lib/audit_logger/version.rb +4 -0
- data/lib/audit_logger.rb +110 -0
- data/lib/generators/audit_logger/install_generator.rb +15 -0
- data/lib/generators/audit_logger/templates/audit.rb +9 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 334deb3b809257d3acf6fb467126e262e035853c
|
4
|
+
data.tar.gz: 689a1e77c79bf0fd3215033174cad210fef69753
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbf998d080d063c7b7e256cc1895b1e7a58310b710fbf4b48d5b76d46285623379099b7733424afbb4a85f70ef3b84be46965c865b3a78cd0979067059755e0c
|
7
|
+
data.tar.gz: 52191f2949b3ae96fd0817ed10fa3e7b9305d28818fd6b1dbb72e156770143f272c60a46fc97423a730ef03b65ccb27dab5e40f27f21c70f38a901fc0b3728ae
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Vasin Dmitriy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
# AuditLogger
|
2
|
+
|
3
|
+
This gem implements simple and separated Rails Logger for any action that you want.
|
4
|
+
If you want separated logger files for email notification, data import, migration, ets. this gem - is what you need.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'audit_logger'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
After running bundle install, run the generator:
|
19
|
+
|
20
|
+
$ rails generate audit_logger:install
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
The installer creates `config/initializers/audit.rb` file which implement dummy setup of audit logger.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
unless Rails.env.test?
|
27
|
+
log_path_with_env = "#{Rails.root}/log/audit/#{Rails.env}"
|
28
|
+
|
29
|
+
::ERROR_LOG = Audit::AuditLogger.new("#{log_path_with_env}_error.log", timestamp: true, pid: true, severity: true, thread: true)
|
30
|
+
|
31
|
+
# ::AUDIT_NULL = Audit::AuditLogger.new(File::NULL)
|
32
|
+
# ::AUDIT_STDOUT = Audit::AuditLogger.new(STDOUT)
|
33
|
+
# ::PRODUCT_LOG = AuditLogger::Audit.new("#{log_path_with_env}_product.log")
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
By default all files will be generated in `log/audit/` ( which is created by generator too ) folder, if you want to change this behavior just change `#{Rails.root}/log/audit/#{Rails.env}` and reload server.
|
38
|
+
All exception which will be rescued will be inserted into `ERROR_LOG`
|
39
|
+
|
40
|
+
## Setup own logger
|
41
|
+
To create new logger you need instantiate `AuditLogger::Audit`
|
42
|
+
First argument is name of the logger file.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
::PRODUCT_LOG = AuditLogger::Audit.new("#{log_path_with_env}_product.log")
|
46
|
+
```
|
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
|
+
|
49
|
+
Additional arguments in initialization:
|
50
|
+
|
51
|
+
# by default
|
52
|
+
timestamp: true
|
53
|
+
thread: false
|
54
|
+
pid: false
|
55
|
+
severity: false
|
56
|
+
|
57
|
+
This option influence on otput which will be showed in the log file.
|
58
|
+
|
59
|
+
|
60
|
+
## Example of usage:
|
61
|
+
Lets add products logger into `config/initializers/audit.rb` and enable all available parametrs:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
::PRODUCT_LOG = Audit::AuditLogger.new("#{log_path_with_env}_product.log", timestamp: true, pid: true, severity: true, thread: true)
|
65
|
+
```
|
66
|
+
|
67
|
+
and use logger inside the rake task: `lib/tasks/products.rake`
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
namespace :products do
|
71
|
+
desc '...'
|
72
|
+
|
73
|
+
task :do_something => :environment do
|
74
|
+
PRODUCT_LOG.audit 'This is rake task' do
|
75
|
+
# Do something
|
76
|
+
PRODUCT_LOG.info 'Output some information'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
lets run it `rake products:do_something`
|
83
|
+
|
84
|
+
Logger output:
|
85
|
+
|
86
|
+
# log/audit/development_product.log
|
87
|
+
[ 2015-05-25 15:05:07 | INFO | pid: 3443 | thread: 70101873590780 | <start_of>: This is rake task ]
|
88
|
+
[ 2015-05-25 15:05:07 | INFO | pid: 3443 | thread: 70101873590780 | Output some information ]
|
89
|
+
[ 2015-05-25 15:05:07 | INFO | pid: 3443 | thread: 70101873590780 | </end_of>: This is rake task ]
|
90
|
+
|
91
|
+
|
92
|
+
## Error Handling:
|
93
|
+
Method audit can accept second argument: `log_exception_only`
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# ( log_exception_only = false by default. )
|
97
|
+
PRODUCT_LOG.audit 'This is rake task', log_exception_only: true do
|
98
|
+
```
|
99
|
+
|
100
|
+
When you run rake task with that option, you does not see any logging at all.
|
101
|
+
But lets add some exception into our rake task:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class NotOurError < ::StandardError; end
|
105
|
+
namespace :products do
|
106
|
+
desc '...'
|
107
|
+
|
108
|
+
task :do_something => :environment do
|
109
|
+
PRODUCT_LOG.audit 'This is rake task', log_exception_only: true do
|
110
|
+
# Do something
|
111
|
+
begin
|
112
|
+
raise NotOurError, "Error A"
|
113
|
+
rescue => error
|
114
|
+
raise "Error B"
|
115
|
+
end
|
116
|
+
|
117
|
+
PRODUCT_LOG.info 'Output some information'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
relaunch rake task and you will see next log:
|
124
|
+
|
125
|
+
# log/audit/development_product.log
|
126
|
+
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | <start_of>: This is rake task ]
|
127
|
+
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | ERROR OCCURRED. See details in the Error Log. ]
|
128
|
+
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | </end_of>: This is rake task ]
|
129
|
+
|
130
|
+
# log/audit/development_error.log
|
131
|
+
[ 2015-05-25 15:06:45 | INFO | pid: 3710 | thread: 70177429783040 | <start_of>: This is rake task // development_product.log ]
|
132
|
+
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | RuntimeError: Error B. Cause exception: ]
|
133
|
+
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | NotOurError: Error A. Call stack: ]
|
134
|
+
[ 2015-05-25 15:06:45 | ERROR | pid: 3710 | thread: 70177429783040 | -> ../lib/tasks/products.rake:44:in `block (3 levels) in <top (required)>' ]
|
135
|
+
[ 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 // development_product.log ]
|
137
|
+
|
138
|
+
|
139
|
+
## Exception resque:
|
140
|
+
When you launch your rake task which cause exception you always got exception and stop running of the code.
|
141
|
+
|
142
|
+
With next option exception will be intercepted and logged but not raised on the top:
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
PRODUCT_LOG.audit 'This is rake task', do_raise: false do
|
146
|
+
```
|
147
|
+
|
148
|
+
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 fall with exception
|
150
|
+
|
151
|
+
Also you can use `LOGGER#audit_with_resque` method for such purpose instead of `LOGGER#audit`.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
PRODUCT_LOG.audit_with_resque 'This is rake task' do
|
155
|
+
```
|
156
|
+
|
157
|
+
## Nested usage:
|
158
|
+
You can use logger in nested way for more deeper detalisation:
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
PRODUCT_LOG.audit_with_resque "#{@user.id} #{@user.name}" do
|
162
|
+
@user.posts.each do |post|
|
163
|
+
PRODUCT_LOG.audit "#{post.id} #{user.name}' do
|
164
|
+
# do something.
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
## ActiveRecord exceptions:
|
171
|
+
Lets see how the gem works with AR.
|
172
|
+
|
173
|
+
Add some constraints into DB:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
create_table "products", force: :cascade do |t|
|
177
|
+
t.string "title", default: "", null: false
|
178
|
+
```
|
179
|
+
|
180
|
+
And create some additional rake task:
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
namespace :products do
|
184
|
+
desc '...'
|
185
|
+
task :create_product => :environment do
|
186
|
+
PRODUCT_LOG.audit_with_resque 'Product creation' do
|
187
|
+
Product.create!
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
```
|
192
|
+
|
193
|
+
relaunch rake task and you will see next log:
|
194
|
+
|
195
|
+
# log/audit/development_product.log
|
196
|
+
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | <start_of>: Product creation ]
|
197
|
+
[ 2015-05-25 15:17:00 | ERROR | pid: 6013 | thread: 70285626049020 | ERROR OCCURRED. See details in the Error Log. ]
|
198
|
+
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | </end_of>: Product creation ]
|
199
|
+
|
200
|
+
# log/audit/development_error.log
|
201
|
+
[ 2015-05-25 15:17:00 | INFO | pid: 6013 | thread: 70285626049020 | <start_of>: Product creation // development_product.log ]
|
202
|
+
[ 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
|
+
[ 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
|
+
[ 2015-05-25 15:17:00 | ERROR | pid: 6013 | thread: 70285626049020 | -> ../lib/tasks/products.rake:77:in `block (3 levels) in <top (required)>' ]
|
205
|
+
[ 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 // development_product.log ]
|
207
|
+
|
208
|
+
What about walidation errors?
|
209
|
+
Lets add some validation on to `Product` model:
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
class Product < ActiveRecord::Base
|
213
|
+
validates :title, length: { minimum: 50 }
|
214
|
+
end
|
215
|
+
```
|
216
|
+
|
217
|
+
And change `Product.create!` to `Product.create!(title: 'Small title')` and relaunch the rake task.
|
218
|
+
`log/audit/development_product.log` will be the same as previous, but `development_error.log` will have more detailed information about error exception:
|
219
|
+
|
220
|
+
# log/audit/development_error.log
|
221
|
+
[ 2015-05-25 15:19:58 | INFO | pid: 6729 | thread: 70207020597760 | <start_of>: Product creation // development_product.log ]
|
222
|
+
[ 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
|
+
[ 2015-05-25 15:19:58 | ERROR | pid: 6729 | thread: 70207020597760 | -> ../lib/tasks/products.rake:77:in `block (3 levels) in <top (required)>' ]
|
224
|
+
[ 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 // development_product.log ]
|
226
|
+
|
227
|
+
## Contributing
|
228
|
+
|
229
|
+
1. Fork it ( https://github.com/[my-github-username]/audit_logger/fork )
|
230
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
231
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
232
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
233
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'audit_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "audit_logger"
|
8
|
+
spec.version = AuditLogger::VERSION
|
9
|
+
spec.authors = ["Vasin Dmitriy"]
|
10
|
+
spec.email = ["vasindima779@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Audit logger for Rails apps."
|
13
|
+
spec.description = "Logger which creates additional files for a more orderly logging information in Rails apps."
|
14
|
+
spec.homepage = "https://github.com/DmytroVasin/audit_logger"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency 'rails', ">= #{AuditLogger::RAILS_VERSION}"
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.9'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'railties', ">= #{AuditLogger::RAILS_VERSION}"
|
26
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "audit_logger"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
Binary file
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module AuditLogger
|
2
|
+
module AuditMessageStyles
|
3
|
+
private
|
4
|
+
|
5
|
+
def format_message(severity, timestamp, progname, msg)
|
6
|
+
"[#{date_info(timestamp)}#{severity_info(severity)}#{pid_info}#{thread_info}#{msg} ]\n"
|
7
|
+
end
|
8
|
+
|
9
|
+
def message_block(block_name, opening:)
|
10
|
+
intro_message = opening ? " <start_of>: " : " </end_of>: "
|
11
|
+
info(intro_message + block_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def pid_info
|
15
|
+
" pid: #{$$} |" if al_pid
|
16
|
+
end
|
17
|
+
|
18
|
+
def severity_info(severity)
|
19
|
+
" #{severity} |" if al_severity
|
20
|
+
end
|
21
|
+
|
22
|
+
def date_info(timestamp)
|
23
|
+
" #{timestamp.to_formatted_s(:db)} |" if al_timestamp
|
24
|
+
end
|
25
|
+
|
26
|
+
def exception_message(e)
|
27
|
+
" #{e.class}: #{e.to_s.squish}."
|
28
|
+
end
|
29
|
+
|
30
|
+
def call_stack_output_messages(messages)
|
31
|
+
messages.each do |message|
|
32
|
+
ERROR_LOG.error " -> ..#{message}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def thread_info
|
37
|
+
" thread: #{Thread.current.object_id} |" if al_thread
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/audit_logger.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'audit_logger/version'
|
2
|
+
require 'audit_logger/railtie' if defined?(Rails)
|
3
|
+
require 'rails'
|
4
|
+
|
5
|
+
require 'audit_logger/audit_message_styles'
|
6
|
+
|
7
|
+
module AuditLogger
|
8
|
+
class Audit < Logger
|
9
|
+
include AuditLogger::AuditMessageStyles
|
10
|
+
|
11
|
+
attr_reader :log_file_name,
|
12
|
+
:al_timestamp,
|
13
|
+
:al_pid,
|
14
|
+
:al_severity,
|
15
|
+
:al_shift_age,
|
16
|
+
:al_shift_size,
|
17
|
+
:al_thread
|
18
|
+
|
19
|
+
def initialize(file_path=STDOUT, opts = {})
|
20
|
+
@al_timestamp = opts[:timestamp] || true
|
21
|
+
@al_pid = opts[:pid] || false
|
22
|
+
@al_severity = opts[:severity] || false
|
23
|
+
@al_thread = opts[:thread] || false
|
24
|
+
@al_shift_age = opts[:shift_age] || 0
|
25
|
+
@al_shift_size = opts[:shift_size] || 2*1024*1024
|
26
|
+
|
27
|
+
|
28
|
+
log_file = init_log_file(file_path)
|
29
|
+
|
30
|
+
super(log_file, al_shift_age, al_shift_size)
|
31
|
+
end
|
32
|
+
|
33
|
+
def audit_with_resque(block_name, log_exception_only: false, do_raise: false, &block)
|
34
|
+
audit(block_name, log_exception_only: log_exception_only, do_raise: do_raise, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def audit(block_name, log_exception_only: false, do_raise: true, &block)
|
38
|
+
perform_with_audit(block_name, log_exception_only: log_exception_only, do_raise: do_raise, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def perform_with_audit(block_name, log_exception_only:, do_raise:, &block)
|
44
|
+
begin
|
45
|
+
wrap_with_message(block_name, before: !log_exception_only, after: !log_exception_only, &block)
|
46
|
+
rescue Exception => e
|
47
|
+
wrap_with_message(block_name, before: log_exception_only, after: true) do
|
48
|
+
log_exception(block_name, e)
|
49
|
+
end
|
50
|
+
|
51
|
+
raise(e) if do_raise
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def init_log_file(path)
|
56
|
+
if path == File::NULL || path == STDOUT
|
57
|
+
@log_file_name = 'IO'
|
58
|
+
path
|
59
|
+
else
|
60
|
+
File.open(path, 'a').tap {|file|
|
61
|
+
file.sync = true
|
62
|
+
@log_file_name = File.basename(file.path)
|
63
|
+
}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def wrap_with_message(block_name, before:, after:, &block)
|
68
|
+
message_block(block_name, opening: true) if before
|
69
|
+
result = block.call if block_given?
|
70
|
+
message_block(block_name, opening: false) if after
|
71
|
+
|
72
|
+
result
|
73
|
+
end
|
74
|
+
|
75
|
+
def log_exception(block_name, e)
|
76
|
+
error ' ERROR OCCURRED. See details in the Error Log.'
|
77
|
+
|
78
|
+
ERROR_LOG.audit("#{block_name} // #{log_file_name}") do
|
79
|
+
begin
|
80
|
+
write_exception_details(e)
|
81
|
+
rescue Exception => e
|
82
|
+
error " Error during writing to log: #{e}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def filter_call_stack_trace(e)
|
88
|
+
e.backtrace.map { |trace_level|
|
89
|
+
trace_level.sub(rails_root, '') if rails_root.in?(trace_level)
|
90
|
+
}.compact
|
91
|
+
end
|
92
|
+
|
93
|
+
def write_exception_details(e)
|
94
|
+
record_errors = "ActiveRecord errors: #{e.record.errors.messages}" if e.is_a?(ActiveRecord::RecordInvalid) || e.is_a?(ActiveRecord::RecordNotSaved)
|
95
|
+
|
96
|
+
if e.cause
|
97
|
+
ERROR_LOG.error "AR SAVE ERROR: #{record_errors}" if record_errors
|
98
|
+
ERROR_LOG.error "#{exception_message(e)} Cause exception:"
|
99
|
+
write_exception_details(e.cause)
|
100
|
+
else
|
101
|
+
ERROR_LOG.error "#{exception_message(e)} Call stack:"
|
102
|
+
call_stack_output_messages(filter_call_stack_trace(e))
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def rails_root
|
107
|
+
@rails_root ||= Rails.root.to_s
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module AuditLogger
|
4
|
+
class InstallGenerator < ::Rails::Generators::Base
|
5
|
+
source_root File.expand_path("../templates", __FILE__)
|
6
|
+
|
7
|
+
desc "Generates a file with initial setup of logger instans"
|
8
|
+
|
9
|
+
def copy_initializer
|
10
|
+
FileUtils.mkdir_p("#{Rails.root}/log/audit/")
|
11
|
+
|
12
|
+
copy_file "audit.rb", "config/initializers/audit.rb"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
unless Rails.env.test?
|
2
|
+
log_path_with_env = "#{Rails.root}/log/audit/#{Rails.env}"
|
3
|
+
|
4
|
+
::ERROR_LOG = AuditLogger::Audit.new("#{log_path_with_env}_error.log", timestamp: true, pid: true, severity: true, thread: true)
|
5
|
+
|
6
|
+
# ::AUDIT_NULL = AuditLogger::Audit.new(File::NULL)
|
7
|
+
# ::AUDIT_STDOUT = AuditLogger::Audit.new(STDOUT)
|
8
|
+
# ::PRODUCT_LOG = AuditLogger::Audit.new("#{log_path_with_env}_product.log")
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: audit_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vasin Dmitriy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.12
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.12
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: railties
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.1.12
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.1.12
|
69
|
+
description: Logger which creates additional files for a more orderly logging information
|
70
|
+
in Rails apps.
|
71
|
+
email:
|
72
|
+
- vasindima779@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- audit_logger.gemspec
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/.DS_Store
|
88
|
+
- lib/audit_logger.rb
|
89
|
+
- lib/audit_logger/.DS_Store
|
90
|
+
- lib/audit_logger/audit_message_styles.rb
|
91
|
+
- lib/audit_logger/railtie.rb
|
92
|
+
- lib/audit_logger/version.rb
|
93
|
+
- lib/generators/audit_logger/install_generator.rb
|
94
|
+
- lib/generators/audit_logger/templates/audit.rb
|
95
|
+
homepage: https://github.com/DmytroVasin/audit_logger
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.4.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Audit logger for Rails apps.
|
119
|
+
test_files: []
|