支持配置是否打印到文件
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")
|
||||||
|
}
|
||||||
@@ -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 // 日志保存天数
|
||||||
@@ -25,6 +26,7 @@ func defaultOptions() loggerOption {
|
|||||||
return loggerOption{
|
return loggerOption{
|
||||||
isGinLog: true,
|
isGinLog: true,
|
||||||
isGid: true,
|
isGid: true,
|
||||||
|
isPrintFile: true,
|
||||||
format: "json",
|
format: "json",
|
||||||
dir: "./log",
|
dir: "./log",
|
||||||
traceField: "trace_id",
|
traceField: "trace_id",
|
||||||
@@ -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) {
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ 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) {
|
||||||
|
|
||||||
|
if l.option.isPrintFile {
|
||||||
f, err := l.getFile(event, false)
|
f, err := l.getFile(event, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -21,6 +23,7 @@ func (l *Logger) write(event string, b []byte) (n int, err error) {
|
|||||||
f.Write(b)
|
f.Write(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(l.option.drivers) > 0 {
|
if len(l.option.drivers) > 0 {
|
||||||
io.MultiWriter(l.option.drivers...).Write(b)
|
io.MultiWriter(l.option.drivers...).Write(b)
|
||||||
|
|||||||
Reference in New Issue
Block a user