支持配置是否打印到文件
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/yuninks/loggerx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx := context.Background()
|
||||||
|
log := loggerx.NewLogger(ctx,
|
||||||
|
// loggerx.SetPrintFile(false),
|
||||||
|
loggerx.SetToConsole(),
|
||||||
|
)
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
log.Info(ctx, "哈哈哈2")
|
||||||
|
}
|
||||||
+17
-8
@@ -12,6 +12,7 @@ type loggerOption struct {
|
|||||||
dir string // 文件目录
|
dir string // 文件目录
|
||||||
isGinLog bool
|
isGinLog bool
|
||||||
isGid bool
|
isGid bool
|
||||||
|
isPrintFile bool
|
||||||
traceField string // trace字段
|
traceField string // trace字段
|
||||||
errorToInfo bool // 错误日志是否写入info日志
|
errorToInfo bool // 错误日志是否写入info日志
|
||||||
days int // 日志保存天数
|
days int // 日志保存天数
|
||||||
@@ -23,14 +24,15 @@ type loggerOption struct {
|
|||||||
|
|
||||||
func defaultOptions() loggerOption {
|
func defaultOptions() loggerOption {
|
||||||
return loggerOption{
|
return loggerOption{
|
||||||
isGinLog: true,
|
isGinLog: true,
|
||||||
isGid: true,
|
isGid: true,
|
||||||
format: "json",
|
isPrintFile: true,
|
||||||
dir: "./log",
|
format: "json",
|
||||||
traceField: "trace_id",
|
dir: "./log",
|
||||||
days: 7,
|
traceField: "trace_id",
|
||||||
fileSplit: FileSplitTimeE,
|
days: 7,
|
||||||
timeZone: time.Local,
|
fileSplit: FileSplitTimeE,
|
||||||
|
timeZone: time.Local,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +73,13 @@ func SetFormat(format string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置是否打印到文件
|
||||||
|
func SetPrintFile(print bool) Option {
|
||||||
|
return func(o *loggerOption) {
|
||||||
|
o.isPrintFile = print
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 是否保存gin的日志
|
// 是否保存gin的日志
|
||||||
func SetGinLog(open bool) Option {
|
func SetGinLog(open bool) Option {
|
||||||
return func(o *loggerOption) {
|
return func(o *loggerOption) {
|
||||||
|
|||||||
+16
-13
@@ -5,20 +5,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (l *Logger) write(event string, b []byte) (n int, err error) {
|
func (l *Logger) write(event string, b []byte) (n int, err error) {
|
||||||
f, err := l.getFile(event, false)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err = f.Write(b)
|
if l.option.isPrintFile {
|
||||||
if err == nil && n < len(b) {
|
f, err := l.getFile(event, false)
|
||||||
err = io.ErrShortWrite
|
if err != nil {
|
||||||
}
|
return 0, err
|
||||||
if err != nil {
|
}
|
||||||
// 强制更新 & 再次写入
|
|
||||||
f, err := l.getFile(event, true)
|
n, err = f.Write(b)
|
||||||
if err == nil {
|
if err == nil && n < len(b) {
|
||||||
f.Write(b)
|
err = io.ErrShortWrite
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// 强制更新 & 再次写入
|
||||||
|
f, err := l.getFile(event, true)
|
||||||
|
if err == nil {
|
||||||
|
f.Write(b)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user