-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
59 lines (49 loc) · 1.49 KB
/
base.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
56
57
58
59
package log
import (
"fmt"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Sets the logger level based on the environment variable.
// Completely ignore if no env variable set.
func SetLoggerLevel(envVarName string, atom zap.AtomicLevel) error {
lvl := os.Getenv(envVarName)
// Expects the string representation of log levels
// I.e. debug, warn, info, error, panic, fatal
logLvl, err := zapcore.ParseLevel(lvl)
// Just exit early with error
if err != nil {
return err
}
atom.SetLevel(logLvl)
return nil
}
// Creates a logger instance, the logger file, and sets the logger level based on the
// CREATE_FULLSTACK_LOG_LVL environment.
// The logger logs to both a logger file (DEBUG level) and stdout (Info).
func CreateLogger(logFilePath string) (*zap.Logger, error) {
// Create logger
atom := zap.NewAtomicLevel()
// To keep the example deterministic, disable timestamps in the output.
encoderCfg := zap.NewProductionEncoderConfig()
// Creates log file if does not already exist
f, err := os.Create(logFilePath)
if err != nil {
return nil, fmt.Errorf("CreateLogger: %s", err.Error())
}
// File + JSON logger
core := zapcore.NewTee(
zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), zapcore.AddSync(f), zap.DebugLevel),
zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
atom,
))
logger := zap.New(core)
err = SetLoggerLevel("CFS_LOG_LVL", atom)
if err != nil {
logger.Debug("using default logger level: WARN")
}
return logger, nil
}