@@ -30,59 +30,66 @@ func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool {
30
30
return level >= h .opts .Level
31
31
}
32
32
33
- func (h * Handler ) Handle (ctx context.Context , r slog.Record ) error {
33
+ func (h * Handler ) Handle (ctx context.Context , rec slog.Record ) error {
34
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"
45
+ }
46
+
40
47
if h .opts .Concise {
41
- r .AddAttrs (slog .Any ("requestHeaders" , slog .GroupValue (getHeaderAttrs (log .Req .Header , h .opts .ReqHeaders )... )))
42
- if h . opts . ReqBody && log .Resp != nil {
43
- r .AddAttrs (slog .String ("requestBody" , log .Req . Body .String ()))
44
- if ! log .Req . BodyFullyRead {
45
- r .AddAttrs (slog .Bool ("requestBodyFullyRead" , false ))
48
+ rec .AddAttrs (slog .Any ("requestHeaders" , slog .GroupValue (getHeaderAttrs (log .Req .Header , h .opts .LogRequestHeaders )... )))
49
+ if log . LogRequestBody && log .Resp != nil {
50
+ rec .AddAttrs (slog .String ("requestBody" , log .ReqBody .String ()))
51
+ if ! log .ReqBodyFullyRead {
52
+ rec .AddAttrs (slog .Bool ("requestBodyFullyRead" , false ))
46
53
}
47
54
}
48
55
49
56
if log .Resp != nil {
50
- r .Message = fmt .Sprintf ("HTTP %v (%v): %s %s" , log .Resp .Status , log .Resp .Duration , log .Req .Method , log .Req .URL )
51
- r .AddAttrs (slog .Any ("responseHeaders" , slog .GroupValue (getHeaderAttrs (log .Resp .Header (), h .opts .RespHeaders )... )))
52
- if h . opts . RespBody {
53
- r .AddAttrs (slog .String ("responseBody" , log .Resp . Body .String ()))
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 )... )))
59
+ if log . LogResponseBody {
60
+ rec .AddAttrs (slog .String ("responseBody" , log .RespBody .String ()))
54
61
}
55
62
} else {
56
- r .Message = fmt .Sprintf ("%s %s://%s%s" , log .Req .Method , log . Req . Scheme , log .Req .Host , log .Req .URL )
63
+ rec .Message = fmt .Sprintf ("%s %s://%s%s" , log .Req .Method , scheme , log .Req .Host , log .Req .URL )
57
64
}
58
65
} else {
59
66
reqAttrs := []slog.Attr {
60
- slog .String ("url" , fmt .Sprintf ("%s://%s%s" , log . Req . Scheme , log .Req .Host , log .Req .URL )),
67
+ slog .String ("url" , fmt .Sprintf ("%s://%s%s" , scheme , log .Req .Host , log .Req .URL )),
61
68
slog .String ("method" , log .Req .Method ),
62
69
slog .String ("path" , log .Req .URL .Path ),
63
70
slog .String ("remoteIp" , log .Req .RemoteAddr ),
64
71
slog .String ("proto" , log .Req .Proto ),
65
- slog .Any ("headers" , slog .GroupValue (getHeaderAttrs (log .Req .Header , h .opts .ReqHeaders )... )),
72
+ slog .Any ("headers" , slog .GroupValue (getHeaderAttrs (log .Req .Header , h .opts .LogRequestHeaders )... )),
66
73
}
67
- if h . opts . ReqBody && log .Resp != nil {
68
- reqAttrs = append (reqAttrs , slog .String ("body" , log .Req . Body .String ()))
69
- if ! log .Req . BodyFullyRead {
74
+ if log . LogRequestBody && log .Resp != nil {
75
+ reqAttrs = append (reqAttrs , slog .String ("body" , log .ReqBody .String ()))
76
+ if ! log .ReqBodyFullyRead {
70
77
reqAttrs = append (reqAttrs , slog .Bool ("bodyFullyRead" , false ))
71
78
}
72
79
}
73
- r .AddAttrs (slog .Any ("request" , slog .GroupValue (reqAttrs ... )))
80
+ rec .AddAttrs (slog .Any ("request" , slog .GroupValue (reqAttrs ... )))
74
81
75
82
if log .Resp != nil {
76
83
respAttrs := []slog.Attr {
77
- slog .Any ("headers" , slog .GroupValue (getHeaderAttrs (log .Resp .Header (), h .opts .RespHeaders )... )),
78
- slog .Int ("status" , log .Resp .Status ),
79
- slog .Int ("bytes" , log .Resp . Bytes ),
84
+ slog .Any ("headers" , slog .GroupValue (getHeaderAttrs (log .Resp .Header (), h .opts .LogResponseHeaders )... )),
85
+ slog .Int ("status" , log .WW .Status () ),
86
+ slog .Int ("bytes" , log .WW . BytesWritten () ),
80
87
slog .Float64 ("duration" , float64 (log .Resp .Duration .Nanoseconds ()/ 1000000.0 )), // in milliseconds
81
88
}
82
- if h . opts . RespBody {
83
- respAttrs = append (respAttrs , slog .String ("body" , log .Resp . Body .String ()))
89
+ if log . LogResponseBody {
90
+ respAttrs = append (respAttrs , slog .String ("body" , log .RespBody .String ()))
84
91
}
85
- r .AddAttrs (slog .Any ("response" , slog .GroupValue (respAttrs ... )))
92
+ rec .AddAttrs (slog .Any ("response" , slog .GroupValue (respAttrs ... )))
86
93
}
87
94
}
88
95
@@ -99,16 +106,16 @@ func (h *Handler) Handle(ctx context.Context, r slog.Record) error {
99
106
break
100
107
}
101
108
}
102
- r .AddAttrs (
109
+ rec .AddAttrs (
103
110
slog .Any ("panic" , log .Panic ),
104
111
slog .Any ("panicStack" , stackValues ),
105
112
)
106
113
}
107
114
108
- r .AddAttrs (h .attrs ... )
109
- r .AddAttrs (log .Attrs ... )
115
+ rec .AddAttrs (h .attrs ... )
116
+ rec .AddAttrs (log .Attrs ... )
110
117
111
- return h .Handler .Handle (ctx , r )
118
+ return h .Handler .Handle (ctx , rec )
112
119
}
113
120
114
121
func (c * Handler ) WithAttrs (attrs []slog.Attr ) slog.Handler {
0 commit comments