release v0.0.4
This commit is contained in:
parent
b3fbc466f1
commit
8dcedb0ce7
@ -6,15 +6,13 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// 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))
|
||||
log.Fatalf("open crash log file error. %v", err)
|
||||
} else {
|
||||
_ = syscall.Dup2(int(f.Fd()), 2)
|
||||
}
|
||||
|
@ -6,15 +6,13 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// 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))
|
||||
log.Fatalf("open crash log file error. %v", err)
|
||||
} else {
|
||||
_ = syscall.Dup3(int(f.Fd()), 2, 0)
|
||||
}
|
||||
|
@ -6,8 +6,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -34,7 +32,7 @@ func NewCrashLog(file string) {
|
||||
} else {
|
||||
err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
|
||||
if err != nil {
|
||||
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
|
||||
log.Fatalf("open crash log file error. %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -3,8 +3,8 @@ module github.com/ehlxr/log
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/ehlxr/lumberjack v2.0.0+incompatible
|
||||
github.com/robfig/cron/v3 v3.0.0
|
||||
go.uber.org/zap v1.12.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
)
|
||||
|
39
log.go
39
log.go
@ -2,7 +2,7 @@ package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
"github.com/ehlxr/lumberjack"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"github.com/ehlxr/log/bufferpool"
|
||||
"github.com/ehlxr/log/crash"
|
||||
"github.com/ehlxr/log/encoder"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/robfig/cron/v3"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
@ -63,7 +62,7 @@ func (lc *logConfig) Init() {
|
||||
}
|
||||
|
||||
func NewLogConfig() *logConfig {
|
||||
config := &logConfig{
|
||||
return &logConfig{
|
||||
Level: DebugLevel,
|
||||
EnableColors: true,
|
||||
CrashLogFilename: "./logs/crash.log",
|
||||
@ -74,16 +73,15 @@ func NewLogConfig() *logConfig {
|
||||
TimestampFormat: "2006-01-02 15:04:05.000",
|
||||
EnableCapitalLevel: true,
|
||||
Logger: &lumberjack.Logger{
|
||||
Filename: "./logs/log.log",
|
||||
MaxSize: 200,
|
||||
MaxAge: 0,
|
||||
MaxBackups: 30,
|
||||
LocalTime: true,
|
||||
Compress: false,
|
||||
Filename: "./logs/log.log",
|
||||
MaxSize: 200,
|
||||
MaxAge: 0,
|
||||
MaxBackups: 30,
|
||||
LocalTime: true,
|
||||
Compress: false,
|
||||
BackupTimeFormat: "2006-01-02",
|
||||
},
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (lc *logConfig) newLogger() *zap.Logger {
|
||||
@ -234,18 +232,19 @@ func (lc *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
|
||||
// }
|
||||
|
||||
writer := &lumberjack.Logger{
|
||||
Filename: fileName,
|
||||
MaxSize: lc.MaxSize, // 单个日志文件大小(MB)
|
||||
MaxBackups: lc.MaxBackups,
|
||||
MaxAge: lc.MaxAge, // 保留多少天的日志
|
||||
LocalTime: lc.LocalTime,
|
||||
Compress: lc.Compress,
|
||||
Filename: fileName,
|
||||
MaxSize: lc.MaxSize, // 单个日志文件大小(MB)
|
||||
MaxBackups: lc.MaxBackups,
|
||||
MaxAge: lc.MaxAge, // 保留多少天的日志
|
||||
LocalTime: lc.LocalTime,
|
||||
Compress: lc.Compress,
|
||||
BackupTimeFormat: lc.BackupTimeFormat,
|
||||
}
|
||||
|
||||
// Rotating log files daily
|
||||
runner := cron.New(cron.WithSeconds(), cron.WithLocation(time.UTC))
|
||||
_, _ = runner.AddFunc("0 0 0 * * ? ", func() {
|
||||
_ = writer.Rotate()
|
||||
_, _ = runner.AddFunc("0 0 0 * * ?", func() {
|
||||
_ = writer.Rotate(time.Now().AddDate(0, 0, -1))
|
||||
})
|
||||
go runner.Run()
|
||||
|
||||
@ -256,7 +255,7 @@ func writeCrashLog(file string) {
|
||||
err := os.MkdirAll(path.Dir(file), os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("make crash log dir error. %v",
|
||||
errors.WithStack(err))
|
||||
err)
|
||||
}
|
||||
|
||||
crash.NewCrashLog(file)
|
||||
|
@ -26,14 +26,12 @@ func TestLogWithConfig(t *testing.T) {
|
||||
|
||||
func TestLogRote(t *testing.T) {
|
||||
lc := NewLogConfig()
|
||||
_ = lc.Level.Set("info")
|
||||
lc.Name = "main"
|
||||
lc.MaxSize = 1
|
||||
|
||||
lc.Init()
|
||||
|
||||
for {
|
||||
Infof("this is %s message", "info")
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(time.Millisecond * 1)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user