优化为json输出
This commit is contained in:
+3
-3
@@ -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("打开日志文件失败")
|
||||
|
||||
@@ -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
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user