audit-log 0.3.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53d84b5630a7c9f749caa8d66d6f14de6bf98c8fd6341bb1abb552f3b9355cdc
4
- data.tar.gz: 683b90bb6df772855a53e9d21c5c97f0e7ce5d12e7d933ff5931d6dd5caaeb1b
3
+ metadata.gz: 781905d915bbdb53e6755f91e70cf3c9263f30f7274a48c8d0118d6ff969c508
4
+ data.tar.gz: d8ae15f1d925905290e02b8130574580573b8b60ab189fad0116f2336966440a
5
5
  SHA512:
6
- metadata.gz: a45f0115b7a154598ae5d92ae4ee34268d33757f7201f3fee155e41e084dde12016c7778b69cd1ba25597aacd47c87d1889b9cb977ca5c59e8996c130497298c
7
- data.tar.gz: 6a032c5c485609ad6b85421d52b5ec3fce29bcf3d3218344a0121c26092ec94cb5904aa1af32b1eff093e9b453aa0a71e507a9b62972d1a5319a4d6410043e79
6
+ metadata.gz: 91f4e614279bd61db2d88f854d7f9f4e5764d519fe11e69bdad0e68a790651b5e815dadc5587514d9401297a6a93a29b4f56e48ce1528991947dfe763a158243
7
+ data.tar.gz: '072735568a99674de1ab2e552306f6311d31eacafff9c95fc1c6730fefb4a70c37cc27a98206a02c8b01b4b300fa3eab80ed3b6f8f828988091fff0cc41742a1'
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 web UI to query logs.
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.ip })
86
- AuditLog.audit!(:sign_in, @user, payload: { ip: request.ip })
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
- class CreateAuditLogs < ActiveRecord::Migration[5.0]
1
+ # frozen_string_literal: true
2
+
3
+ class CreateAuditLogs < ActiveRecord::Migration[5.2]
2
4
  def change
3
5
  create_table 'audit_logs', force: :cascade do |t|
4
6
  t.string 'action', null: false
5
- t.integer 'user_id'
6
- t.integer 'record_id'
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'
@@ -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
- request_info = {}
28
- if request
29
- request_info = {
30
- ip: request.ip,
31
- url: request.url,
32
- user_agent: request.user_agent
33
- }
34
- end
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
- # Set nil if record is a new_record, do this for avoid create record.
37
- record = nil if record&.new_record?
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
- AuditLog::Log.create!(
40
- action: action,
41
- record: record,
42
- payload: (payload || {}).to_h.deep_stringify_keys,
43
- user: user,
44
- request: request_info.deep_stringify_keys
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
57
  def action_options
50
- I18n.t("audit_log.action").map { |k, v| [v, k.to_s] }
58
+ I18n.t('audit_log.action').map { |k, v| [v, k.to_s] }
51
59
  end
52
60
  end
53
61
  end
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AuditLog
2
- VERSION = '0.3.0'
4
+ VERSION = '1.1.0'
3
5
  end
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.3.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-15 00:00:00.000000000 Z
11
+ date: 2020-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kaminari
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '4.0'
33
+ version: '5.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '4.0'
40
+ version: '5.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mysql2
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -1,2 +0,0 @@
1
- //= link_directory ../javascripts/audit-log.js
2
- //= link_directory ../stylesheets/audit-log.css