init
This commit is contained in:
46
audit/logger.go
Normal file
46
audit/logger.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Entry represents a single audit log record.
|
||||
type Entry struct {
|
||||
Timestamp string `json:"timestamp"`
|
||||
RemoteIP string `json:"remote_ip"`
|
||||
Action string `json:"action"`
|
||||
Path string `json:"path"`
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
|
||||
// Logger writes structured audit entries as JSON to stdout.
|
||||
type Logger struct {
|
||||
out *log.Logger
|
||||
}
|
||||
|
||||
// NewLogger creates a new audit logger writing to stdout.
|
||||
func NewLogger() *Logger {
|
||||
return &Logger{
|
||||
out: log.New(os.Stdout, "", 0),
|
||||
}
|
||||
}
|
||||
|
||||
// Log writes a structured audit entry.
|
||||
func (l *Logger) Log(remoteIP, action, path, summary string) {
|
||||
entry := Entry{
|
||||
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
||||
RemoteIP: remoteIP,
|
||||
Action: action,
|
||||
Path: path,
|
||||
Summary: summary,
|
||||
}
|
||||
data, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
l.out.Printf(`{"error":"failed to marshal audit entry: %v"}`, err)
|
||||
return
|
||||
}
|
||||
l.out.Print(string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user