openreplay/backend/pkg/logger/logger.go
Alexander 30a69893bb
New backend logs integrations (#2717)
* feat(integrations): new version of backend integrations

* feat(integrations): added ingress rule

* feat(integrations): fixed a port number

* feat(integrations): enabled ingress in values.yaml

* feat(integrations): added startup log

* feat(integrations): added extra logger for 3 of 4 backend logs integrations.

* feat(integrations): removed a logs loop call

* feat(integrations): fixed a table name

* feat(integrations): disabled extra logger

* feat(integrations): made extra logger as an option

* feat(integrations): changed contentType for logs file

* feat(integrations): bug fix

* feat(integrations): struct/string config support for datadog provider

* feat(integrations): map config support for datadog provider

* feat(integrations): removed unnecessary transformation

* feat(integrations): fixed datadog and sentry response format

* feat(integrations): added correct creds parser for sentry provider

* feat(integrations): removed unnecessary return statement

* feat(integrations): added correct creds parser for elastic search

* feat(integrations): changed elastic to elasticsearch

* feat(integrations): added correct creds parser for dynatrace

* feat(integrations): fixed an issue in query request for elasticsearch provider

* feat(integrations): made extra logger configurable by env var

* feat(integrations): removed debug logs
2024-10-31 15:28:38 +01:00

102 lines
3.1 KiB
Go

package logger
import (
"context"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
type Logger interface {
Debug(ctx context.Context, message string, args ...interface{})
Info(ctx context.Context, message string, args ...interface{})
Warn(ctx context.Context, message string, args ...interface{})
Error(ctx context.Context, message string, args ...interface{})
Fatal(ctx context.Context, message string, args ...interface{})
}
type loggerImpl struct {
l *zap.Logger
useExtra bool
extra ExtraLogger
}
func New() Logger {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
core := zapcore.NewCore(jsonEncoder, zapcore.AddSync(os.Stdout), zap.InfoLevel)
baseLogger := zap.New(core, zap.AddCaller())
logger := baseLogger.WithOptions(zap.AddCallerSkip(1))
customLogger := &loggerImpl{l: logger}
// Use it only for debugging purposes
if doExtra := os.Getenv("ENABLE_EXTRA_LOGS"); doExtra == "true" {
customLogger.extra = NewExtraLogger()
customLogger.useExtra = true
}
return customLogger
}
func (l *loggerImpl) prepare(ctx context.Context, logger *zap.Logger) *zap.Logger {
if sID, ok := ctx.Value("sessionID").(string); ok {
logger = logger.With(zap.String("sessionID", sID))
}
if pID, ok := ctx.Value("projectID").(string); ok {
logger = logger.With(zap.String("projectID", pID))
}
if tVer, ok := ctx.Value("tracker").(string); ok {
logger = logger.With(zap.String("tracker", tVer))
}
if httpMethod, ok := ctx.Value("httpMethod").(string); ok {
logger = logger.With(zap.String("httpMethod", httpMethod))
}
if urlPath, ok := ctx.Value("url").(string); ok {
logger = logger.With(zap.String("url", urlPath))
}
if batch, ok := ctx.Value("batch").(string); ok {
logger = logger.With(zap.String("batch", batch))
}
return logger
}
func (l *loggerImpl) Debug(ctx context.Context, message string, args ...interface{}) {
logStr := fmt.Sprintf(message, args...)
l.prepare(ctx, l.l.With(zap.String("level", "debug"))).Debug(logStr)
if l.useExtra {
l.extra.Log(ctx, logStr)
}
}
func (l *loggerImpl) Info(ctx context.Context, message string, args ...interface{}) {
logStr := fmt.Sprintf(message, args...)
l.prepare(ctx, l.l.With(zap.String("level", "info"))).Info(logStr)
if l.useExtra {
l.extra.Log(ctx, logStr)
}
}
func (l *loggerImpl) Warn(ctx context.Context, message string, args ...interface{}) {
logStr := fmt.Sprintf(message, args...)
l.prepare(ctx, l.l.With(zap.String("level", "warn"))).Warn(logStr)
if l.useExtra {
l.extra.Log(ctx, logStr)
}
}
func (l *loggerImpl) Error(ctx context.Context, message string, args ...interface{}) {
logStr := fmt.Sprintf(message, args...)
l.prepare(ctx, l.l.With(zap.String("level", "error"))).Error(logStr)
if l.useExtra {
l.extra.Log(ctx, logStr)
}
}
func (l *loggerImpl) Fatal(ctx context.Context, message string, args ...interface{}) {
logStr := fmt.Sprintf(message, args...)
l.prepare(ctx, l.l.With(zap.String("level", "fatal"))).Fatal(logStr)
if l.useExtra {
l.extra.Log(ctx, logStr)
}
}