From acd7de0deeb9ddf0fb3ebd6bfcb9fbe46bbaecb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 13 Jan 2023 17:51:55 +0100 Subject: [PATCH] cherry-pick caller logprint fix from psycho-backend --- scnserver/README.md | 15 ++++++++----- scnserver/init.go | 55 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/scnserver/README.md b/scnserver/README.md index 724aa6a..348f6bf 100644 --- a/scnserver/README.md +++ b/scnserver/README.md @@ -6,7 +6,7 @@ #### BEFORE RELEASE -- tests (!) + - finish tests (!) - migration script for existing data @@ -18,16 +18,21 @@ - diff my currently used scnsend script vs the one in the docs here -- (?) use str-ids (also prevents wrong-joins) -> see psycho - -> how does it work with existing data? (do i care, there are only 2 active users... (are there?)) +- (?) use str-ids (hide counts and prevents wrong-joins) -> see psycho + -> ensre that all queries that return multiple are properly ordered + -> how does it work with existing data? + -> do i care, there are only 2 active users... (are there?) - error logging as goroutine, gets all errors via channel, (channel buffered - nonblocking send, second channel that gets a message when sender failed ) (then all errors end up in _second_ sqlite table) due to message channel etc everything is non blocking and cant fail in main - - request logging (log all requests with body response, exitcode, headers, uri, route, userid, ..., tx-retries, etc), (trim body/response if too big?) - + - => implement proper error logging in goext, kinda combines zerolog and wrapped-errors + copy basic code from bringman, but remove all bm specific stuff and make it abstract + Register(ErrType) methods, errtypes then as structs + log.xxx package with same interface as zerolog + - jobs to clear requests-db and logs-db after to only keep X entries... -> logs and request-logging into their own sqlite files (sqlite-files are prepped) diff --git a/scnserver/init.go b/scnserver/init.go index bbfb4bb..feb0a84 100644 --- a/scnserver/init.go +++ b/scnserver/init.go @@ -1,16 +1,32 @@ package server import ( + "fmt" "github.com/gin-gonic/gin" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "os" + "path" + "path/filepath" + "runtime" ) +var callerRoot = "" + +func init() { + _, file, _, ok := runtime.Caller(0) + if !ok { + return + } + + callerRoot = path.Dir(file) +} + func Init(cfg Config) { cw := zerolog.ConsoleWriter{ - Out: os.Stdout, - TimeFormat: "2006-01-02 15:04:05 Z07:00", + Out: os.Stdout, + TimeFormat: "2006-01-02 15:04:05 Z07:00", + FormatCaller: formatCaller, } zerolog.TimeFieldFormat = zerolog.TimeFormatUnix @@ -32,3 +48,38 @@ func Init(cfg Config) { log.Debug().Msg("Initialized") } + +func formatCaller(i any) string { + const ( + colorBlack = iota + 30 + colorRed + colorGreen + colorYellow + colorBlue + colorMagenta + colorCyan + colorWhite + + colorBold = 1 + colorDarkGray = 90 + ) + + var c string + if cc, ok := i.(string); ok { + c = cc + } + if len(c) > 0 { + if rel, err := filepath.Rel(callerRoot, c); err == nil { + c = rel + } + c = colorize(c, colorBold, false) + colorize(" >", colorCyan, false) + } + return c +} + +func colorize(s interface{}, c int, disabled bool) string { + if disabled { + return fmt.Sprintf("%s", s) + } + return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s) +}