-
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.
applying centralised logging configuration
- Loading branch information
1 parent
ab5b643
commit 8b04925
Showing
2 changed files
with
40 additions
and
75 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 |
---|---|---|
@@ -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) | ||
} |