feat: writing logs to the filesystem with rotation support (#1299)
* refactor: move lib/policy/config to lib/config Signed-off-by: Xe Iaso <me@xeiaso.net> * refactor: don't set global loggers anymore Ref #864 You were right @kotx, it is a bad idea to set the global logger instance. Signed-off-by: Xe Iaso <me@xeiaso.net> * feat(config): add log sink support Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: update spelling Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(test): go mod tidy Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: update spelling Signed-off-by: Xe Iaso <me@xeiaso.net> * docs(admin/policies): add logging block documentation Signed-off-by: Xe Iaso <me@xeiaso.net> * docs: update CHANGELOG Signed-off-by: Xe Iaso <me@xeiaso.net> * fix(cmd/anubis): revert this change, it's meant to be its own PR Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: go mod tidy Signed-off-by: Xe Iaso <me@xeiaso.net> * test: add file logging smoke test Assisted-by: GLM 4.6 via Claude Code Signed-off-by: Xe Iaso <me@xeiaso.net> * fix: don't expose the old log file time format string Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
a709a2b2da
commit
f032d5d0ac
118 changed files with 789 additions and 65 deletions
124
lib/config/logging.go
Normal file
124
lib/config/logging.go
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMissingLoggingFileConfig = errors.New("config.Logging: missing value parameters in logging block")
|
||||
ErrInvalidLoggingSink = errors.New("config.Logging: invalid sink")
|
||||
ErrInvalidLoggingFileConfig = errors.New("config.LoggingFileConfig: invalid parameters")
|
||||
ErrOutOfRange = errors.New("config: error out of range")
|
||||
)
|
||||
|
||||
type Logging struct {
|
||||
Sink string `json:"sink"` // Logging sink, either "stdio" or "file"
|
||||
Level *slog.Level `json:"level"` // Log level, if set supercedes the level in flags
|
||||
Parameters *LoggingFileConfig `json:"parameters"` // Logging parameters, to be dynamic in the future
|
||||
}
|
||||
|
||||
const (
|
||||
LogSinkStdio = "stdio"
|
||||
LogSinkFile = "file"
|
||||
)
|
||||
|
||||
func (l *Logging) Valid() error {
|
||||
var errs []error
|
||||
|
||||
switch l.Sink {
|
||||
case LogSinkStdio:
|
||||
// no validation needed
|
||||
case LogSinkFile:
|
||||
if l.Parameters == nil {
|
||||
errs = append(errs, ErrMissingLoggingFileConfig)
|
||||
}
|
||||
|
||||
if err := l.Parameters.Valid(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
default:
|
||||
errs = append(errs, fmt.Errorf("%w: sink %s is unknown to me", ErrInvalidLoggingSink, l.Sink))
|
||||
}
|
||||
|
||||
if len(errs) != 0 {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (Logging) Default() *Logging {
|
||||
return &Logging{
|
||||
Sink: "stdio",
|
||||
}
|
||||
}
|
||||
|
||||
type LoggingFileConfig struct {
|
||||
Filename string `json:"file"`
|
||||
MaxBackups int `json:"maxBackups"`
|
||||
MaxBytes int64 `json:"maxBytes"`
|
||||
MaxAge int `json:"maxAge"`
|
||||
Compress bool `json:"compress"`
|
||||
UseLocalTime bool `json:"useLocalTime"`
|
||||
}
|
||||
|
||||
func (lfc *LoggingFileConfig) Valid() error {
|
||||
if lfc == nil {
|
||||
return fmt.Errorf("logging file config is nil, why are you calling this?")
|
||||
}
|
||||
|
||||
var errs []error
|
||||
|
||||
if lfc.Zero() {
|
||||
errs = append(errs, ErrMissingValue)
|
||||
}
|
||||
|
||||
if lfc.Filename == "" {
|
||||
errs = append(errs, fmt.Errorf("%w: filename", ErrMissingValue))
|
||||
}
|
||||
|
||||
if lfc.MaxBackups < 0 {
|
||||
errs = append(errs, fmt.Errorf("%w: max backup count %d is not greater than or equal to zero", ErrOutOfRange, lfc.MaxBackups))
|
||||
}
|
||||
|
||||
if lfc.MaxAge < 0 {
|
||||
errs = append(errs, fmt.Errorf("%w: max backup count %d is not greater than or equal to zero", ErrOutOfRange, lfc.MaxAge))
|
||||
}
|
||||
|
||||
if len(errs) != 0 {
|
||||
errs = append([]error{ErrInvalidLoggingFileConfig}, errs...)
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lfc LoggingFileConfig) Zero() bool {
|
||||
for _, cond := range []bool{
|
||||
lfc.Filename != "",
|
||||
lfc.MaxBackups != 0,
|
||||
lfc.MaxBytes != 0,
|
||||
lfc.MaxAge != 0,
|
||||
lfc.Compress,
|
||||
lfc.UseLocalTime,
|
||||
} {
|
||||
if cond {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (LoggingFileConfig) Default() *LoggingFileConfig {
|
||||
return &LoggingFileConfig{
|
||||
Filename: "./var/anubis.log",
|
||||
MaxBackups: 3,
|
||||
MaxBytes: 104857600, // 100 Mi
|
||||
MaxAge: 7, // 7 days
|
||||
Compress: true,
|
||||
UseLocalTime: false,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue