日志写入问题
lkeme opened this issue · comments
System (please complete the following information):
- OS:
windows
- GO Version:
1.17
- Pkg Version:
master
Describe the bug
- 使用默认示例代码,只生成了文件,没有实际内容写入。
- 使用
TimeRotateFileHandler
同样无法写入。 - 使用
RotateFileHandler
,能成功写入,但是只会写入第一行(第一个输出)。 - 示例代码有点问题,在截图里说明。
To Reproduce
// go code
Expected behavior
A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.
Additional context
Add any other context about the problem here.
我看下
// 默认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()
刚才再次测试了,确实都是启用buffer引起的,给标个invalid label吧
NewRotateFile 那里是不是有点问题。按天的好像生成错了。。 :)
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