2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-23 10:42:33 -04:00
|
|
|
/*
|
|
|
|
Package logger provides logging functionality for Constellation services.
|
|
|
|
It is a thin wrapper around the zap package, providing a consistent interface for logging.
|
|
|
|
Use this package to implement logging for your Constellation services.
|
|
|
|
|
2022-08-05 09:30:23 -04:00
|
|
|
# Usage
|
2022-06-23 10:42:33 -04:00
|
|
|
|
|
|
|
1. Create a logger using New().
|
|
|
|
|
|
|
|
2. Defer the Sync() method to ensure that all log entries are flushed.
|
|
|
|
|
|
|
|
3. Use the Debugf(), Infof(), Warnf(), Errorf(), and Fatalf() methods depending on the level of logging you need.
|
|
|
|
|
|
|
|
4. Use the Named() method to create a named child logger.
|
|
|
|
|
|
|
|
5. Use the With() method to create a child logger with structured context.
|
|
|
|
This can also be used to add context to a single log message:
|
|
|
|
|
|
|
|
logger.With(zap.String("key", "value")).Infof("log message")
|
|
|
|
|
2022-08-05 09:30:23 -04:00
|
|
|
# Log Levels
|
2022-06-23 10:42:33 -04:00
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
Use [Logger.Debugf] to log low level and detailed information that is useful for debugging.
|
2022-06-23 10:42:33 -04:00
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
Use [Logger.Infof] to log general information. This method is correct for most logging purposes.
|
2022-06-23 10:42:33 -04:00
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
Use [Logger.Warnf] to log information that may indicate unwanted behavior, but is not an error.
|
2022-06-23 10:42:33 -04:00
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
Use [Logger.Errorf] to log information about any errors that occurred.
|
2022-06-23 10:42:33 -04:00
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
Use [Logger.Fatalf] to log information about any errors that occurred and then exit the program.
|
2022-06-23 10:42:33 -04:00
|
|
|
*/
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2023-08-29 08:07:19 -04:00
|
|
|
"context"
|
2022-06-28 10:51:30 -04:00
|
|
|
"fmt"
|
2022-06-23 10:42:33 -04:00
|
|
|
"os"
|
2022-06-28 10:51:30 -04:00
|
|
|
"testing"
|
2022-06-23 10:42:33 -04:00
|
|
|
|
2023-08-29 08:07:19 -04:00
|
|
|
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
|
2022-06-23 10:42:33 -04:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2022-06-28 10:51:30 -04:00
|
|
|
"go.uber.org/zap/zaptest"
|
2022-06-23 10:42:33 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogType indicates the output encoding of the log.
|
|
|
|
type LogType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// JSONLog encodes logs in JSON format.
|
|
|
|
JSONLog LogType = iota
|
|
|
|
// PlainLog encodes logs as human readable text.
|
|
|
|
PlainLog
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is a wrapper for zap logger.
|
|
|
|
// The purpose is to provide a simple interface for logging with sensible defaults.
|
|
|
|
type Logger struct {
|
|
|
|
logger *zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Logger.
|
|
|
|
// Set name to an empty string to create an unnamed logger.
|
|
|
|
func New(logType LogType, logLevel zapcore.Level) *Logger {
|
|
|
|
encoderCfg := zap.NewProductionEncoderConfig()
|
|
|
|
encoderCfg.StacktraceKey = zapcore.OmitKey
|
|
|
|
encoderCfg.EncodeLevel = zapcore.CapitalLevelEncoder
|
|
|
|
encoderCfg.EncodeTime = zapcore.RFC3339TimeEncoder
|
|
|
|
|
|
|
|
var encoder zapcore.Encoder
|
|
|
|
if logType == PlainLog {
|
|
|
|
encoder = zapcore.NewConsoleEncoder(encoderCfg)
|
|
|
|
} else {
|
|
|
|
encoder = zapcore.NewJSONEncoder(encoderCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
logCore := zapcore.NewCore(encoder, zapcore.Lock(os.Stderr), zap.NewAtomicLevelAt(logLevel))
|
|
|
|
|
|
|
|
logger := zap.New(
|
|
|
|
logCore,
|
|
|
|
zap.AddCaller(), // add the file and line number of the logging call
|
|
|
|
zap.AddCallerSkip(1), // skip the first caller so that we don't only see this package as the caller
|
|
|
|
)
|
|
|
|
|
|
|
|
return &Logger{logger: logger.Sugar()}
|
|
|
|
}
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
// NewTest creates a logger for unit / integration tests.
|
2022-06-28 10:51:30 -04:00
|
|
|
func NewTest(t *testing.T) *Logger {
|
|
|
|
return &Logger{
|
|
|
|
logger: zaptest.NewLogger(t).Sugar().Named(fmt.Sprintf("%q", t.Name())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 10:42:33 -04:00
|
|
|
// Debugf logs a message at Debug level.
|
|
|
|
// Debug logs are typically voluminous, and contain detailed information on the flow of execution.
|
|
|
|
func (l *Logger) Debugf(format string, args ...any) {
|
|
|
|
l.logger.Debugf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Infof logs a message at Info level.
|
|
|
|
// This is the default logging priority and should be used for all normal messages.
|
|
|
|
func (l *Logger) Infof(format string, args ...any) {
|
|
|
|
l.logger.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf logs a message at Warn level.
|
|
|
|
// Warn logs are more important than Info, but they don't need human review or necessarily indicate an error.
|
|
|
|
func (l *Logger) Warnf(format string, args ...any) {
|
|
|
|
l.logger.Warnf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf logs a message at Error level.
|
|
|
|
// Error logs are high priority and indicate something has gone wrong.
|
|
|
|
func (l *Logger) Errorf(format string, args ...any) {
|
|
|
|
l.logger.Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf logs the message and then calls os.Exit(1).
|
|
|
|
// Use this to exit your program when a fatal error occurs.
|
|
|
|
func (l *Logger) Fatalf(format string, args ...any) {
|
|
|
|
l.logger.Fatalf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync flushes any buffered log entries.
|
|
|
|
// Applications should take care to call Sync before exiting.
|
|
|
|
func (l *Logger) Sync() {
|
|
|
|
_ = l.logger.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithIncreasedLevel returns a logger with increased logging level.
|
|
|
|
func (l *Logger) WithIncreasedLevel(level zapcore.Level) *Logger {
|
2022-06-28 10:51:30 -04:00
|
|
|
return &Logger{logger: l.getZapLogger().WithOptions(zap.IncreaseLevel(level)).Sugar()}
|
2022-06-23 10:42:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// With returns a logger with structured context.
|
|
|
|
func (l *Logger) With(fields ...any) *Logger {
|
|
|
|
return &Logger{logger: l.logger.With(fields...)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Named returns a named logger.
|
|
|
|
func (l *Logger) Named(name string) *Logger {
|
|
|
|
return &Logger{logger: l.logger.Named(name)}
|
|
|
|
}
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
// ReplaceGRPCLogger replaces grpc's internal logger with the given logger.
|
|
|
|
func (l *Logger) ReplaceGRPCLogger() {
|
2023-08-29 08:07:19 -04:00
|
|
|
replaceGRPCLogger(l.getZapLogger())
|
2022-06-23 10:42:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerUnaryInterceptor returns a gRPC server option for intercepting unary gRPC logs.
|
|
|
|
func (l *Logger) GetServerUnaryInterceptor() grpc.ServerOption {
|
2023-08-29 08:07:19 -04:00
|
|
|
return grpc.UnaryInterceptor(
|
|
|
|
logging.UnaryServerInterceptor(l.middlewareLogger()),
|
|
|
|
)
|
2022-06-23 10:42:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerStreamInterceptor returns a gRPC server option for intercepting streaming gRPC logs.
|
|
|
|
func (l *Logger) GetServerStreamInterceptor() grpc.ServerOption {
|
2023-08-29 08:07:19 -04:00
|
|
|
return grpc.StreamInterceptor(
|
|
|
|
logging.StreamServerInterceptor(l.middlewareLogger()),
|
|
|
|
)
|
2022-06-23 10:42:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientUnaryInterceptor returns a gRPC client option for intercepting unary gRPC logs.
|
|
|
|
func (l *Logger) GetClientUnaryInterceptor() grpc.DialOption {
|
2023-08-29 08:07:19 -04:00
|
|
|
return grpc.WithUnaryInterceptor(
|
|
|
|
logging.UnaryClientInterceptor(l.middlewareLogger()),
|
|
|
|
)
|
2022-06-23 10:42:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientStreamInterceptor returns a gRPC client option for intercepting stream gRPC logs.
|
|
|
|
func (l *Logger) GetClientStreamInterceptor() grpc.DialOption {
|
2023-08-29 08:07:19 -04:00
|
|
|
return grpc.WithStreamInterceptor(
|
|
|
|
logging.StreamClientInterceptor(l.middlewareLogger()),
|
|
|
|
)
|
2022-06-23 10:42:33 -04:00
|
|
|
}
|
2022-06-28 10:51:30 -04:00
|
|
|
|
|
|
|
// getZapLogger returns the underlying zap logger.
|
|
|
|
func (l *Logger) getZapLogger() *zap.Logger {
|
|
|
|
return l.logger.Desugar()
|
|
|
|
}
|
2023-08-29 08:07:19 -04:00
|
|
|
|
|
|
|
func (l *Logger) middlewareLogger() logging.Logger {
|
|
|
|
return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
|
|
|
|
f := make([]zap.Field, 0, len(fields)/2)
|
|
|
|
|
|
|
|
for i := 0; i < len(fields); i += 2 {
|
|
|
|
key := fields[i]
|
|
|
|
value := fields[i+1]
|
|
|
|
|
|
|
|
switch v := value.(type) {
|
|
|
|
case string:
|
|
|
|
f = append(f, zap.String(key.(string), v))
|
|
|
|
case int:
|
|
|
|
f = append(f, zap.Int(key.(string), v))
|
|
|
|
case bool:
|
|
|
|
f = append(f, zap.Bool(key.(string), v))
|
|
|
|
default:
|
|
|
|
f = append(f, zap.Any(key.(string), v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := l.getZapLogger().WithOptions(zap.AddCallerSkip(1)).With(f...)
|
|
|
|
|
|
|
|
switch lvl {
|
|
|
|
case logging.LevelDebug:
|
|
|
|
logger.Debug(msg)
|
|
|
|
case logging.LevelInfo:
|
|
|
|
logger.Info(msg)
|
|
|
|
case logging.LevelWarn:
|
|
|
|
logger.Warn(msg)
|
|
|
|
case logging.LevelError:
|
|
|
|
logger.Error(msg)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown level %v", lvl))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|