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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f683b75551b46a4dd5799d3699c235765c742d7f0e36e0d51fb3b8f06354e1b
4
- data.tar.gz: ccd650c70feeae9b584b5b566c95e5d7b4d1a7e1fb6496d20915cf7f9e1ebbfd
3
+ metadata.gz: 8c5aaea871bd51849f3c444ba34c134515f07df19dc6a30fff8de5a831032c64
4
+ data.tar.gz: 8804fd0c4b167dca2448114d2a9f145ef5bb0150631b2ca4f484cff33cac181a
5
5
  SHA512:
6
- metadata.gz: 32954d87892042d6871f9b66e2b808270aef6a167c0d6fab63f85ec3a0de538047890e99d8cd3493486176e9eae89cac16e750ed3838b39dcd9190f41793789b
7
- data.tar.gz: 5f8e1a81880d4430bf07cb9af7c8acc5ff5caadcfcac63e5c8ffd87524c5c7cdb63ca204357ec318e40e1c7f7d4cb9f83ce3db69159d818f6aad2d584e215e2b
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 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
+ # 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.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
- def action_options(locale: I18n.locale)
50
- I18n.backend.send(:translations)[locale][:audit_log][:action].map { |k, v| [v, k.to_s] }
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
@@ -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.2.1'
4
+ VERSION = '1.0.1'
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.2.1
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: 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
@@ -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