-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: 去掉 github.com/lestrrat-go/file-rotatelogs 库的依赖, 实现zap.String(…
…"business", "xx")自定义业务日志数据路径, 输出文件路径为./global.GVA_CONFIG.Zap.Director/2006-01-02/xx/level(debug, info, warn, error, dpanic, panic, fatal).log (#1551)
- Loading branch information
1 parent
84224cd
commit 2cb0669
Showing
5 changed files
with
102 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package internal | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Cutter struct { | ||
level string // 日志级别(debug, info, warn, error, dpanic, panic, fatal) | ||
format string // 时间格式(2006-01-02) | ||
Director string // 日志文件夹 | ||
file *os.File // 文件句柄 | ||
mutex *sync.RWMutex // 读写锁 | ||
} | ||
|
||
type CutterOption func(*Cutter) | ||
|
||
// WithCutterFormat 设置时间格式 | ||
func WithCutterFormat(format string) CutterOption { | ||
return func(c *Cutter) { | ||
c.format = format | ||
} | ||
} | ||
|
||
func NewCutter(director string, level string, options ...CutterOption) *Cutter { | ||
rotate := &Cutter{ | ||
level: level, | ||
Director: director, | ||
mutex: new(sync.RWMutex), | ||
} | ||
for i := 0; i < len(options); i++ { | ||
options[i](rotate) | ||
} | ||
return rotate | ||
} | ||
|
||
// Write satisfies the io.Writer interface. It writes to the | ||
// appropriate file handle that is currently being used. | ||
// If we have reached rotation time, the target file gets | ||
// automatically rotated, and also purged if necessary. | ||
func (c *Cutter) Write(bytes []byte) (n int, err error) { | ||
c.mutex.Lock() | ||
defer func() { | ||
if c.file != nil { | ||
_ = c.file.Close() | ||
c.file = nil | ||
} | ||
c.mutex.Unlock() | ||
}() | ||
var business string | ||
if strings.Contains(string(bytes), "business") { | ||
var compile *regexp.Regexp | ||
compile, err = regexp.Compile(`{"business": "([^,]+)"}`) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if compile.Match(bytes) { | ||
finds := compile.FindSubmatch(bytes) | ||
business = string(finds[len(finds)-1]) | ||
bytes = compile.ReplaceAll(bytes, []byte("")) | ||
} | ||
compile, err = regexp.Compile(`"business": "([^,]+)"`) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if compile.Match(bytes) { | ||
finds := compile.FindSubmatch(bytes) | ||
business = string(finds[len(finds)-1]) | ||
bytes = compile.ReplaceAll(bytes, []byte("")) | ||
} | ||
} | ||
format := time.Now().Format(c.format) | ||
formats := make([]string, 0, 4) | ||
formats = append(formats, c.Director) | ||
if format != "" { | ||
formats = append(formats, format) | ||
} | ||
if business != "" { | ||
formats = append(formats, business) | ||
} | ||
formats = append(formats, c.level+".log") | ||
filename := filepath.Join(formats...) | ||
dirname := filepath.Dir(filename) | ||
err = os.MkdirAll(dirname, 0755) | ||
if err != nil { | ||
return 0, err | ||
} | ||
c.file, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return c.file.Write(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters