commit 21a38eff5edeb35f3a2297f5624a24cf9797969f
parent 683015d4df80721a2f209979338f383ee3678db9
Author: Carlosokumu <carlosokumu254@gmail.com>
Date: Tue, 31 Dec 2024 11:42:06 +0300
add a session id as part of the logging
Diffstat:
M | logging/vanilla.go | | | 71 | ++++++++++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 42 insertions(+), 29 deletions(-)
diff --git a/logging/vanilla.go b/logging/vanilla.go
@@ -16,41 +16,47 @@ var (
// Vanilla is a basic single-line structured output logger for terminal output.
type Vanilla struct {
- domain string
+ domain string
levelFilter int
}
// NewVanilla creates a new Vanilla logger.
func NewVanilla() Vanilla {
return Vanilla{
- domain: "main",
+ domain: "main",
levelFilter: LogLevel,
}
}
// WithDomain sets the logging domain. It is prepended to the caller file/line information.
-func(v Vanilla) WithDomain(domain string) Vanilla {
+func (v Vanilla) WithDomain(domain string) Vanilla {
v.domain = domain
return v
}
// WithLevel overrides the globally set loglevel for the logger instance.
-func(v Vanilla) WithLevel(level int) Vanilla {
+func (v Vanilla) WithLevel(level int) Vanilla {
v.levelFilter = level
return v
}
// Printf logs to the global writer.
-func(v Vanilla) Printf(level int, msg string, args ...any) {
+func (v Vanilla) Printf(level int, msg string, args ...any) {
v.Writef(LogWriter, level, msg, args...)
}
// compile log line from inputs and send to given writer.
-func(v Vanilla) writef(w io.Writer, file string, line int, level int, msg string, args ...any) {
+func (v Vanilla) writef(ctx context.Context, w io.Writer, file string, line int, level int, msg string, args ...any) {
+ var argsStr string
if level > v.levelFilter {
return
}
- argsStr := argsToString(args)
+ if ctx == nil {
+ argsStr = argsToString(nil, args)
+ } else {
+ argsStr = argsToString(ctx, args)
+ }
+
if len(msg) > 0 {
fmt.Fprintf(w, "[%s] %s:%s:%v %s\t%s\n", AsString(level), v.domain, file, line, msg, argsStr)
} else {
@@ -59,81 +65,81 @@ func(v Vanilla) writef(w io.Writer, file string, line int, level int, msg string
}
// Writef logs to the given writer.
-func(v Vanilla) Writef(w io.Writer, level int, msg string, args ...any) {
+func (v Vanilla) Writef(w io.Writer, level int, msg string, args ...any) {
file, line := getCaller(2)
- v.writef(w, file, line, level, msg, args)
+ v.writef(nil, w, file, line, level, msg, args)
}
// WriteCtxf logs with context to the given writer.
-func(v Vanilla) WriteCtxf(ctx context.Context, w io.Writer, level int, msg string, args ...any) {
+func (v Vanilla) WriteCtxf(ctx context.Context, w io.Writer, level int, msg string, args ...any) {
file, line := getCaller(2)
- v.writef(w, file, line, level, msg, args...)
+ v.writef(ctx, w, file, line, level, msg, args...)
}
// get caller information and pass on to writef
-func(v Vanilla) printf(level int, msg string, args ...any) {
+func (v Vanilla) printf(level int, msg string, args ...any) {
file, line := getCaller(3)
- v.writef(LogWriter, file, line, level, msg, args...)
+ v.writef(nil, LogWriter, file, line, level, msg, args...)
}
// get caller information and pass on to writef
-func(v Vanilla) printCtxf(ctx context.Context, level int, msg string, args ...any) {
+func (v Vanilla) printCtxf(ctx context.Context, level int, msg string, args ...any) {
file, line := getCaller(3)
- v.writef(LogWriter, file, line, level, msg, args...)
+ v.writef(ctx, LogWriter, file, line, level, msg, args...)
}
// PrintCtxf logs with context to the global writer.
-func(v Vanilla) PrintCtxf(ctx context.Context, level int, msg string, args ...any) {
+func (v Vanilla) PrintCtxf(ctx context.Context, level int, msg string, args ...any) {
v.printf(level, msg, args...)
}
// Tracef logs a line with level TRACE to the global writer.
-func(v Vanilla) Tracef(msg string, args ...any) {
+func (v Vanilla) Tracef(msg string, args ...any) {
v.printf(LVL_TRACE, msg, args...)
}
// Debugf logs a line with level DEBUG to the global writer.
-func(v Vanilla) Debugf(msg string, args ...any) {
+func (v Vanilla) Debugf(msg string, args ...any) {
v.printf(LVL_DEBUG, msg, args...)
}
// Infof logs a line with level INFO to the global writer.
-func(v Vanilla) Infof(msg string, args ...any) {
+func (v Vanilla) Infof(msg string, args ...any) {
v.printf(LVL_INFO, msg, args...)
}
// Warnf logs a line with level WARN to the global writer.
-func(v Vanilla) Warnf(msg string, args ...any) {
+func (v Vanilla) Warnf(msg string, args ...any) {
v.printf(LVL_WARN, msg, args...)
}
// Errorf logs a line with level ERROR to the global writer.
-func(v Vanilla) Errorf(msg string, args ...any) {
+func (v Vanilla) Errorf(msg string, args ...any) {
v.printf(LVL_ERROR, msg, args...)
}
// TraceCtxf logs a line with context with level TRACE to the global writer.
-func(v Vanilla) TraceCtxf(ctx context.Context, msg string, args ...any) {
+func (v Vanilla) TraceCtxf(ctx context.Context, msg string, args ...any) {
v.printCtxf(ctx, LVL_TRACE, msg, args...)
}
// DebugCtxf logs a line with context with level DEBUG to the global writer.
-func(v Vanilla) DebugCtxf(ctx context.Context, msg string, args ...any) {
+func (v Vanilla) DebugCtxf(ctx context.Context, msg string, args ...any) {
v.printCtxf(ctx, LVL_DEBUG, msg, args...)
}
// InfoCtxf logs a line with context with level INFO to the global writer.
-func(v Vanilla) InfoCtxf(ctx context.Context, msg string, args ...any) {
+func (v Vanilla) InfoCtxf(ctx context.Context, msg string, args ...any) {
v.printCtxf(ctx, LVL_INFO, msg, args...)
}
// WarnCtxf logs a line with context with level WARN to the global writer.
-func(v Vanilla) WarnCtxf(ctx context.Context, msg string, args ...any) {
+func (v Vanilla) WarnCtxf(ctx context.Context, msg string, args ...any) {
v.printCtxf(ctx, LVL_WARN, msg, args...)
}
// ErrorCtxf logs a line with context with level ERROR to the global writer.
-func(v Vanilla) ErrorCtxf(ctx context.Context, msg string, args ...any) {
+func (v Vanilla) ErrorCtxf(ctx context.Context, msg string, args ...any) {
v.printCtxf(ctx, LVL_ERROR, msg, args...)
}
@@ -141,14 +147,21 @@ func(v Vanilla) ErrorCtxf(ctx context.Context, msg string, args ...any) {
func getCaller(depth int) (string, int) {
var file string
var line int
- _, file, line,_ = runtime.Caller(depth)
+ _, file, line, _ = runtime.Caller(depth)
baseFile := path.Base(file)
return baseFile, line
}
// string representation of the given structured log args.
-func argsToString(args []any) string {
+func argsToString(ctx context.Context, args []any) string {
var s string
+
+ if ctx != nil {
+ sessionId, ok := ctx.Value("SessionId").(string)
+ if ok {
+ args = append(args, "session-id", sessionId)
+ }
+ }
c := len(args)
var i int
for i = 0; i < c; i += 2 {
@@ -156,7 +169,7 @@ func argsToString(args []any) string {
s += ", "
}
- if i + 1 < c {
+ if i+1 < c {
var argByte []byte
var ok bool
argByte, ok = args[i+1].([]byte)