generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop using /common/logging from kyma monorepo (#742)
- Loading branch information
Showing
14 changed files
with
686 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package logger | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type Format string | ||
|
||
const ( | ||
JSON Format = "json" | ||
TEXT Format = "text" | ||
) | ||
|
||
var allFormats = []Format{JSON, TEXT} | ||
|
||
func MapFormat(input string) (Format, error) { | ||
var format = Format(input) | ||
switch format { | ||
case JSON, TEXT: | ||
return format, nil | ||
default: | ||
return format, fmt.Errorf("given log format: %s, doesn't match with any of %v", format, allFormats) | ||
} | ||
} | ||
|
||
func (f Format) ToZapEncoder() (zapcore.Encoder, error) { | ||
encoderConfig := zap.NewProductionEncoderConfig() | ||
encoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder | ||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder | ||
encoderConfig.TimeKey = "timestamp" | ||
encoderConfig.MessageKey = "message" | ||
switch f { | ||
case JSON: | ||
return zapcore.NewJSONEncoder(encoderConfig), nil | ||
case TEXT: | ||
return zapcore.NewConsoleEncoder(encoderConfig), nil | ||
default: | ||
return nil, errors.New("unknown encoder") | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
components/serverless/internal/logging/logger/format_test.go
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,54 @@ | ||
package logger_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kyma-project/serverless/components/serverless/internal/logging/logger" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFormatMapping(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
input string | ||
expected logger.Format | ||
expectedErr bool | ||
}{ | ||
{ | ||
name: "text format", | ||
input: "text", | ||
expected: logger.TEXT, | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "json format", | ||
input: "json", | ||
expected: logger.JSON, | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "not existing format", | ||
input: "csv", | ||
expectedErr: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
//WHEN | ||
|
||
output, err := logger.MapFormat(testCase.input) | ||
|
||
//THEN | ||
if !testCase.expectedErr { | ||
assert.Equal(t, testCase.expected, output) | ||
require.NoError(t, err) | ||
} else { | ||
require.Error(t, err) | ||
} | ||
|
||
}) | ||
} | ||
} |
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,49 @@ | ||
package logger | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type Level string | ||
|
||
const ( | ||
DEBUG Level = "debug" | ||
INFO Level = "info" | ||
WARN Level = "warn" | ||
ERROR Level = "error" | ||
FATAL Level = "fatal" | ||
) | ||
|
||
var allLevels = []Level{DEBUG, INFO, WARN, ERROR, FATAL} | ||
|
||
func MapLevel(level string) (Level, error) { | ||
var lvl = Level(level) | ||
|
||
switch lvl { | ||
case DEBUG, INFO, WARN, ERROR, FATAL: | ||
return lvl, nil | ||
default: | ||
return lvl, fmt.Errorf("given log level: %s, doesn't match with any of %v", level, allLevels) | ||
} | ||
} | ||
|
||
func (l Level) ToZapLevel() (zapcore.Level, error) { | ||
switch l { | ||
case DEBUG: | ||
return zap.DebugLevel, nil | ||
case INFO: | ||
return zap.InfoLevel, nil | ||
case WARN: | ||
return zap.WarnLevel, nil | ||
case ERROR: | ||
return zap.ErrorLevel, nil | ||
case FATAL: | ||
return zap.FatalLevel, nil | ||
default: | ||
return zap.DebugLevel, errors.New("unknown level") | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
components/serverless/internal/logging/logger/level_test.go
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,66 @@ | ||
package logger_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kyma-project/serverless/components/serverless/internal/logging/logger" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLevelMapping(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
input string | ||
expected logger.Level | ||
expectedErr bool | ||
}{ | ||
{ | ||
name: "debug level", | ||
input: "debug", | ||
expected: logger.DEBUG, | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "info level", | ||
input: "info", | ||
expected: logger.INFO, | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "warn level", | ||
input: "warn", | ||
expected: logger.WARN, | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "error level", | ||
input: "error", | ||
expected: logger.ERROR, | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "not existing level", | ||
input: "level", | ||
expectedErr: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
//WHEN | ||
|
||
output, err := logger.MapLevel(testCase.input) | ||
|
||
//THEN | ||
if !testCase.expectedErr { | ||
assert.Equal(t, testCase.expected, output) | ||
require.NoError(t, err) | ||
} else { | ||
require.Error(t, err) | ||
} | ||
|
||
}) | ||
} | ||
} |
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,96 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/go-logr/zapr" | ||
"github.com/kyma-project/serverless/components/serverless/internal/logging/tracing" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type Logger struct { | ||
zapLogger *zap.SugaredLogger | ||
} | ||
|
||
/* | ||
This function creates logger structure based on given format, atomicLevel and additional cores | ||
AtomicLevel structure allows to change level dynamically | ||
*/ | ||
func NewWithAtomicLevel(format Format, atomicLevel zap.AtomicLevel, additionalCores ...zapcore.Core) (*Logger, error) { | ||
return new(format, atomicLevel, additionalCores...) | ||
} | ||
|
||
/* | ||
This function creates logger structure based on given format, level and additional cores | ||
*/ | ||
func New(format Format, level Level, additionalCores ...zapcore.Core) (*Logger, error) { | ||
filterLevel, err := level.ToZapLevel() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "while getting zap log level") | ||
} | ||
|
||
levelEnabler := zap.LevelEnablerFunc(func(incomingLevel zapcore.Level) bool { | ||
return incomingLevel >= filterLevel | ||
}) | ||
|
||
return new(format, levelEnabler, additionalCores...) | ||
} | ||
|
||
func new(format Format, levelEnabler zapcore.LevelEnabler, additionalCores ...zapcore.Core) (*Logger, error) { | ||
encoder, err := format.ToZapEncoder() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "while getting encoding configuration for %s format", format) | ||
} | ||
|
||
defaultCore := zapcore.NewCore( | ||
encoder, | ||
zapcore.Lock(os.Stderr), | ||
levelEnabler, | ||
) | ||
cores := append(additionalCores, defaultCore) | ||
return &Logger{zap.New(zapcore.NewTee(cores...), zap.AddCaller()).Sugar()}, nil | ||
} | ||
|
||
func (l *Logger) WithTracing(ctx context.Context) *zap.SugaredLogger { | ||
newLogger := *l | ||
for key, val := range tracing.GetMetadata(ctx) { | ||
newLogger.zapLogger = newLogger.zapLogger.With(key, val) | ||
} | ||
|
||
return newLogger.WithContext() | ||
} | ||
|
||
func (l *Logger) WithContext() *zap.SugaredLogger { | ||
return l.zapLogger.With(zap.Namespace("context")) | ||
} | ||
|
||
/* | ||
By default the Fatal Error log will be in json format, because it's production default. | ||
*/ | ||
func LogFatalError(format string, args ...interface{}) error { | ||
logger, err := New(JSON, ERROR) | ||
if err != nil { | ||
return errors.Wrap(err, "while getting Error Json Logger") | ||
} | ||
logger.zapLogger.Fatalf(format, args...) | ||
return nil | ||
} | ||
|
||
/* | ||
This function initialize klog which is used in k8s/go-client | ||
*/ | ||
func InitKlog(log *Logger, level Level) error { | ||
zaprLogger := zapr.NewLogger(log.WithContext().Desugar()) | ||
lvl, err := level.ToZapLevel() | ||
if err != nil { | ||
return errors.Wrap(err, "while getting zap log level") | ||
} | ||
zaprLogger.V((int)(lvl)) | ||
klog.SetLogger(zaprLogger) | ||
return nil | ||
} |
Oops, something went wrong.