From 9712dbfd2c3fded977afd24454721f169caba164 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Wed, 6 Nov 2019 10:11:12 +0800 Subject: [PATCH] add log test --- .gitignore | 4 +++- crash/crash_darwin.go | 6 +++--- crash/crash_unix.go | 6 +++--- crash/crash_win.go | 4 ++-- go.mod | 2 ++ log.go | 30 +++++++++++++++++++----------- log_test.go | 25 +++++++++++++++++++++++++ 7 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 log_test.go diff --git a/.gitignore b/.gitignore index bcf81ef..afc4a7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .vscode -go.sum \ No newline at end of file +go.sum +/.idea/ +logs \ No newline at end of file diff --git a/crash/crash_darwin.go b/crash/crash_darwin.go index ae7ce3d..4f1d7c8 100644 --- a/crash/crash_darwin.go +++ b/crash/crash_darwin.go @@ -10,12 +10,12 @@ import ( "github.com/pkg/errors" ) -// CrashLog set crash log -func CrashLog(file string) { +// NewCrashLog set crash log +func NewCrashLog(file string) { f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("open crash log file error. %v", errors.WithStack(err)) } else { - syscall.Dup2(int(f.Fd()), 2) + _ = syscall.Dup2(int(f.Fd()), 2) } } diff --git a/crash/crash_unix.go b/crash/crash_unix.go index 34ba929..147d89f 100644 --- a/crash/crash_unix.go +++ b/crash/crash_unix.go @@ -10,12 +10,12 @@ import ( "github.com/pkg/errors" ) -// CrashLog set crash log -func CrashLog(file string) { +// NewCrashLog set crash log +func NewCrashLog(file string) { f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("open crash log file error. %v", errors.WithStack(err)) } else { - syscall.Dup3(int(f.Fd()), 2, 0) + _ = syscall.Dup3(int(f.Fd()), 2, 0) } } diff --git a/crash/crash_win.go b/crash/crash_win.go index 6c23602..dc635bb 100644 --- a/crash/crash_win.go +++ b/crash/crash_win.go @@ -26,8 +26,8 @@ func setStdHandle(stdhandle int32, handle syscall.Handle) error { return nil } -// CrashLog set crash log -func CrashLog(file string) { +// NewCrashLog set crash log +func NewCrashLog(file string) { f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Println(err.Error()) diff --git a/go.mod b/go.mod index a9dd2e9..4953af0 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.13 require ( github.com/ehlxr/logger v0.0.0-20191105075740-0235eee42e0f + github.com/lestrrat-go/file-rotatelogs v2.2.0+incompatible + github.com/lestrrat-go/strftime v0.0.0-20190725011945-5c849dd2c51d // indirect github.com/pkg/errors v0.8.1 go.uber.org/zap v1.12.0 gopkg.in/natefinch/lumberjack.v2 v2.0.0 diff --git a/log.go b/log.go index 812a838..92c5487 100644 --- a/log.go +++ b/log.go @@ -2,6 +2,7 @@ package log import ( "fmt" + "gopkg.in/natefinch/lumberjack.v2" "log" "os" "path" @@ -15,7 +16,6 @@ import ( "github.com/pkg/errors" "go.uber.org/zap" "go.uber.org/zap/zapcore" - "gopkg.in/natefinch/lumberjack.v2" ) var logger *zap.SugaredLogger @@ -72,10 +72,10 @@ func (lc *logConfig) Init() { func NewLogConfig() *logConfig { return &logConfig{ Level: DebugLevel, - FilePath: "./log/", + FilePath: "./logs/", EnableColors: true, - CrashLogPath: "./log/crash.log", - ErrorLogPath: "./log/error_", + CrashLogPath: "./logs/crash.log", + ErrorLogPath: "./logs/error_", EnableLineNumber: true, EnableLevelTruncation: true, EnableErrorStacktrace: true, @@ -220,18 +220,26 @@ func trimCallerFilePath(ec zapcore.EntryCaller) string { } func fileWriteSyncer(name string) zapcore.WriteSyncer { - fileName := fmt.Sprintf("%s%s.log", - name, - time.Now().Format("2006-01-02")) + // writer, err := rotatelogs.New( + // name+".%Y%m%d", + // rotatelogs.WithLinkName(name), // 生成软链,指向最新日志文件 + // rotatelogs.WithMaxAge(7*24*time.Hour), // 文件最大保存时间 + // rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔 + // ) + // if err != nil { + // log.Fatalf("config normal logger file error. %v", errors.WithStack(err)) + // } - return zapcore.AddSync(&lumberjack.Logger{ - Filename: fileName, + writer := &lumberjack.Logger{ + Filename: fmt.Sprintf("%s%s.log", name, time.Now().Format("2006-01-02")), MaxSize: 500, // 单个日志文件大小(MB) MaxBackups: 10, MaxAge: 30, // 保留多少天的日志 LocalTime: true, Compress: true, - }) + } + + return zapcore.AddSync(writer) } func writeCrashLog(file string) { @@ -240,7 +248,7 @@ func writeCrashLog(file string) { log.Fatalf("make crash log dir error. %v", errors.WithStack(err)) } - crash.CrashLog(file) + crash.NewCrashLog(file) } func Fields(args ...interface{}) { diff --git a/log_test.go b/log_test.go new file mode 100644 index 0000000..e48144a --- /dev/null +++ b/log_test.go @@ -0,0 +1,25 @@ +package log + +import ( + "testing" +) + +func TestLog(t *testing.T) { + Debugf("this is %s message", "debug") + Infof("this is %s message", "info") + Errorf("this is %s message", "error") + // Panicf("this is %s message", "panic") +} + + +func TestLogWithConfig(t *testing.T) { + lc := NewLogConfig() + _ = lc.Level.Set("info") + lc.Name="main" + lc.Init() + + Debugf("this is %s message", "debug") + Infof("this is %s message", "info") + Errorf("this is %s message", "error") + // Panicf("this is %s message", "panic") +}