Skip to content

Commit

Permalink
add zaplog
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanshaoye committed May 25, 2022
1 parent b1c657a commit e1108c3
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ctdao
logs/*
.idea
go.sum
Binary file modified ctdao
Binary file not shown.
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/yifanshaoye/ctdao

go 1.18

require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
)
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package main

import (
"fmt"
"github.com/yifanshaoye/ctdao/zaplog"

"github.com/yifanshaoye/ctdao/database"
)

func main() {
fmt.Println("Hello, World !!!")
database.Done()

//zaplog.SetLogFilePath("./database/access.log")
zaplog.InitZaplog()
zaplog.Info("test", "test log %+v", "zaplog")
zaplog.Error("second", "send log: %+v", "hahah")
}
77 changes: 77 additions & 0 deletions zaplog/zaplog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package zaplog

import (
"errors"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"path/filepath"
"time"
)

type coreLogger struct {
logPath string
zaplog *zap.Logger

}

var logger *coreLogger

func init() {
path, _ := filepath.Abs("./logs/logger.log")
logger = &coreLogger{logPath: path}
}

func (lg *coreLogger) initLogger() {
enconf := zap.NewProductionEncoderConfig()
enconf.EncodeTime = func(tm time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(tm.Format("2006-01-02 15:04:05.000"))
}
encoder := zapcore.NewJSONEncoder(enconf)

fdir := filepath.Dir(lg.logPath)
os.MkdirAll(fdir, 0766)
file, err := os.OpenFile(lg.logPath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
lg.logPath, _= filepath.Abs("./logs/logger.log")
fdir := filepath.Dir(lg.logPath)
os.MkdirAll(fdir, 0766)
file, _ = os.OpenFile(lg.logPath, os.O_WRONLY|os.O_CREATE, 0666)
}
wrteSyncer := zapcore.AddSync(file)

core := zapcore.NewCore(encoder, wrteSyncer, zapcore.InfoLevel)


lg.zaplog = zap.New(core, zap.AddCallerSkip(1), zap.AddCaller())
}

// 初始化zaplog, 在写日志之前必须初始化
func InitZaplog() *coreLogger {
if logger.zaplog == nil {
logger.initLogger()
}
return logger
}

func SetLogFilePath(path string) error {
apath, err := filepath.Abs(path)
if err != nil {
return errors.New("invalie path !!!")
}
logger.logPath = apath
return nil
}

func Info(tag, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
logger.zaplog.Info(msg,
zap.String("tag", tag),)
}

func Error(tag, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
logger.zaplog.Error(msg,
zap.String("tag", tag),)
}

0 comments on commit e1108c3

Please sign in to comment.