Compare commits

..

No commits in common. "master" and "v0.0.3" have entirely different histories.

10 changed files with 97 additions and 159 deletions

View File

@ -1,6 +1 @@
# logger
[![license](https://badgen.net/badge/license/MIT/blue)](./LICENSE)
[![](https://badgen.net/github/commits/ehlxr/log)](https://github.com/ehlxr/log/commits/)
[![](https://badgen.net/github/last-commit/ehlxr/log)]((https://github.com/ehlxr/log/commits/))
[![](https://badgen.net/github/releases/ehlxr/log)](https://github.com/ehlxr/log/releases)

View File

@ -6,13 +6,15 @@ 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", err)
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
} else {
_ = syscall.Dup2(int(f.Fd()), 2)
}

View File

@ -6,13 +6,15 @@ 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", err)
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
} else {
_ = syscall.Dup3(int(f.Fd()), 2, 0)
}

View File

@ -6,6 +6,8 @@ import (
"log"
"os"
"syscall"
"github.com/pkg/errors"
)
var (
@ -32,7 +34,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", err)
log.Fatalf("open crash log file error. %v", errors.WithStack(err))
}
}
}

View File

@ -68,13 +68,12 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
if enc.TimeKey != "" && enc.EncodeTime != nil {
enc.EncodeTime(ent.Time, arr)
}
if enc.LevelKey != "" && enc.EncodeLevel != nil {
enc.EncodeLevel(ent.Level, arr)
}
if ent.LoggerName != "" && enc.NameKey != "" {
nameEncoder := enc.EncodeName
if nameEncoder == nil {
// Fall back to FullNameEncoder for backward compatibility.
nameEncoder = zapcore.FullNameEncoder
@ -82,23 +81,17 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
nameEncoder(ent.LoggerName, arr)
}
if ent.Caller.Defined && enc.CallerKey != "" && enc.EncodeCaller != nil {
enc.EncodeCaller(ent.Caller, arr)
}
for i := range arr.elems {
// if i > 0 {
// line.AppendByte('\t')
// }
_, _ = fmt.Fprint(line, arr.elems[i])
}
putSliceEncoder(arr)
// Add any structured context.
enc.writeContext(line, fields)
// Add the message itself.
if enc.MessageKey != "" {
// c.addTabIfNecessary(line)
@ -106,6 +99,9 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
line.AppendString(ent.Message)
}
// Add any structured context.
enc.writeContext(line, fields)
// If there's no stacktrace key, honor that; this allows users to force
// single-line output.
if ent.Stack != "" && enc.StacktraceKey != "" {
@ -118,7 +114,6 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
} else {
line.AppendString(zapcore.DefaultLineEnding)
}
return line, nil
}
@ -133,10 +128,9 @@ func (enc textEncoder) writeContext(line *buffer.Buffer, extra []zapcore.Field)
}
// c.addTabIfNecessary(line)
// line.AppendByte('{')
line.AppendByte(' ')
line.AppendByte('{')
_, _ = line.Write(context.buf.Bytes())
// line.AppendByte('}')
line.AppendByte('}')
}
func (enc textEncoder) addTabIfNecessary(line *buffer.Buffer) {
@ -222,13 +216,11 @@ func (enc *textEncoder) AddDuration(key string, val time.Duration) {
func (enc *textEncoder) AddFloat64(key string, val float64) {
enc.addKey(key)
enc.appendFloat(val, 64)
enc.buf.AppendByte('}')
}
func (enc *textEncoder) AddInt64(key string, val int64) {
enc.addKey(key)
enc.addElementSeparator()
enc.buf.AppendInt(val)
enc.buf.AppendByte('}')
}
func (enc *textEncoder) AddReflected(key string, obj interface{}) error {
enc.resetReflectBuf()
@ -249,10 +241,9 @@ func (enc *textEncoder) OpenNamespace(key string) {
func (enc *textEncoder) AddString(key, val string) {
enc.addKey(key)
enc.addElementSeparator()
// enc.buf.AppendByte('"')
enc.buf.AppendByte('"')
enc.safeAddString(val)
// enc.buf.AppendByte('"')
enc.buf.AppendByte('}')
enc.buf.AppendByte('"')
}
func (enc *textEncoder) AddTime(key string, val time.Time) {
enc.addKey(key)
@ -270,7 +261,6 @@ func (enc *textEncoder) AddUint64(key string, val uint64) {
enc.addKey(key)
enc.addElementSeparator()
enc.buf.AppendUint(val)
enc.buf.AppendByte('}')
}
//noinspection GoRedundantConversion
@ -303,39 +293,15 @@ func (enc *textEncoder) clone() *textEncoder {
func (enc *textEncoder) addKey(key string) {
enc.addElementSeparator()
// enc.buf.AppendByte('"')
if enc.replaceSeparator('}') {
enc.buf.AppendByte(',')
enc.buf.AppendByte(' ')
} else {
enc.buf.AppendByte('{')
}
enc.buf.AppendByte('"')
enc.safeAddString(key)
// enc.buf.AppendByte('"')
// enc.buf.AppendByte(':')
enc.buf.AppendByte('=')
enc.buf.AppendByte('"')
enc.buf.AppendByte(':')
if enc.spaced {
enc.buf.AppendByte(' ')
}
}
func (enc *textEncoder) replaceSeparator(v byte) bool {
last := enc.buf.Len() - 1
if last < 0 {
return false
}
if enc.buf.Bytes()[last] == v {
t := enc.buf.Bytes()[:last]
enc.buf.Reset()
_, _ = enc.buf.Write(t)
return true
}
return false
}
func (enc *textEncoder) addElementSeparator() {
last := enc.buf.Len() - 1
if last < 0 {
@ -345,7 +311,7 @@ func (enc *textEncoder) addElementSeparator() {
case '{', '[', ':', ',', ' ':
return
default:
// enc.buf.AppendByte(',')
enc.buf.AppendByte(',')
if enc.spaced {
enc.buf.AppendByte(' ')
}

11
go.mod
View File

@ -3,11 +3,8 @@ module github.com/ehlxr/log
go 1.13
require (
github.com/ehlxr/lumberjack v0.0.2-0.20200107093220-2a579f1b2e4d
github.com/robfig/cron/v3 v3.0.1
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee // indirect
honnef.co/go/tools v0.1.0 // indirect
github.com/pkg/errors v0.8.1
github.com/robfig/cron/v3 v3.0.0
go.uber.org/zap v1.12.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

View File

@ -35,15 +35,15 @@ const (
White
)
func (config *logConfig) initColor() {
func (lc *logConfig) initColor() {
for level, color := range _levelToColor {
lcs := level.String()
if config.EnableCapitalLevel {
if lc.EnableCapitalLevel {
lcs = level.CapitalString()
}
if config.EnableLevelTruncation {
if lc.EnableLevelTruncation {
lcs = lcs[:4]
}
_levelToColorStrings[level] = color.Add(lcs)

113
log.go
View File

@ -2,7 +2,7 @@ package log
import (
"fmt"
"github.com/ehlxr/lumberjack"
"gopkg.in/natefinch/lumberjack.v2"
"log"
"os"
"path"
@ -12,6 +12,7 @@ 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"
@ -35,7 +36,6 @@ type logConfig struct {
CrashLogFilename string
ErrorLogFilename string
EnableLineNumber bool
AddCallerSkip int
// enable the truncation of the level text to 4 characters.
EnableLevelTruncation bool
@ -44,31 +44,26 @@ type logConfig struct {
EnableCapitalLevel bool
atomicLevel zap.AtomicLevel
Name string
Fields []zap.Field
*lumberjack.Logger
}
func init() {
config := &logConfig{
lc := &logConfig{
Logger: &lumberjack.Logger{
LocalTime: true,
},
}
logger = config.newLogger().Sugar()
logger = lc.newLogger().Sugar()
}
func (config *logConfig) Init() {
logger = config.newLogger().Sugar()
}
func (config *logConfig) New() *zap.SugaredLogger {
return config.newLogger().Sugar()
func (lc *logConfig) Init() {
logger = lc.newLogger().Sugar()
}
func NewLogConfig() *logConfig {
return &logConfig{
config := &logConfig{
Level: DebugLevel,
EnableColors: true,
CrashLogFilename: "./logs/crash.log",
@ -85,59 +80,60 @@ func NewLogConfig() *logConfig {
MaxBackups: 30,
LocalTime: true,
Compress: false,
BackupTimeFormat: "2006-01-02",
},
}
return config
}
func (config *logConfig) newLogger() *zap.Logger {
if config.CrashLogFilename != "" {
writeCrashLog(config.CrashLogFilename)
func (lc *logConfig) newLogger() *zap.Logger {
if lc.CrashLogFilename != "" {
writeCrashLog(lc.CrashLogFilename)
}
config.atomicLevel = zap.NewAtomicLevelAt(config.Level)
config.initColor()
lc.atomicLevel = zap.NewAtomicLevelAt(lc.Level)
lc.initColor()
cores := []zapcore.Core{
zapcore.NewCore(
encoder.NewTextEncoder(config.encoderConfig()),
encoder.NewTextEncoder(lc.encoderConfig()),
zapcore.Lock(os.Stdout),
config.atomicLevel,
lc.atomicLevel,
)}
if config.Filename != "" {
cores = append(cores, config.fileCore())
if lc.Filename != "" {
cores = append(cores, lc.fileCore())
}
if config.ErrorLogFilename != "" {
cores = append(cores, config.errorFileCore())
if lc.ErrorLogFilename != "" {
cores = append(cores, lc.errorFileCore())
}
core := zapcore.NewTee(cores...)
var options []zap.Option
if config.EnableLineNumber {
options = append(options, zap.AddCaller(), zap.AddCallerSkip(config.AddCallerSkip))
if lc.EnableLineNumber {
options = append(options, zap.AddCaller(), zap.AddCallerSkip(1))
}
if config.EnableErrorStacktrace {
if lc.EnableErrorStacktrace {
options = append(options, zap.AddStacktrace(zapcore.ErrorLevel))
}
zapLog := zap.New(core, options...)
if config.Name != "" {
zapLog = zapLog.Named(fmt.Sprintf("[%s]", config.Name))
if lc.Name != "" {
zapLog = zapLog.Named(fmt.Sprintf("[%s]", lc.Name))
}
return zapLog.With(config.Fields...)
return zapLog
}
func (config *logConfig) encoderConfig() zapcore.EncoderConfig {
el := config.encodeLevel
if config.EnableColors {
el = config.encodeColorLevel
func (lc *logConfig) encoderConfig() zapcore.EncoderConfig {
el := lc.encodeLevel
if lc.EnableColors {
el = lc.encodeColorLevel
}
return zapcore.EncoderConfig{
@ -151,8 +147,8 @@ func (config *logConfig) encoderConfig() zapcore.EncoderConfig {
EncodeLevel: el,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
tf := time.RFC3339
if config.TimestampFormat != "" {
tf = config.TimestampFormat
if lc.TimestampFormat != "" {
tf = lc.TimestampFormat
}
enc.AppendString(t.Format(fmt.Sprintf("[%s]", tf)))
},
@ -163,40 +159,40 @@ func (config *logConfig) encoderConfig() zapcore.EncoderConfig {
}
}
func (config *logConfig) fileCore() zapcore.Core {
func (lc *logConfig) fileCore() zapcore.Core {
return zapcore.NewCore(
encoder.NewTextEncoder(config.encoderConfig()),
encoder.NewTextEncoder(lc.encoderConfig()),
// zapcore.NewMultiWriteSyncer(
// zapcore.Lock(os.Stdout),
// config.fileWriteSyncer(),
// lc.fileWriteSyncer(),
// ),
config.fileWriteSyncer(config.Filename),
config.atomicLevel,
lc.fileWriteSyncer(lc.Filename),
lc.atomicLevel,
)
}
func (config *logConfig) errorFileCore() zapcore.Core {
func (lc *logConfig) errorFileCore() zapcore.Core {
return zapcore.NewCore(
encoder.NewTextEncoder(config.encoderConfig()),
encoder.NewTextEncoder(lc.encoderConfig()),
config.fileWriteSyncer(config.ErrorLogFilename),
lc.fileWriteSyncer(lc.ErrorLogFilename),
zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
}),
)
}
func (config *logConfig) encodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
func (lc *logConfig) encodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
levelString := l.CapitalString()
if config.EnableLevelTruncation {
if lc.EnableLevelTruncation {
levelString = levelString[:4]
}
enc.AppendString(fmt.Sprintf("[%s]", levelString))
}
func (config *logConfig) encodeColorLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
func (lc *logConfig) encodeColorLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
s, ok := _levelToColorStrings[l]
if !ok {
s = _unknownLevelColor.Add(l.CapitalString())
@ -225,7 +221,7 @@ func trimCallerFilePath(ec zapcore.EntryCaller) string {
return fmt.Sprintf(" %s", caller)
}
func (config *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
func (lc *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
// go get github.com/lestrrat-go/file-rotatelogs
// writer, err := rotatelogs.New(
// name+".%Y%m%d",
@ -239,18 +235,17 @@ func (config *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
writer := &lumberjack.Logger{
Filename: fileName,
MaxSize: config.MaxSize, // 单个日志文件大小MB
MaxBackups: config.MaxBackups,
MaxAge: config.MaxAge, // 保留多少天的日志
LocalTime: config.LocalTime,
Compress: config.Compress,
BackupTimeFormat: config.BackupTimeFormat,
MaxSize: lc.MaxSize, // 单个日志文件大小MB
MaxBackups: lc.MaxBackups,
MaxAge: lc.MaxAge, // 保留多少天的日志
LocalTime: lc.LocalTime,
Compress: lc.Compress,
}
// Rotating log files daily
runner := cron.New(cron.WithSeconds(), cron.WithLocation(time.Local))
runner := cron.New(cron.WithSeconds(), cron.WithLocation(time.UTC))
_, _ = runner.AddFunc("0 0 0 * * ? ", func() {
_ = writer.Rotate(time.Now().AddDate(0, 0, -1))
_ = writer.Rotate()
})
go runner.Run()
@ -261,7 +256,7 @@ func writeCrashLog(file string) {
err := os.MkdirAll(path.Dir(file), os.ModePerm)
if err != nil {
log.Fatalf("make crash log dir error. %v",
err)
errors.WithStack(err))
}
crash.NewCrashLog(file)
@ -270,7 +265,3 @@ func writeCrashLog(file string) {
func Fields(args ...interface{}) {
logger = logger.With(args...)
}
func With(l *zap.SugaredLogger, args ...interface{}) *zap.SugaredLogger {
return l.With(args...)
}

View File

@ -3,8 +3,6 @@ package log
import (
"testing"
"time"
"go.uber.org/zap"
)
func TestLog(t *testing.T) {
@ -15,42 +13,27 @@ func TestLog(t *testing.T) {
}
func TestLogWithConfig(t *testing.T) {
config := NewLogConfig()
_ = config.Level.Set("debug")
config.Name = "main"
// config.Fields = []zap.Field{zap.String("traceid", "12123123123")}
lc := NewLogConfig()
_ = lc.Level.Set("info")
lc.Name = "main"
lc.Init()
config.Init()
Fields("traceid", float64(21221212122))
Debugf("this is %s message", "debug")
config.Init()
Fields(zap.String("traceid", "12123123123"))
Infof("this is %s message", "info")
// Errorf("this is %s message", "error")
Errorf("this is %s message", "error")
// Panicf("this is %s message", "panic")
}
func TestLogWithNew(t *testing.T) {
config := NewLogConfig()
_ = config.Level.Set("debug")
config.Name = "main"
logger := config.New()
log := With(logger, "traceid", float64(21221212122), "request", "[POST]/hello/v2")
log.Debugf("this is %s message", "debug")
log.Infof("this is %s message", "info")
}
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.Millisecond * 1)
time.Sleep(time.Second)
}
}

View File

@ -1 +1 @@
v0.0.11
v0.0.2