add log test
This commit is contained in:
parent
7c38410537
commit
9712dbfd2c
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
.vscode
|
.vscode
|
||||||
go.sum
|
go.sum
|
||||||
|
/.idea/
|
||||||
|
logs
|
@ -10,12 +10,12 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CrashLog set crash log
|
// NewCrashLog set crash log
|
||||||
func CrashLog(file string) {
|
func NewCrashLog(file string) {
|
||||||
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
||||||
} else {
|
} else {
|
||||||
syscall.Dup2(int(f.Fd()), 2)
|
_ = syscall.Dup2(int(f.Fd()), 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,12 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CrashLog set crash log
|
// NewCrashLog set crash log
|
||||||
func CrashLog(file string) {
|
func NewCrashLog(file string) {
|
||||||
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
||||||
} else {
|
} else {
|
||||||
syscall.Dup3(int(f.Fd()), 2, 0)
|
_ = syscall.Dup3(int(f.Fd()), 2, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,8 @@ func setStdHandle(stdhandle int32, handle syscall.Handle) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CrashLog set crash log
|
// NewCrashLog set crash log
|
||||||
func CrashLog(file string) {
|
func NewCrashLog(file string) {
|
||||||
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
|
2
go.mod
2
go.mod
@ -4,6 +4,8 @@ go 1.13
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ehlxr/logger v0.0.0-20191105075740-0235eee42e0f
|
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
|
github.com/pkg/errors v0.8.1
|
||||||
go.uber.org/zap v1.12.0
|
go.uber.org/zap v1.12.0
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||||
|
30
log.go
30
log.go
@ -2,6 +2,7 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -15,7 +16,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
"gopkg.in/natefinch/lumberjack.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger *zap.SugaredLogger
|
var logger *zap.SugaredLogger
|
||||||
@ -72,10 +72,10 @@ func (lc *logConfig) Init() {
|
|||||||
func NewLogConfig() *logConfig {
|
func NewLogConfig() *logConfig {
|
||||||
return &logConfig{
|
return &logConfig{
|
||||||
Level: DebugLevel,
|
Level: DebugLevel,
|
||||||
FilePath: "./log/",
|
FilePath: "./logs/",
|
||||||
EnableColors: true,
|
EnableColors: true,
|
||||||
CrashLogPath: "./log/crash.log",
|
CrashLogPath: "./logs/crash.log",
|
||||||
ErrorLogPath: "./log/error_",
|
ErrorLogPath: "./logs/error_",
|
||||||
EnableLineNumber: true,
|
EnableLineNumber: true,
|
||||||
EnableLevelTruncation: true,
|
EnableLevelTruncation: true,
|
||||||
EnableErrorStacktrace: true,
|
EnableErrorStacktrace: true,
|
||||||
@ -220,18 +220,26 @@ func trimCallerFilePath(ec zapcore.EntryCaller) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fileWriteSyncer(name string) zapcore.WriteSyncer {
|
func fileWriteSyncer(name string) zapcore.WriteSyncer {
|
||||||
fileName := fmt.Sprintf("%s%s.log",
|
// writer, err := rotatelogs.New(
|
||||||
name,
|
// name+".%Y%m%d",
|
||||||
time.Now().Format("2006-01-02"))
|
// 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{
|
writer := &lumberjack.Logger{
|
||||||
Filename: fileName,
|
Filename: fmt.Sprintf("%s%s.log", name, time.Now().Format("2006-01-02")),
|
||||||
MaxSize: 500, // 单个日志文件大小(MB)
|
MaxSize: 500, // 单个日志文件大小(MB)
|
||||||
MaxBackups: 10,
|
MaxBackups: 10,
|
||||||
MaxAge: 30, // 保留多少天的日志
|
MaxAge: 30, // 保留多少天的日志
|
||||||
LocalTime: true,
|
LocalTime: true,
|
||||||
Compress: true,
|
Compress: true,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
return zapcore.AddSync(writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeCrashLog(file string) {
|
func writeCrashLog(file string) {
|
||||||
@ -240,7 +248,7 @@ func writeCrashLog(file string) {
|
|||||||
log.Fatalf("make crash log dir error. %v", errors.WithStack(err))
|
log.Fatalf("make crash log dir error. %v", errors.WithStack(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
crash.CrashLog(file)
|
crash.NewCrashLog(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fields(args ...interface{}) {
|
func Fields(args ...interface{}) {
|
||||||
|
25
log_test.go
Normal file
25
log_test.go
Normal 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")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user