2023-08-29 08:07:19 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2024-02-08 09:20:01 -05:00
|
|
|
"context"
|
2023-08-29 08:07:19 -04:00
|
|
|
"fmt"
|
2024-02-08 09:20:01 -05:00
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
2023-08-29 08:07:19 -04:00
|
|
|
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
)
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
func replaceGRPCLogger(log *slog.Logger) {
|
2023-08-29 08:07:19 -04:00
|
|
|
gl := &grpcLogger{
|
2024-02-08 09:20:01 -05:00
|
|
|
logger: log.With(slog.String("system", "grpc"), slog.Bool("grpc_log", true)),
|
2023-08-29 08:07:19 -04:00
|
|
|
verbosity: 0,
|
|
|
|
}
|
|
|
|
grpclog.SetLoggerV2(gl)
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
func (l *grpcLogger) log(level slog.Level, args ...interface{}) {
|
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(3, pcs[:])
|
|
|
|
r := slog.NewRecord(time.Now(), level, fmt.Sprint(args...), pcs[0])
|
|
|
|
_ = l.logger.Handler().Handle(context.Background(), r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) logf(level slog.Level, format string, args ...interface{}) {
|
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(3, pcs[:])
|
|
|
|
r := slog.NewRecord(time.Now(), level, fmt.Sprintf(format, args...), pcs[0])
|
|
|
|
_ = l.logger.Handler().Handle(context.Background(), r)
|
|
|
|
}
|
|
|
|
|
2023-08-29 08:07:19 -04:00
|
|
|
type grpcLogger struct {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger *slog.Logger
|
2023-08-29 08:07:19 -04:00
|
|
|
verbosity int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Info(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelInfo, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Infoln(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelInfo, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Infof(format string, args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.logf(slog.LevelInfo, format, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Warning(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelWarn, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Warningln(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelWarn, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Warningf(format string, args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.logf(slog.LevelWarn, format, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Error(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelError, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Errorln(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelError, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Errorf(format string, args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.logf(slog.LevelError, format, args...)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Fatal(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelError, args...)
|
|
|
|
os.Exit(1)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Fatalln(args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.log(slog.LevelError, args...)
|
|
|
|
os.Exit(1)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) Fatalf(format string, args ...interface{}) {
|
2024-02-08 09:20:01 -05:00
|
|
|
l.logf(slog.LevelError, format, args...)
|
|
|
|
os.Exit(1)
|
2023-08-29 08:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *grpcLogger) V(level int) bool {
|
|
|
|
// Check whether the verbosity of the current log ('level') is within the specified threshold ('l.verbosity').
|
|
|
|
// As in https://github.com/grpc/grpc-go/blob/41e044e1c82fcf6a5801d6cbd7ecf952505eecb1/grpclog/loggerv2.go#L199-L201.
|
|
|
|
return level <= l.verbosity
|
|
|
|
}
|