-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
55 lines (41 loc) · 1.47 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package log
import (
"context"
"log"
"github.com/pkg/errors"
itbasisCoreUtilsEnvReader "github.com/itbasis/go-core-utils/v2/env-reader"
itbasisDockerUtils "github.com/itbasis/go-docker-utils/v2"
"github.com/juju/zaputil/zapctx"
"go.uber.org/zap"
)
func ConfigureDefaultContextLogger(forcePlainText bool, zapConfig zap.Config) *zap.Logger {
if forcePlainText {
zapConfig.Encoding = "console"
}
return ConfigureDefaultContextLoggerCustomConfig(zapConfig)
}
func ConfigureDefaultContextLoggerCustomConfig(config zap.Config) *zap.Logger {
logger, err := config.Build(zap.AddCaller())
if err != nil {
log.Panic(err)
}
zapctx.Default = logger
return zapctx.Default
}
func ConfigureRootLogger(ctx context.Context, serviceName string, zapConfig zap.Config) (*zap.Logger, error) {
ConfigureDefaultContextLogger(false, zapConfig)
config := Config{}
if err := itbasisCoreUtilsEnvReader.ReadEnvConfig(ctx, &config, nil); err != nil {
return nil, errors.Wrap(err, "error reading environment configuration")
}
return ConfigureRootLoggerWithConfig(serviceName, zapConfig, config)
}
func ConfigureRootLoggerWithConfig(serviceName string, zapConfig zap.Config, config Config) (*zap.Logger, error) {
ConfigureDefaultContextLogger(
!(config.LogForcePlainText || itbasisDockerUtils.IsRunningInDockerContainer()),
zapConfig,
)
zapctx.LogLevel.SetLevel(config.LogRootLevel)
zapctx.Default = zapctx.Default.With(zap.String(MdcServiceName, serviceName))
return zapctx.Default, nil
}