Log corruption and content interleaving when using Logger and Async TSVLogger simultaneously

Đang mở
#115 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
go
Lĩnh vực
backend

Hướng nghiên cứu

Start by reading the Logger, TSVLogger, AsyncWriter, and FileWriter implementations, then run the provided TestSyncAndAsyncLoggerConsistency case. Trace how the writers handle concurrent writes to separate files and check whether the test reproduces the interleaving. Done when the async TSV output contains only complete expected lines and the test passes.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Library Version: v1.0.121

Description: I encountered a log corruption issue when using a synchronous Logger and an asynchronous TSVLogger (wrapped with AsyncWriter) in the same test case. Even though the two loggers are configured to write to different files, the output in the asynchronous log file becomes interleaved and corrupted.

Specifically, fragments of the JSON format from the synchronous logger (e.g., {"time":) appear inside the TSV logger's output file, and the messages are truncated or mixed together.

Code:

func TestSyncAndAsyncLoggerConsistency(t *testing.T) {
	syncFile := "consistency_sync.log"
	asyncFile := "consistency_async.log"

	// 1. Define a synchronous JSON logger
	syncLogger := Logger{
		Writer: &FileWriter{Filename: syncFile},
	}

	// 2. Define an asynchronous TSV logger
	asyncLogger := TSVLogger{
		Writer: &AsyncWriter{
			ChannelSize:   4096,
			DisableWritev: false,
			DiscardOnFull: false,
			Writer:        &FileWriter{Filename: asyncFile},
		},
	}

	for i := 0; i < 1000; i++ {
		msg := "hello file writer"
		// Write JSON log
		syncLogger.Info().Str("line", msg).Msg("")
		// Write TSV log
		asyncLogger.New().Str(msg).Msg()
	}

	if err := syncLogger.Writer.(io.Closer).Close(); err != nil {
		t.Fatalf("close sync logger: %+v", err)
	}
	if err := asyncLogger.Writer.(io.Closer).Close(); err != nil {
		t.Fatalf("close async logger: %+v", err)
	}

	// Helper to check content consistency
	LinesEqual := func(path string, content string) error {
		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close()

		scanner := bufio.NewScanner(f)
		for scanner.Scan() {
			// In this case, we expect the async log to only contain "hello file writer"
			if scanner.Text() != content {
				return errors.New("content mismatch: " + scanner.Text() + " != " + content)
			}
		}
		return scanner.Err()
	}

	err := LinesEqual(asyncFile, "hello file writer")
	if err != nil {
		t.Fatalf("read async file: %+v", err)
	}
}

Actual Output in consistency_async.log: The log file contains corrupted lines like:

hello file writer
hello file writer
{"time":le writer
{"time":le writer
hello file writer
{"time":le writer
Ngôn ngữ chính
Go
Star
881
Fork
64
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phuslu/log

Tất cả issue của phuslu/log

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.