Move to the layout REPO_POLICIES.md gives (closes #30)
check / check (push) Successful in 2m32s
check / check (push) Successful in 2m32s
The Go module moves from backend/ to the root (go.mod, go.sum, .golangci.yml, cmd/, internal/), the page's source to web/, and nginx's two files to configs/. backend/ is gone: its LICENSE and .dockerignore are deleted, and its .gitignore entries, README and scripts are folded into the root ones. script/build builds the server binary after the frontend; script/run and script/clean move up from backend/script/; make test, lint, fmt and fmt-check do what the backend's did. Vite's root is web/ and it still builds into dist/. script/bootstrap no longer installs gcc, which only the host Go tests needed. Model: opus-5-5
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// Package logger provides a configured slog.Logger with TTY
|
||||
// detection for development vs production output.
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"sneak.berlin/go/netwatch/internal/globals"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
// MaxLoggedFieldBytes bounds untrusted text (request fields,
|
||||
// header values, decode error text) before it is logged, so a
|
||||
// caller cannot inflate log volume with an oversized value.
|
||||
const MaxLoggedFieldBytes = 128
|
||||
|
||||
// BoundedForLog truncates an untrusted string to a fixed byte
|
||||
// bound so an attacker-controlled field cannot dominate the log.
|
||||
func BoundedForLog(s string) string {
|
||||
if len(s) > MaxLoggedFieldBytes {
|
||||
return s[:MaxLoggedFieldBytes]
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Params defines the dependencies for Logger.
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
Globals *globals.Globals
|
||||
}
|
||||
|
||||
// Logger wraps slog.Logger with dynamic level control.
|
||||
type Logger struct {
|
||||
log *slog.Logger
|
||||
level *slog.LevelVar
|
||||
params Params
|
||||
}
|
||||
|
||||
// New creates a Logger with TTY-aware output formatting.
|
||||
func New(_ fx.Lifecycle, params Params) (*Logger, error) {
|
||||
l := new(Logger)
|
||||
l.level = new(slog.LevelVar)
|
||||
l.level.Set(slog.LevelInfo)
|
||||
l.params = params
|
||||
|
||||
tty := false
|
||||
|
||||
if fileInfo, _ := os.Stdout.Stat(); fileInfo != nil {
|
||||
if (fileInfo.Mode() & os.ModeCharDevice) != 0 {
|
||||
tty = true
|
||||
}
|
||||
}
|
||||
|
||||
var handler slog.Handler
|
||||
if tty {
|
||||
handler = slog.NewTextHandler(
|
||||
os.Stdout,
|
||||
&slog.HandlerOptions{
|
||||
Level: l.level,
|
||||
AddSource: true,
|
||||
},
|
||||
)
|
||||
} else {
|
||||
handler = slog.NewJSONHandler(
|
||||
os.Stdout,
|
||||
&slog.HandlerOptions{
|
||||
Level: l.level,
|
||||
AddSource: true,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
l.log = slog.New(handler)
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// EnableDebugLogging sets the log level to debug.
|
||||
func (l *Logger) EnableDebugLogging() {
|
||||
l.level.Set(slog.LevelDebug)
|
||||
l.log.Debug("debug logging enabled", "debug", true)
|
||||
}
|
||||
|
||||
// Get returns the underlying slog.Logger.
|
||||
func (l *Logger) Get() *slog.Logger {
|
||||
return l.log
|
||||
}
|
||||
|
||||
// Identify logs the application's build-time metadata.
|
||||
func (l *Logger) Identify() {
|
||||
l.log.Info("starting",
|
||||
"appname", l.params.Globals.Appname,
|
||||
"version", l.params.Globals.Version,
|
||||
"arch", runtime.GOARCH,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user