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