Skip to content

Commit

Permalink
applying centralised logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
somolinosm authored and adrianpozueco committed Jul 7, 2021
1 parent ab5b643 commit 8b04925
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 75 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 39 additions & 75 deletions logger/logger.go
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)
}

0 comments on commit 8b04925

Please sign in to comment.