feat: replace log with rs/zerolog for structured logging

Switch from the standard library log package to rs/zerolog with
ConsoleWriter for colorful terminal output in dev mode and JSON
output in production. Users can now provide their own logger via
Options.Logger or set the level via Options.LogLevel.
This commit is contained in:
Ryan Hamamura
2026-01-31 08:18:24 -10:00
parent d8318af9c4
commit a7ace9099f
10 changed files with 75 additions and 52 deletions

View File

@@ -1,15 +1,17 @@
package via
import "github.com/alexedwards/scs/v2"
import (
"github.com/alexedwards/scs/v2"
"github.com/rs/zerolog"
)
type LogLevel int
func ptr(l zerolog.Level) *zerolog.Level { return &l }
const (
undefined LogLevel = iota
LogLevelError
LogLevelWarn
LogLevelInfo
LogLevelDebug
var (
LogLevelDebug = ptr(zerolog.DebugLevel)
LogLevelInfo = ptr(zerolog.InfoLevel)
LogLevelWarn = ptr(zerolog.WarnLevel)
LogLevelError = ptr(zerolog.ErrorLevel)
)
// Plugin is a func that can mutate the given *via.V app runtime. It is useful to integrate popular JS/CSS UI libraries or tools.
@@ -23,9 +25,12 @@ type Options struct {
// The http server address. e.g. ':3000'
ServerAddress string
// Level of the logs to write to stdout.
// Options: Error, Warn, Info, Debug.
LogLvl LogLevel
// LogLevel sets the minimum log level. nil keeps the default (Info).
LogLevel *zerolog.Level
// Logger overrides the default logger entirely. When set, LogLevel and
// DevMode have no effect on logging.
Logger *zerolog.Logger
// The title of the HTML document.
DocumentTitle string