Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rails-engine/audit-log
📑 Create audit logs into the database for user behaviors, including a web UI to query logs.
https://github.com/rails-engine/audit-log
audit engine log op-log operation rails
Last synced: about 23 hours ago
JSON representation
📑 Create audit logs into the database for user behaviors, including a web UI to query logs.
- Host: GitHub
- URL: https://github.com/rails-engine/audit-log
- Owner: rails-engine
- License: mit
- Created: 2019-05-27T02:55:13.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-12-04T18:34:38.000Z (about 1 year ago)
- Last Synced: 2024-12-14T12:05:55.961Z (8 days ago)
- Topics: audit, engine, log, op-log, operation, rails
- Language: Ruby
- Homepage:
- Size: 95.7 KB
- Stars: 168
- Watchers: 3
- Forks: 26
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: MIT-LICENSE
- Audit: audit-log.gemspec
Awesome Lists containing this project
README
# AuditLog
[![build](https://github.com/rails-engine/audit-log/actions/workflows/build.yml/badge.svg)](https://github.com/rails-engine/audit-log/actions/workflows/build.yml)
Trail audit logs (Operation logs) into the database for user behaviors, including a Web UI to query logs.
> 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.
[中文介绍与使用说明](https://ruby-china.org/topics/39890)
## Demo UI
Audit log list:
Detail page:
## Installation
Add this line to your application's Gemfile:
```ruby
gem "audit-log"
```And then execute:
```bash
$ bundle
```Generate files:
```bash
$ rails g audit_log:install
```## Usage
Use in controllers:
```rb
class TicktsController < ApplicationController
def index
audit! :list_ticket, nil
enddef create
if @ticket.save
audit! :create_ticket, @ticket, payload: ticket_params
else
render :new
end
enddef update
if @ticket.save
audit! :update_ticket, @ticket, payload: ticket_params
else
render :edit
end
enddef approve
if @ticket.approve
audit! :approve_ticket, @ticket, payload: ticket_params
end
enddef destroy
# store original attributes for destroy for keep values
audit! :delete_ticket, nil, @ticket.attributes
endprivate
def ticket_params
params.required(:ticket).permit!(:title, :description, :status)
end
end
```In models or other places:
```rb
AuditLog.audit!(:update_password, @user, payload: { ip: request.remote_ip })
AuditLog.audit!(:sign_in, @user, payload: { ip: request.remote_ip })
AuditLog.audit!(:create_address, nil, payload: params)
```Change `config/routes.rb` to add Route:
```rb
Rails.application.routes.draw do
authenticate :user, -> (u) { u.admin? } do
mount AuditLog::Engine => "/audit-log"
end
end
```I18n for audit names, you need create a `config/locales/audit-log.zh-CN.yml`:
```yml
zh-CN:
audit_log:
action:
sign_in: 登录
update_password: 修改密码
create_address: 添加住址
list_ticket: 查看工单列表
create_ticket: 创建工单
update_ticket: 更新工单
delete_ticket: 删除工单
approve_ticket: 审批工单
```For track Warden (Devise) sign in behavirs:
config/initializes/devise.rb
```rb
Warden::Manager.after_authentication do |user, auth, opts|
request = ActionDispatch::Request.new(auth.env)
AuditLog.audit!(:sign_in, user, payload: opts, user: user, request: request)
endWarden::Manager.before_failure do |env, opts|
request = ActionDispatch::Request.new(env)
email = request.params.dig(:user, :email)
user = User.find_by_email(email)
opts[:email] = email
AuditLog.audit!(:sign_in_failure, nil, payload: opts, request: request, user: user)
end
```## Configuration
You can write a `config/initializers/audit_log.rb` to configure the behavior of audit log.
```rb
AuditLog.configure do
# class name of you User model, default: 'User'
self.user_class = "User"
# current_user method name in your Controller, default: 'current_user'
self.current_user_method = "current_user"
# Speical a table_name for AuditLog model, default: "audit_logs"
self.table_name = "audit_logs"
end
```## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).