@@ -23,48 +23,54 @@ func NewHandler(handler slog.Handler, opts *Options) *Handler {
23
23
}
24
24
25
25
func (h * Handler ) Enabled (ctx context.Context , level slog.Level ) bool {
26
- if log , ok := ctx .Value (logCtxKey {}).(* Log ); ok {
26
+ if log , ok := ctx .Value (logCtxKey {}).(* log ); ok {
27
27
return level >= log .Level
28
28
}
29
29
30
30
return level >= h .opts .Level
31
31
}
32
32
33
33
func (h * Handler ) Handle (ctx context.Context , rec slog.Record ) error {
34
- log , ok := ctx .Value (logCtxKey {}).(* Log )
34
+ log , ok := ctx .Value (logCtxKey {}).(* log )
35
35
if ! ok {
36
36
// Panic to stress test the use of this handler. Later, we can return error.
37
37
panic ("use of httplog.DefaultHandler outside of context set by http.RequestLogger middleware" )
38
38
}
39
39
40
- // r := log.Req
41
-
42
- scheme := "http"
43
- if log .Req .TLS != nil {
44
- scheme = "https"
40
+ if h .opts .LogRequestCURL {
41
+ rec .AddAttrs (slog .String ("curl" , log .curl ()))
45
42
}
46
43
47
44
if h .opts .Concise {
48
- rec .AddAttrs (slog .Any ("requestHeaders" , slog .GroupValue (getHeaderAttrs (log .Req .Header , h .opts .LogRequestHeaders )... )))
45
+ reqAttrs := []slog.Attr {}
46
+ respAttrs := []slog.Attr {}
47
+
48
+ reqAttrs = append (reqAttrs , slog .Any ("headers" , slog .GroupValue (getHeaderAttrs (log .Req .Header , h .opts .LogRequestHeaders )... )))
49
49
if log .LogRequestBody && log .Resp != nil {
50
- rec . AddAttrs ( slog .String ("requestBody " , log .ReqBody .String ()))
50
+ reqAttrs = append ( reqAttrs , slog .String ("body " , log .ReqBody .String ()))
51
51
if ! log .ReqBodyFullyRead {
52
- rec . AddAttrs ( slog .Bool ("requestBodyFullyRead " , false ))
52
+ reqAttrs = append ( reqAttrs , slog .Bool ("bodyFullyRead " , false ))
53
53
}
54
54
}
55
55
56
56
if log .Resp != nil {
57
- rec .Message = fmt .Sprintf ("HTTP %v (%v): %s %s " , log .WW . Status () , log .Resp . Duration , log .Req . Method , log .Req . URL )
58
- rec . AddAttrs ( slog .Any ("responseHeaders " , slog .GroupValue (getHeaderAttrs (log .Resp .Header (), h .opts .LogResponseHeaders )... )))
57
+ rec .Message = fmt .Sprintf ("%s %s => HTTP %v (%v)" , log .Req . Method , log .Req . URL , log .WW . Status () , log .Resp . Duration )
58
+ respAttrs = append ( respAttrs , slog .Any ("headers " , slog .GroupValue (getHeaderAttrs (log .Resp .Header (), h .opts .LogResponseHeaders )... )))
59
59
if log .LogResponseBody {
60
- rec . AddAttrs ( slog .String ("responseBody " , log .RespBody .String ()))
60
+ respAttrs = append ( respAttrs , slog .String ("body " , log .RespBody .String ()))
61
61
}
62
62
} else {
63
- rec .Message = fmt .Sprintf ("%s %s://%s%s" , log .Req .Method , scheme , log .Req .Host , log .Req .URL )
63
+ rec .Message = fmt .Sprintf ("%s %s://%s%s" , log .Req .Method , log . scheme () , log .Req .Host , log .Req .URL )
64
64
}
65
+
66
+ if log .WW .Status () >= 400 {
67
+ rec .AddAttrs (slog .Any ("request" , slog .GroupValue (reqAttrs ... )))
68
+ rec .AddAttrs (slog .Any ("response" , slog .GroupValue (respAttrs ... )))
69
+ }
70
+
65
71
} else {
66
72
reqAttrs := []slog.Attr {
67
- slog .String ("url" , fmt .Sprintf ("%s://%s%s" , scheme , log .Req .Host , log .Req .URL )),
73
+ slog .String ("url" , fmt .Sprintf ("%s://%s%s" , log . scheme () , log .Req .Host , log .Req .URL )),
68
74
slog .String ("method" , log .Req .Method ),
69
75
slog .String ("path" , log .Req .URL .Path ),
70
76
slog .String ("remoteIp" , log .Req .RemoteAddr ),
0 commit comments