Files
quyun-v2/backend/app/services/audit.go

41 lines
874 B
Go

package services
import (
"context"
"quyun/v2/database/models"
"github.com/sirupsen/logrus"
)
// @provider
type audit struct{}
func (s *audit) Log(ctx context.Context, tenantID, operatorID int64, action, targetID, detail string) {
logrus.WithFields(logrus.Fields{
"audit": true,
"tenant": tenantID,
"operator": operatorID,
"action": action,
"target": targetID,
"detail": detail,
}).Info("Audit Log")
entry := &models.AuditLog{
TenantID: tenantID,
OperatorID: operatorID,
Action: action,
TargetID: targetID,
Detail: detail,
}
if err := models.AuditLogQuery.WithContext(ctx).Create(entry); err != nil {
logrus.WithFields(logrus.Fields{
"audit": true,
"tenant": tenantID,
"operator": operatorID,
"action": action,
"target": targetID,
}).WithError(err).Warn("Audit log persist failed")
}
}