komuw / ong

ong, is a Go http toolkit.
MIT License
16 stars 4 forks source link

logID are duplicated #316

Closed komuw closed 1 year ago

komuw commented 1 year ago
{
    "time": "2023-07-08T15:53:00.104546732Z",
    "level": "ERROR",
    "source": {
        "function": "github.com/komuw/ong/middleware.logger.func1.1",
        "file": "/home/komuw/go/pkg/mod/github.com/komuw/ong@v0.0.62/middleware/log.go",
        "line": 74
    },
    "msg": "http_server",
    "logID": "Xnxs8KAkRxQgBmMM",
    "pid": 173780,
    "env": "production",
    "ver": "9c5b32f3",
    "logID": "g7mxShTgWzBgkC7f",
    "clientIP": "15.22.144.124",
    "clientFingerPrint": "f659afad3573393928e",
    "method": "GET",
    "path": "/",
    "code": 400,
    "status": "Bad Request",
    "durationMS": 0,
    "ongError": "ong/middleware: the HOST http header has an unexpected value",
    "logID": "Xnxs8KAkRxQgBmMM"
}
komuw commented 1 year ago

repro:

go run -race ./example/...

curl -vkL "https://127.0.0.1:65081/health" -H "Host: badHost.com"
{
    "time": "2023-07-09T18:50:30.771492654Z",
    "level": "ERROR",
    "source": {
        "function": "github.com/komuw/ong/middleware.logger.func1.1",
        "file": "/home/ong/middleware/log.go",
        "line": 74
    },
    "msg": "http_server",
    "logID": "gTXenPA8mXCxQPqh",
    "pid": 301538,
    "logID": "KHxm2cKKfPjGqHDt",
    "clientIP": "127.0.0.1",
    "clientFingerPrint": "0d9kmhf55e80c",
    "method": "GET",
    "path": "/health",
    "code": 400,
    "status": "Bad Request",
    "durationMS": 0,
    "ongError": "ong/middleware: the HOST http header has an unexpected value: badHost.com",
    "logID": "gTXenPA8mXCxQPqh"
}
komuw commented 1 year ago

repro:

package main

import (
    "context"
    "os"

    "github.com/komuw/ong/log"
)

func main() {
    l := log.New(os.Stdout, 100)(context.Background())
    l.Error("my error")
}
{
    "time": "2009-11-10T23:00:00Z",
    "level": "ERROR",
    "source": {
        "function": "main.main",
        "file": "/tmp/sandbox4189155437/prog.go",
        "line": 12
    },
    "msg": "my error",
    "logID": "cEKmgxtQCeDw7t3G",
    "logID": "cEKmgxtQCeDw7t3G"
}

https://go.dev/play/p/UO6u-ViDQTs

komuw commented 1 year ago

The following patch seems to fix it;

diff --git a/log/log.go b/log/log.go
index d257025..1636d2a 100644
--- a/log/log.go
+++ b/log/log.go
@@ -149,7 +149,7 @@ func (h handler) Handle(ctx context.Context, r slog.Record) error {
    ctx = context.WithValue(ctx, octx.LogCtxKey, id)

    newAttrs := []slog.Attr{
-       {Key: logIDFieldName, Value: slog.StringValue(id)},
+       // {Key: logIDFieldName, Value: slog.StringValue(id)},
    }
    r.Attrs(func(a slog.Attr) bool {
        if e, ok := a.Value.Any().(error); ok {