59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
|
/*
|
||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||
|
*/
|
||
|
|
||
|
package config
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"strings"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
"go.uber.org/zap/zapcore"
|
||
|
)
|
||
|
|
||
|
type LibCxt struct {
|
||
|
Logger *zap.Logger
|
||
|
Context context.Context
|
||
|
}
|
||
|
|
||
|
func NewLibCxt(loglevel string) *LibCxt {
|
||
|
return &LibCxt{
|
||
|
Logger: initLogger(loglevel),
|
||
|
Context: context.Background(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func initLogger(ll string) *zap.Logger {
|
||
|
base := zap.NewProductionConfig()
|
||
|
|
||
|
base.Encoding = "console"
|
||
|
base.Level = zap.NewAtomicLevelAt(getLogLevel(ll))
|
||
|
|
||
|
base.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||
|
base.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||
|
base.DisableCaller = true
|
||
|
return zap.Must(base.Build())
|
||
|
}
|
||
|
|
||
|
func getLogLevel(ll string) zapcore.Level {
|
||
|
var level zapcore.Level
|
||
|
|
||
|
switch strings.ToUpper(ll) {
|
||
|
case "ERROR":
|
||
|
level = zap.ErrorLevel
|
||
|
case "WARN":
|
||
|
level = zap.WarnLevel
|
||
|
case "INFO":
|
||
|
level = zap.InfoLevel
|
||
|
case "DEBUG":
|
||
|
level = zap.DebugLevel
|
||
|
default:
|
||
|
level = zap.InfoLevel
|
||
|
}
|
||
|
|
||
|
return level
|
||
|
}
|