photoprism/internal/event/audit.go
Michael Mayer 3c4cc40882 Security: Refactor log levels and events #98
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-10-10 16:34:07 +02:00

63 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package event
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
)
// AuditLog optionally logs security events.
var AuditLog Logger
var AuditPrefix = "audit: "
var AuditMessageSep = " "
// Format formats an audit log event.
func Format(ev []string, args ...interface{}) string {
return fmt.Sprintf(strings.Join(ev, AuditMessageSep), args...)
}
// Audit optionally reports security-relevant events.
func Audit(level logrus.Level, ev []string, args ...interface{}) {
// Skip if empty.
if len(ev) == 0 {
return
}
// Format log message.
message := Format(ev, args...)
// Show log message if AuditLog is specified.
if AuditLog != nil {
AuditLog.Log(level, AuditPrefix+message)
}
// Publish event if log level is info or higher.
if level <= logrus.InfoLevel {
Publish(
"audit."+level.String(),
Data{
"time": TimeStamp(),
"level": level.String(),
"message": message,
},
)
}
}
func AuditErr(ev []string, args ...interface{}) {
Audit(logrus.ErrorLevel, ev, args...)
}
func AuditWarn(ev []string, args ...interface{}) {
Audit(logrus.WarnLevel, ev, args...)
}
func AuditInfo(ev []string, args ...interface{}) {
Audit(logrus.InfoLevel, ev, args...)
}
func AuditDebug(ev []string, args ...interface{}) {
Audit(logrus.DebugLevel, ev, args...)
}