-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudrequestlog): migrate HTTP requests to slog
Part of ongoing migration from zap to slog.
- Loading branch information
Showing
4 changed files
with
177 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package cloudrequestlog | ||
|
||
import ( | ||
"log/slog" | ||
"math" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func slogToLevel(l slog.Level) zapcore.Level { | ||
switch { | ||
case l >= slog.LevelError: | ||
return zapcore.ErrorLevel | ||
case l >= slog.LevelWarn: | ||
return zapcore.WarnLevel | ||
case l >= slog.LevelInfo: | ||
return zapcore.InfoLevel | ||
default: | ||
return zapcore.DebugLevel | ||
} | ||
} | ||
|
||
func levelToSlog(l zapcore.Level) slog.Level { | ||
switch l { | ||
case zapcore.DebugLevel: | ||
return slog.LevelDebug | ||
case zapcore.InfoLevel: | ||
return slog.LevelInfo | ||
case zapcore.WarnLevel: | ||
return slog.LevelWarn | ||
case zapcore.ErrorLevel, zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel: | ||
return slog.LevelError | ||
default: | ||
return slog.LevelDebug | ||
} | ||
} | ||
|
||
func attrToField(attr slog.Attr) zapcore.Field { | ||
if attr.Equal(slog.Attr{}) { | ||
// Ignore empty attrs. | ||
return zap.Skip() | ||
} | ||
switch attr.Value.Kind() { | ||
case slog.KindBool: | ||
return zap.Bool(attr.Key, attr.Value.Bool()) | ||
case slog.KindDuration: | ||
return zap.Duration(attr.Key, attr.Value.Duration()) | ||
case slog.KindFloat64: | ||
return zap.Float64(attr.Key, attr.Value.Float64()) | ||
case slog.KindInt64: | ||
return zap.Int64(attr.Key, attr.Value.Int64()) | ||
case slog.KindString: | ||
return zap.String(attr.Key, attr.Value.String()) | ||
case slog.KindTime: | ||
return zap.Time(attr.Key, attr.Value.Time()) | ||
case slog.KindUint64: | ||
return zap.Uint64(attr.Key, attr.Value.Uint64()) | ||
case slog.KindGroup: | ||
if attr.Key == "" { | ||
// Inlines recursively. | ||
return zap.Inline(groupObject(attr.Value.Group())) | ||
} | ||
return zap.Object(attr.Key, groupObject(attr.Value.Group())) | ||
case slog.KindLogValuer: | ||
return attrToField(slog.Attr{ | ||
Key: attr.Key, | ||
// TODO: resolve the value in a lazy way. | ||
// This probably needs a new Zap field type | ||
// that can be resolved lazily. | ||
Value: attr.Value.Resolve(), | ||
}) | ||
default: | ||
return zap.Any(attr.Key, attr.Value.Any()) | ||
} | ||
} | ||
|
||
// groupObject holds all the Attrs saved in a slog.GroupValue. | ||
type groupObject []slog.Attr | ||
|
||
func (gs groupObject) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
for _, attr := range gs { | ||
attrToField(attr).AddTo(enc) | ||
} | ||
return nil | ||
} | ||
|
||
func fieldToAttr(field zapcore.Field) slog.Attr { | ||
switch field.Type { | ||
case zapcore.StringType: | ||
return slog.String(field.Key, field.String) | ||
case zapcore.Int64Type: | ||
return slog.Int64(field.Key, field.Integer) | ||
case zapcore.Int32Type: | ||
return slog.Int(field.Key, int(field.Integer)) | ||
case zapcore.Uint64Type: | ||
return slog.Uint64(field.Key, uint64(field.Integer)) | ||
case zapcore.Float64Type: | ||
return slog.Float64(field.Key, math.Float64frombits(uint64(field.Integer))) | ||
case zapcore.BoolType: | ||
return slog.Bool(field.Key, field.Integer == 1) | ||
case zapcore.TimeType: | ||
if field.Interface != nil { | ||
loc, ok := field.Interface.(*time.Location) | ||
if ok { | ||
return slog.Time(field.Key, time.Unix(0, field.Integer).In(loc)) | ||
} | ||
} | ||
return slog.Time(field.Key, time.Unix(0, field.Integer)) | ||
case zapcore.DurationType: | ||
return slog.Duration(field.Key, time.Duration(field.Integer)) | ||
default: | ||
return slog.Any(field.Key, field.Interface) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
|
||
"go.einride.tech/cloudrunner" | ||
) | ||
|
||
func main() { | ||
if err := cloudrunner.Run(func(ctx context.Context) error { | ||
cloudrunner.Logger(ctx).Info("hello world") | ||
httpServer := cloudrunner.NewHTTPServer(ctx, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
slog.InfoContext(ctx, "hello from handler") | ||
cloudrunner.AddRequestLogFields(r.Context(), "foo", "bar") | ||
w.Header().Set("Content-Type", "text/plain") | ||
_, _ = w.Write([]byte("hello world")) | ||
})) | ||
return cloudrunner.ListenHTTP(ctx, httpServer) | ||
}); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |