API 参考取自源码中的 Go 文档注释,以英文书写。
package audit
import "github.com/nsgo-dev/game/pkg/online/audit"概述
Package audit writes an append-only JSON Lines log of a server's administrative and security-relevant events — kicks, replaced sessions, operator commands and destructive calls — with the actor and the subject of each, so support questions, anti-cheat review and accountability for admin power have a trail that survives restarts. One file per UTC day rolls at midnight; the files are text, greppable and shippable to a log store, and need no database.
索引
常量
类型
type Options struct {
// Dir is the directory the log lives in, one <yyyy-mm-dd>.jsonl file per
// UTC day, created when missing. Required.
Dir string
// Queue is how many records buffer while the writer appends earlier
// ones, DefaultQueue when 0. Log drops and counts a record past it
// rather than blocking the caller.
Queue int
// Retain is how many days of files to keep, counting back from the day
// of the record being written; older files are deleted as the log opens
// a new day's file. 0 keeps every file.
Retain int
}Options are a Writer's settings.
type Record struct {
// V is the schema version; Log fills in SchemaVersion when 0.
V int `json:"v"`
// Time is when the event happened; Log fills in the current time when
// zero. The record lands in the file of its own UTC day, so a replayed
// batch lands in the files of the days it happened.
Time time.Time `json:"time"`
// Actor is the account ID, an admin token subject, or "system" when empty.
Actor string `json:"actor"`
// Action is what happened, such as "peer.kick", "session.replaced" or
// "admin.run".
Action string `json:"action"`
// Subject is what the action acted on: a peer, an account, a board/slot.
Subject string `json:"subject"`
// Data holds the action's details; omitted from the line when empty.
Data map[string]any `json:"data,omitempty"`
}Record is one audited event: who did what to whom, with the action's details in Data.
type Writer struct {
// contains filtered or unexported fields
}Writer appends records to the day's file on one background goroutine, fsyncing in batches. It is safe for concurrent use.
func Open(o Options) (*Writer, error)Open returns a Writer logging to o.Dir, creating the directory when missing.
func (w *Writer) Close() errorClose flushes the queued records, fsyncs and closes the file, and returns the first append failure, if any. It is safe to call twice.
func (w *Writer) Dropped() uint64Dropped reports how many records never reached the file: they arrived while the queue was full, after Close, or their append failed.
func (w *Writer) Log(r Record)Log queues a record, filling in its schema version, time and actor when unset. It never blocks: a record that does not fit in the queue is dropped and counted by Dropped, as is one logged after Close.