add log test

This commit is contained in:
ehlxr 2019-11-06 10:11:12 +08:00
parent 7c38410537
commit 9712dbfd2c
7 changed files with 57 additions and 20 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
.vscode
go.sum
/.idea/
logs

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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())

2
go.mod
View File

@ -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

30
log.go
View File

@ -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{}) {

25
log_test.go Normal file
View File

@ -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")
}