From 8b04925fcd4bd54a894a3acf52bb8524c404fdbf Mon Sep 17 00:00:00 2001 From: "manu.somolinos" Date: Mon, 21 Jun 2021 11:54:24 +0200 Subject: [PATCH] applying centralised logging configuration --- go.mod | 1 + logger/logger.go | 114 ++++++++++++++++------------------------------- 2 files changed, 40 insertions(+), 75 deletions(-) diff --git a/go.mod b/go.mod index 9fca090..d3b80eb 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/Ryanair/goetlib go 1.16 require ( + github.com/Ryanair/gofrlib v1.0.5 // indirect github.com/aws/aws-lambda-go v1.24.0 github.com/aws/aws-sdk-go v1.38.63 github.com/stretchr/testify v1.7.0 diff --git a/logger/logger.go b/logger/logger.go index 9e33bf1..9b2b851 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,97 +1,61 @@ package logger import ( - "fmt" + "github.com/Ryanair/gofrlib/log" "go.uber.org/zap" - "go.uber.org/zap/zapcore" ) -var Log *zap.SugaredLogger - -func InitLogger(logLevel string, application string, function string, logGroup string, logStream string, vpc string, - env string) { - zap.NewProductionConfig() - rawLog, _ := buildConfig(logLevel).Build() - rawLog = rawLog. - WithOptions(zap.AddCallerSkip(1)). - With(zap.String("application", application)). - With(zap.String("function_name", function)). - With(zap.String("@log_group", logGroup)). - With(zap.String("@log_stream", logStream)). - With(zap.String("@vpc", vpc)). - With(zap.String("@env", env)) - Log = rawLog.Sugar() -} - -func InitLoggerTest() { - InitLogger("debug", "test", "function", "log_group", "log_stream", "vpc", "env") -} - -func Debug(msg string, vars ...interface{}) { - Log.Debugf(msg, vars...) -} - -func Info(msg string, vars ...interface{}) { - Log.Infof(msg, vars...) +func NewLogConfiguration(logLevel string, application string, project string, projectGroup string) log.Configuration { + logConfiguration := log.NewConfiguration( + logLevel, + application, + project, + projectGroup, + "") + return logConfiguration } -func Warn(msg string, vars ...interface{}) { - Log.Warnf(msg, vars...) +func InitLogger(logConfiguration log.Configuration) { + log.Init(logConfiguration) } -func Error(msg string, vars ...interface{}) { - Log.Errorf(msg, vars...) -} +func InitLoggerTest() { + logConfiguration := log.NewConfiguration( + "debug", + "test", + "test-project", + "test-project-group", + "") -func AddToContext(k string, v string) { - if v != "" { - Log = Log.With(zap.String(k, v)) - } + InitLogger(logConfiguration) } -func AddRefToContext(k string, v *string) { - if v != nil { - Log = Log.With(zap.String(k, *v)) - } +func InitialLambdaConfiguration(functionName string, logGroup string, logStream string, vpc string, env string) { + log.With("function_name", functionName) + log.With("@log_group", logGroup) + log.With("@log_stream", logStream) + log.With("@vpc", vpc) + log.With("@env", env) } -func buildConfig(logLevel string) zap.Config { - return zap.Config{ - Level: readLogLevel(logLevel), - Development: false, - Sampling: &zap.SamplingConfig{ - Initial: 100, - Thereafter: 100, - }, - Encoding: "json", - EncoderConfig: buildEncoderConfig(), - OutputPaths: []string{"stderr"}, - ErrorOutputPaths: []string{"stderr"}, +func SetTraceId(traceId string) { + if traceId != "" { + log.With(zap.String("Body.context.trace.spanId", traceId)) + log.With(zap.String("SpanId", traceId)) } } -func readLogLevel(logLevel string) zap.AtomicLevel { - logAtomicLevel := zap.AtomicLevel{} - if err := logAtomicLevel.UnmarshalText([]byte(logLevel)); err != nil { - fmt.Printf("malformed log level: %+v\n", logLevel) - logAtomicLevel = zap.NewAtomicLevelAt(zap.ErrorLevel) +func SetSpanId(spanId string) { + if spanId != "" { + log.With(zap.String("Body.context.trace.spanId", spanId)) + log.With(zap.String("SpanId", spanId)) } - - return logAtomicLevel } -func buildEncoderConfig() zapcore.EncoderConfig { - return zapcore.EncoderConfig{ - TimeKey: "@timestamp", - LevelKey: "level", - NameKey: "logger", - CallerKey: "logger_name", - MessageKey: "message", - StacktraceKey: "stack_trace", - LineEnding: zapcore.DefaultLineEnding, - EncodeLevel: zapcore.CapitalLevelEncoder, - EncodeTime: zapcore.ISO8601TimeEncoder, - EncodeDuration: zapcore.SecondsDurationEncoder, - EncodeCaller: zapcore.ShortCallerEncoder, - } +func SetRequestInfo(method string, url string, route string, query string, userAgent string) { + log.WithCustomAttr("Body.context.origin.request.method", method) + log.WithCustomAttr("Body.context.origin.request.url", url) + log.WithCustomAttr("Body.context.origin.request.route", route) + log.WithCustomAttr("Body.context.origin.request.query", query) + log.WithCustomAttr("Body.context.origin.request.userAgent", userAgent) }