-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1c657a
commit e1108c3
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ctdao | ||
logs/* | ||
.idea | ||
go.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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),) | ||
} |