zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.08k stars 3.93k forks source link

The log rotation format is chaotic, leading to truncation and missing corresponding line breaks. #4395

Open JellyTony opened 2 weeks ago

JellyTony commented 2 weeks ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior, if applicable:

  1. The code is

    func TestZap(t *testing.T) {
    sync, err := logx.NewLogger("../logs/stat.log", logx.DefaultRotateRule("../logs/stat.log", "-", 0, false), false)
    assert.NoError(t, err)
    syncer := zapcore.AddSync(sync)
    encoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
    core := zapcore.NewCore(encoder, syncer, zapcore.DebugLevel)
    
    logger := zap.New(core)
    logger.Sync()
    
    // {"level": ... ,"ts" ... ,"msg":"hello world"}
    // 一行日志大约 60B,写入 100000 次,总大小为 6 MB+
    for i := 0; i < 100000; i++ {
        logger.Debug("hello world")
        logger.Info("hello world")
        logger.Warn("hello world")
        logger.Error("hello world")
    }
    }
  2. The error is

    
    {"level":"info","ts":1727265133.6969411,"msg":"hello world"}
    {"level":"info","ts":1727265133.6969411,"msg":"hello world"}{"level":"error","ts":1727265133.6970549,"msg":"hello world"}{"level":"error","ts":1727265133.6970549,"msg":"hello world"}
    {"level":"info","ts"::1727265133.697073,"msg":"hello world"}
    {"level":"info","ts":1727265133.697075,"msg":"hello world"}
    {"level":"error","ts":1727265133.6970859,"msg":"hello world"{"level":"error","ts":1727265133.6970859,"msg":"hello world"}
    {"level":"info","ts":1727265133.697118,"msg":"hello world"}
    }
    {"level":"info","ts":1727265133.697118,"msg":"hello world"}
    }{"level":"error","ts":1727265133.69714,"msg":"hello world"}
    {"level":"error","ts":1727265133.69714,"msg":"hello world"}

{"level":"info","ts":1727265133.6971579,"msg":"hello world"} {"level":"info","ts":1727265133.6971579,"msg":"hello world"}{"level":"info","ts":1727265133.6971579,"msg":"hello world"}{"level":"info","ts":1727265133.6971579,"msg":"hello world"} {"level":"warn","ts":1727265133.697186,"msg":"hello world"}



**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environments (please complete the following information):**
 - OS: [e.g. Linux]
 - go-zero version [e.g. 1.2.1]
 - goctl version [e.g. 1.2.1, optional]

**More description**
go-zero 的日志轮转,我发现在使用 zap 的时候 日志会混乱,{"level":"error","ts":"2024-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","
"a":123,"v":1g_test.go:1374-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","a":123,"v":1}
"a":123,"v":1g_test.go:1374-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","a":123,"v":1}
"a":123,"v":1g_test.go:1374-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","a":123,"v":1}

"a":123,"v":1g_test.go:1374-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","
"a":123,"v":1g_test.go:1374-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","v":1,"a":123}
"a":123,"v":1g_test.go:1374-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","v":1,"a":123}
"a":123,"v":1g_test.go:1374-09-25T16:22:30.567+0800","caller":"logger/logging_test.go:150","msg":"test msg","v":1,"a":123}输出类似这种,我仔细看了一下是 channel 缓冲导致的,在同时打印多种日志级别即会出现
kevwan commented 1 week ago

Does it work well if you only use logx?