audit-log 0.2.1 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -3
- data/db/migrate/20190527035005_create_audit_logs.rb +4 -2
- data/lib/audit-log.rb +27 -19
- data/lib/audit-log/engine.rb +8 -0
- data/lib/audit-log/log_subscriber.rb +12 -0
- data/lib/audit-log/version.rb +3 -1
- metadata +3 -3
- data/app/assets/config/exception_track_manifest.js +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c5aaea871bd51849f3c444ba34c134515f07df19dc6a30fff8de5a831032c64
|
4
|
+
data.tar.gz: 8804fd0c4b167dca2448114d2a9f145ef5bb0150631b2ca4f484cff33cac181a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1c445ecbc5ffc18258dbdc191e4e5404d2f80d9dfff4b86bb5d6aae655694509d72b66698ebf9ec9019feeae37d1384f4440b7e2d0251844db76a6b13c70fca
|
7
|
+
data.tar.gz: 9bd87a113dbb65a3f911245f41cd18a0205e47337c94c08f2438f88208fd91e0f2a44ddd1db4f462deaa16816c66cf4c98d4145d2e00f74366cda2cf2966b92d
|
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
# AuditLog
|
2
2
|
|
3
|
-
Trail audit logs (Operation logs) into the database for user behaviors, including a
|
3
|
+
Trail audit logs (Operation logs) into the database for user behaviors, including a Web UI to query logs.
|
4
4
|
|
5
5
|
[![Build Status](https://travis-ci.org/rails-engine/audit-log.svg?branch=master)](https://travis-ci.org/rails-engine/audit-log)
|
6
6
|
|
7
|
+
> We used audit-log in our production environment more than 1 year, until now (2020.5.21), it's inserted about **20 million** log in our system.
|
8
|
+
|
9
|
+
[中文介绍与使用说明](https://ruby-china.org/topics/39890)
|
10
|
+
|
7
11
|
## Demo UI
|
8
12
|
|
9
13
|
Audit log list:
|
@@ -82,8 +86,8 @@ end
|
|
82
86
|
In models or other places:
|
83
87
|
|
84
88
|
```rb
|
85
|
-
AuditLog.audit!(:update_password, @user, payload: { ip: request.
|
86
|
-
AuditLog.audit!(:sign_in, @user, payload: { ip: request.
|
89
|
+
AuditLog.audit!(:update_password, @user, payload: { ip: request.remote_ip })
|
90
|
+
AuditLog.audit!(:sign_in, @user, payload: { ip: request.remote_ip })
|
87
91
|
AuditLog.audit!(:create_address, nil, payload: params)
|
88
92
|
```
|
89
93
|
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class CreateAuditLogs < ActiveRecord::Migration[5.0]
|
2
4
|
def change
|
3
5
|
create_table 'audit_logs', force: :cascade do |t|
|
4
6
|
t.string 'action', null: false
|
5
|
-
t.
|
6
|
-
t.
|
7
|
+
t.bigint 'user_id'
|
8
|
+
t.bigint 'record_id'
|
7
9
|
t.string 'record_type'
|
8
10
|
t.text 'payload'
|
9
11
|
t.text 'request'
|
data/lib/audit-log.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative './audit-log/version'
|
2
4
|
require_relative './audit-log/configuration'
|
3
5
|
require_relative './audit-log/model'
|
6
|
+
require_relative './audit-log/log_subscriber'
|
4
7
|
require_relative './audit-log/engine'
|
5
8
|
require 'kaminari'
|
6
9
|
|
@@ -24,30 +27,35 @@ module AuditLog
|
|
24
27
|
#
|
25
28
|
# AuditLog.audit!(:edit_account, @account, payload: account_params, user: current_user)
|
26
29
|
def audit!(action, record = nil, payload: nil, user: nil, request: nil)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
ActiveSupport::Notifications.instrument('audit.audit_log', action: action) do
|
31
|
+
request_info = {}
|
32
|
+
if request
|
33
|
+
request_info = {
|
34
|
+
request_id: request.request_id,
|
35
|
+
ip: request.remote_ip,
|
36
|
+
url: request.url,
|
37
|
+
user_agent: request.user_agent
|
38
|
+
}
|
39
|
+
end
|
35
40
|
|
36
|
-
|
37
|
-
|
41
|
+
# Set nil if record is a new_record, do this for avoid create record.
|
42
|
+
record = nil if record&.new_record?
|
38
43
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
Rails.logger.silence do
|
45
|
+
AuditLog::Log.create!(
|
46
|
+
action: action,
|
47
|
+
record: record,
|
48
|
+
payload: (payload || {}).to_h.deep_stringify_keys,
|
49
|
+
user: user,
|
50
|
+
request: request_info.deep_stringify_keys
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
46
54
|
end
|
47
55
|
|
48
56
|
# Get I18n action name options for select
|
49
|
-
def action_options
|
50
|
-
I18n.
|
57
|
+
def action_options
|
58
|
+
I18n.t('audit_log.action').map { |k, v| [v, k.to_s] }
|
51
59
|
end
|
52
60
|
end
|
53
61
|
end
|
data/lib/audit-log/engine.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative './controller_helper'
|
2
4
|
|
3
5
|
module AuditLog
|
@@ -7,5 +9,11 @@ module AuditLog
|
|
7
9
|
ActiveSupport.on_load(:action_controller) do
|
8
10
|
prepend AuditLog::ControllerHelper
|
9
11
|
end
|
12
|
+
|
13
|
+
AuditLog::LogSubscriber.attach_to :audit_log
|
14
|
+
|
15
|
+
initializer 'audit-log.assets.precompile', group: :all do |app|
|
16
|
+
app.config.assets.precompile += %w[audit-log/application.css]
|
17
|
+
end
|
10
18
|
end
|
11
19
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AuditLog
|
4
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
5
|
+
# ActiveSupport::Notifications.instrument('audit.audit_log', action: action)
|
6
|
+
def audit(event)
|
7
|
+
prefix = color('AuditLog', CYAN)
|
8
|
+
action = color(event.payload[:action], BLUE)
|
9
|
+
debug " #{prefix} #{action} (#{event.duration.round(1)}ms)"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/audit-log/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: audit-log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kaminari
|
@@ -63,7 +63,6 @@ files:
|
|
63
63
|
- MIT-LICENSE
|
64
64
|
- README.md
|
65
65
|
- Rakefile
|
66
|
-
- app/assets/config/exception_track_manifest.js
|
67
66
|
- app/assets/stylesheets/audit-log/application.css
|
68
67
|
- app/controllers/audit_log/logs_controller.rb
|
69
68
|
- app/models/audit_log/log.rb
|
@@ -78,6 +77,7 @@ files:
|
|
78
77
|
- lib/audit-log/configuration.rb
|
79
78
|
- lib/audit-log/controller_helper.rb
|
80
79
|
- lib/audit-log/engine.rb
|
80
|
+
- lib/audit-log/log_subscriber.rb
|
81
81
|
- lib/audit-log/model.rb
|
82
82
|
- lib/audit-log/version.rb
|
83
83
|
- lib/generators/audit_log/install_generator.rb
|