优化为json输出

This commit is contained in:
Yun
2024-02-03 01:57:56 +08:00
parent 1b61ac46e4
commit a08d268c92
4 changed files with 40 additions and 9 deletions
+3 -3
View File
@@ -17,13 +17,13 @@ func (l *Logger) nowFileName(event string) string {
if l.channel != "" {
timeDir = l.channel + "/" + timeDir
}
path := l.option.dir + "/" + timeDir + "_"+event + ".log"
path := l.option.dir + "/" + timeDir + "_" + event + ".log"
// fmt.Println(filepath.Abs(path))
return path
}
// 新建文件
func (l *Logger) getFile(event string,isRefresh bool) (*os.File, error) {
func (l *Logger) getFile(event string, isRefresh bool) (*os.File, error) {
f := l.loadFile(event)
if f != nil && !isRefresh {
return f, nil
@@ -38,7 +38,7 @@ func (l *Logger) getFile(event string,isRefresh bool) (*os.File, error) {
os.MkdirAll(dir, os.ModePerm) // 创建多层目录,如果存在不会报错
// 打开该文件,如果不存在则创建
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
// 打开失败,尝试创建
fmt.Println("打开日志文件失败")
+22 -3
View File
@@ -30,13 +30,32 @@ func (l *Logger) logger(ctx context.Context, event string, v ...any) {
traceId, _ := ctx.Value(l.option.traceField).(string)
writeStr := "[" + event + "]" + nowTime + " " + file + ":" + fmt.Sprintf("%d", line) + " " + funcName + " gid:" + getGID() + " " + traceId + " @data@: " + string(by) + "\n\n"
// writeStr := "[" + event + "]" + nowTime + " " + file + ":" + fmt.Sprintf("%d", line) + " " + funcName + " gid:" + getGID() + " " + traceId + " @data@: " + string(by) + "\n\n"
l.write(event, []byte(writeStr))
fd := FormatData{
Time: nowTime,
File: file + ":" + fmt.Sprintf("%d", line),
Func: funcName,
Gid: getGID(),
Content: string(by),
TraceId: traceId,
}
fdb, _ := json.Marshal(fd)
l.write(event, fdb)
if l.option.errorToInfo && event == "error" {
l.write("info", []byte(writeStr))
l.write("info", fdb)
}
// log.Println("" + string(by))
}
type FormatData struct {
Time string `json:"time,omitempty"`
File string `json:"file,omitempty"`
Func string `json:"func,omitempty"`
Gid string `json:"gid,omitempty"`
Content string `json:"content,omitempty"`
TraceId string `json:"traceId,omitempty"`
}
+2 -2
View File
@@ -17,9 +17,9 @@ func TestLogger(t *testing.T) {
l.Error(context.Background(), "test error")
l.Channel("test").Error(context.Background(), "test error")
l.Channel("channel").Error(context.Background(), "test error")
l.Info(context.Background(), "test info")
time.Sleep(time.Second * 2)
time.Sleep(time.Second * 5)
}
+13 -1
View File
@@ -1,6 +1,8 @@
package loggerx
import "io"
import (
"io"
)
func (l *Logger) write(event string, b []byte) (n int, err error) {
f, err := l.getFile(event, false)
@@ -8,6 +10,16 @@ func (l *Logger) write(event string, b []byte) (n int, err error) {
return 0, err
}
// fmt.Println("write", string(b))
// defer func() {
// fmt.Println(n, err)
// }()
// 添加换行
a := []byte("\n[" + event + "]")
b = append(a, b...)
n, err = f.Write(b)
if err == nil && n < len(b) {
err = io.ErrShortWrite