162 lines
3.3 KiB
Go
162 lines
3.3 KiB
Go
package logger
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
type LogEnvironment int
|
|
|
|
const (
|
|
Development LogEnvironment = iota
|
|
Debug
|
|
Testing
|
|
Production
|
|
)
|
|
|
|
var (
|
|
Logger *zap.Logger
|
|
LogConfigs = [4]zap.Config{
|
|
{
|
|
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
|
|
Development: true,
|
|
Encoding: "console",
|
|
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
|
|
OutputPaths: []string{"stderr"},
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
},
|
|
{
|
|
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
|
|
Development: true,
|
|
Encoding: "console",
|
|
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
|
|
OutputPaths: []string{"./logs/debug", "stderr"},
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
},
|
|
{
|
|
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
|
|
Development: true,
|
|
Encoding: "console",
|
|
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
|
|
OutputPaths: []string{"./logs/debug_test"},
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
},
|
|
{
|
|
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
|
|
Development: false,
|
|
Encoding: "json",
|
|
EncoderConfig: zapcore.EncoderConfig{
|
|
TimeKey: "ts",
|
|
LevelKey: "level",
|
|
NameKey: "logger",
|
|
CallerKey: "caller",
|
|
FunctionKey: zapcore.OmitKey,
|
|
MessageKey: "msg",
|
|
StacktraceKey: "stacktrace",
|
|
LineEnding: zapcore.DefaultLineEnding,
|
|
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
|
EncodeTime: zapcore.ISO8601TimeEncoder,
|
|
EncodeDuration: zapcore.SecondsDurationEncoder,
|
|
EncodeCaller: zapcore.ShortCallerEncoder,
|
|
},
|
|
OutputPaths: []string{"stderr"},
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
},
|
|
}
|
|
)
|
|
|
|
func SetLogger(env LogEnvironment) {
|
|
var logDir = "./logs"
|
|
|
|
switch env {
|
|
case Development:
|
|
// file system not used
|
|
case Debug:
|
|
if _, err := os.Stat(logDir); os.IsNotExist(err) {
|
|
if err = os.Mkdir(logDir, os.ModePerm); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
if _, err := os.Stat("./logs/debug"); os.IsNotExist(err) {
|
|
file, err := os.Create(filepath.Join(logDir, "debug"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
_ = file.Close()
|
|
}
|
|
case Testing:
|
|
if _, err := os.Stat(logDir); os.IsNotExist(err) {
|
|
if err = os.Mkdir(logDir, os.ModePerm); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
if _, err := os.Stat("./logs/debug_test"); os.IsNotExist(err) {
|
|
file, err := os.Create(filepath.Join(logDir, "debug_test"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
_ = file.Close()
|
|
}
|
|
|
|
AtExit.Register(DeleteTestLog)
|
|
case Production:
|
|
// file system not used
|
|
}
|
|
|
|
Logger = zap.Must(LogConfigs[env].Build())
|
|
}
|
|
|
|
func DeleteTestLog() {
|
|
_ = os.Remove("./logs/debug_test")
|
|
|
|
dir, _ := os.Open("./logs")
|
|
if _, err := dir.Readdirnames(1); err == io.EOF {
|
|
_ = os.Remove("./logs")
|
|
}
|
|
}
|
|
|
|
func DebugOnError(err error) {
|
|
if err != nil {
|
|
Logger.Debug(err.Error())
|
|
}
|
|
}
|
|
|
|
func InfoOnError(err error) {
|
|
if err != nil {
|
|
Logger.Info(err.Error())
|
|
}
|
|
}
|
|
|
|
func WarnOnError(err error) {
|
|
if err != nil {
|
|
Logger.Warn(err.Error())
|
|
}
|
|
}
|
|
|
|
func ErrorOnError(err error) {
|
|
if err != nil {
|
|
Logger.Error(err.Error())
|
|
}
|
|
}
|
|
|
|
func FatalOnError(err error) {
|
|
if err != nil {
|
|
AtExit.Cleanup()
|
|
Logger.Fatal(err.Error())
|
|
}
|
|
}
|
|
|
|
func PanicOnError(err error) {
|
|
if err != nil {
|
|
AtExit.Cleanup()
|
|
Logger.Panic(err.Error())
|
|
}
|
|
}
|