stillbox/pkg/gordio/server/logging.go

208 lines
4.1 KiB
Go
Raw Normal View History

2024-10-17 10:00:23 -04:00
package server
import (
2024-10-18 00:15:21 -04:00
"fmt"
2024-10-17 10:00:23 -04:00
"io"
"io/fs"
2024-10-18 00:15:21 -04:00
"net/http"
2024-10-17 10:00:23 -04:00
"os"
"os/signal"
2024-10-18 00:15:21 -04:00
"runtime/debug"
2024-10-17 10:00:23 -04:00
"syscall"
2024-10-18 00:15:21 -04:00
"time"
2024-10-17 10:00:23 -04:00
"dynatron.me/x/stillbox/pkg/gordio/config"
2024-10-18 00:15:21 -04:00
"github.com/go-chi/chi/v5/middleware"
2024-10-17 10:00:23 -04:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
LOGPERM fs.FileMode = 0600
)
type Logger struct {
console io.Writer
writers []io.Writer
2024-10-17 11:28:43 -04:00
hup chan os.Signal
2024-10-18 00:15:21 -04:00
lastFieldName string
noColor bool
2024-10-17 10:00:23 -04:00
}
func NewLogger(cfg *config.Config) (*Logger, error) {
2024-10-18 00:15:21 -04:00
l := &Logger{}
cw := &zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: "Jan 2 15:04:05",
FormatFieldName: l.fieldNameFormat,
FormatFieldValue: l.fieldValueFormat,
2024-10-17 10:00:23 -04:00
}
2024-10-17 11:28:43 -04:00
2024-10-18 00:15:21 -04:00
l.console = cw
2024-10-17 10:00:23 -04:00
l.hup = make(chan os.Signal, 1)
go func() {
for sig := range l.hup {
log.Logger = log.Output(l.console)
log.Info().Msgf("received %s, closing and reopening logfiles", sig)
l.Close()
err := l.OpenLogs(cfg)
if err != nil {
log.Error().Err(err).Msg("error reopening logs")
continue
}
2024-10-18 00:15:21 -04:00
l.Install()
2024-10-17 10:00:23 -04:00
}
}()
signal.Notify(l.hup, syscall.SIGHUP)
err := l.OpenLogs(cfg)
if err != nil {
return nil, err
}
2024-10-18 00:15:21 -04:00
l.Install()
2024-10-17 10:00:23 -04:00
return l, nil
}
2024-10-18 00:15:21 -04:00
func (l *Logger) Install() {
log.Logger = log.Output(zerolog.MultiLevelWriter(l.writers...))
}
2024-10-17 10:00:23 -04:00
func (l *Logger) Close() {
for _, lg := range l.writers {
if _, isConsole := lg.(*zerolog.ConsoleWriter); isConsole {
continue
}
if cl, isCloser := lg.(io.Closer); isCloser {
err := cl.Close()
if err != nil {
log.Error().Err(err).Msg("closing writer")
}
}
}
l.writers = nil
}
func (l *Logger) OpenLogs(cfg *config.Config) error {
l.writers = make([]io.Writer, 0, len(cfg.Log))
for _, lc := range cfg.Log {
level := zerolog.TraceLevel
if lc.Level != nil {
var err error
level, err = zerolog.ParseLevel(*lc.Level)
if err != nil {
return err
}
}
w := &zerolog.FilteredLevelWriter{
Level: level,
}
switch lc.File {
case nil:
w.Writer = &zerolog.LevelWriterAdapter{Writer: l.console}
default:
2024-10-18 00:15:21 -04:00
f, err := os.OpenFile(*lc.File, os.O_APPEND|os.O_WRONLY|os.O_CREATE, LOGPERM)
2024-10-17 10:00:23 -04:00
if err != nil {
return err
}
w.Writer = &zerolog.LevelWriterAdapter{
Writer: f,
}
}
l.writers = append(l.writers, w)
}
return nil
}
2024-10-18 00:15:21 -04:00
func RequestLogger() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
defer func() {
if r := recover(); r != nil && r != http.ErrAbortHandler {
log.Error().Interface("recover", r).Bytes("stack", debug.Stack()).Msg("incoming_request_panic")
ww.WriteHeader(http.StatusInternalServerError)
}
log.Info().Fields(map[string]interface{}{
"remote_addr": r.RemoteAddr,
"path": r.URL.Path,
"proto": r.Proto,
"method": r.Method,
"user_agent": r.UserAgent(),
"status": http.StatusText(ww.Status()),
"status_code": ww.Status(),
"bytes_in": r.ContentLength,
"bytes_out": ww.BytesWritten(),
"duration": time.Since(t1).String(),
"reqID": middleware.GetReqID(r.Context()),
}).Msg("incoming_request")
}()
next.ServeHTTP(ww, r)
}
return http.HandlerFunc(fn)
}
}
const (
colorBlack = iota + 30
colorRed
colorGreen
colorYellow
colorBlue
colorMagenta
colorCyan
colorWhite
colorNone
colorBold = 1
colorDarkGray = 90
)
func (l *Logger) fieldNameFormat(i interface{}) string {
l.lastFieldName = fmt.Sprint(i)
return l.colorize(l.lastFieldName+"=", colorCyan)
}
func (l *Logger) fieldValueFormat(i interface{}) string {
color := colorNone
switch l.lastFieldName {
case "method":
color = colorMagenta
case "reqID":
color = colorYellow
case "duration":
color = colorBlue
}
l.lastFieldName = ""
if color == colorNone {
return fmt.Sprint(i)
}
return l.colorize(i, color)
}
// colorize returns the string s wrapped in ANSI code c, unless disabled is true or c is 0.
func (l *Logger) colorize(s interface{}, c int) string {
if l.noColor {
return fmt.Sprintf("%v", s)
}
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)
}