gookit / slog

📑 Lightweight, configurable, extensible logging library written in Go. Support multi level, multi outputs and built-in multi file logger, buffers, clean, rotate-file handling.一个易于使用的,轻量级、可配置、可扩展的日志库。支持多个级别,输出到多文件;内置文件日志处理、自动切割、清理、压缩等增强功能

Home Page:https://pkg.go.dev/github.com/gookit/slog

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

日志写入问题

lkeme opened this issue · comments

commented

System (please complete the following information):

  • OS: windows
  • GO Version: 1.17
  • Pkg Version: master

Describe the bug

  1. 使用默认示例代码,只生成了文件,没有实际内容写入。
  2. 使用TimeRotateFileHandler 同样无法写入。
  3. 使用RotateFileHandler,能成功写入,但是只会写入第一行(第一个输出)。
  4. 示例代码有点问题,在截图里说明。

To Reproduce

// go code

Expected behavior

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

Screenshots
image

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.

我看下

commented
// 默认buffer大小
const defaultBufferSize = 256 * 1024


// 写入部分
// enable buffer
if h.bufio == nil && h.BuffSize > 0 {
    fmt.Println(h.BuffSize)
    h.bufio = bufio.NewWriterSize(h.file, h.BuffSize)
}
_, err = h.bufio.Write(bts)
// bufio.Write方法
// 数据小于设定的缓冲长度(256 * 1024),数据会被复制到缓冲,但不会直接写入。
func (b *Writer) Write(p []byte) (nn int, err error) {
	for len(p) > b.Available() && b.err == nil {
		var n int
		if b.Buffered() == 0 {
			// Large write, empty buffer.
			// Write directly from p to avoid copy.
			n, b.err = b.wr.Write(p)
		} else {
			n = copy(b.buf[b.n:], p)
			b.n += n
			b.Flush()
		}
		nn += n
		p = p[n:]
	}
	if b.err != nil {
		return nn, b.err
	}
	n := copy(b.buf[b.n:], p)
	b.n += n
	nn += n
	return nn, nil
}

稍微看了一下,如果少量数据写入(< 256 * 1024),就不会直接写入,导致文件内无实际内容。

没细看,不清楚这个在buffer这在架构里有没有其他用意,是否算bug。

可以调整默认buffsize?或者写入后默认给个Flush()方法?

simple_file output logs to file, no buffer Write directly to file 就是来解决这种问题的?

en 默认启用了buffer 的. 可以加个 defer slog.Flush()

commented

image

NewRotateFile的逻辑也挺奇怪的,第一次打开生成了info.log,没有实际写入。

第二次打开生成了info.log.20210906_115504,只写入了第一行日志。

commented

刚才再次测试了,确实都是启用buffer引起的,给标个invalid label吧

NewRotateFile 那里是不是有点问题。按天的好像生成错了。。 :)

commented

是有些问题,不过按逻辑来讲,EveryDay不应该统一在一天里的文件里继续吗 ?

每次执行都会生成一个info.log.yyyymmdd_hhmmss ,这好像是针对时分秒的生成。

image

还有些很莫名其妙的问题,我把buff关了,写入倒是正常了。

打印了6次Info(), 第一次执行,info.log里有6行。

第二次执行,info.log里只有5行。info.log.20210906_121819里却有7行。

info.log里的第一行,跑到了 info.log.20210906_121819里最后一行。

commented

NewRotateFile 问题, 无论什么选项都是返回Every Second。

// GetIntervalAndFormat get check interval time and log suffix format
func (rt rotateTime) GetIntervalAndFormat() (checkInterval int64, suffixFormat string) {
	switch rt {
	case EveryDay:
		checkInterval = 3600 * 24
		suffixFormat = "20060102"
	case EveryHour:
		checkInterval = 3600
		suffixFormat = "20060102_1500"
	case Every30Minutes:
		checkInterval = 1800
		suffixFormat = "20060102_1504"
	case Every15Minutes:
		checkInterval = 900
		suffixFormat = "20060102_1504"
	case EveryMinute:
		checkInterval = 60
		suffixFormat = "20060102_1504"
	}

	// Every Second
	return 1, "20060102_150405"
}

en 我开个新issue处理这个问题。tks