https://github.com/lestrrat-go/file-rotatelogs was archived,and there are some small defects.
lestrrat-go/file-rotatelogs defects
No longer output log, when too much log output rotated by size
You can use "log" or "logrus" test
package main
import (
"fmt"
"log"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)
func main() {
rl, _ := rotatelogs.New(
"/tmp/logs/access_log.%Y%m%d%H%M",
rotatelogs.WithLinkName("/tmp/logs/access_log"),
//rotatelogs.WithMaxAge(24*time.Hour),
rotatelogs.WithRotationCount(3),
//rotatelogs.WithRotationTime(time.Hour),
rotatelogs.WithRotationSize(12),
)
log.SetOutput(rl)
for i := 0; i < 1000; i++ {
fmt.Printf("Console print %d\n", i)
log.Printf("Test content %d\n", i)
time.Sleep(time.Second)
}
log.Printf("Ending pose!\n")
}
The log output is stuck in the ninth, expected to be the 1000th
ls -l /tmp/logs/
total 24
lrwxr-xr-x 1 kane wheel 26 8 22 15:33 access_log -> access_log.202108220000.10
-rw-r--r-- 1 kane wheel 35 8 22 15:33 access_log.202108220000.7
-rw-r--r-- 1 kane wheel 35 8 22 15:33 access_log.202108220000.8
-rw-r--r-- 1 kane wheel 35 8 22 15:33 access_log.202108220000.9
cat /tmp/logs/access_log.202108220000.9
2021/08/22 15:33:04 Test content 9
cat /tmp/logs/access_log
cat: /tmp/logs/access_log: No such file or directory
cat /tmp/logs/access_log.202108220000.10
cat: /tmp/logs/access_log.202108220000.10: No such file or directory
The log file name is ambiguous
You expect the log name to be accurate to minutes
rotatelogs.New("/tmp/logs/access_log.%Y%m%d%H%M")
But it's actually accurate to days,because the "rotationTime" is 1 day by default
lrwxr-xr-x 1 kane wheel 26 8 22 15:33 access_log -> access_log.202108220000.10
RotationTime doesn't work
“%Y%m%d%H%M” accurate to minutes,“rotatelogs.WithRotationTime(time.Second)” accurate to seconds invalid
package main
import (
"fmt"
"log"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)
func main() {
rl, _ := rotatelogs.New(
"/tmp/logs/access_log.%Y%m%d%H%M",
rotatelogs.WithLinkName("/tmp/logs/access_log"),
rotatelogs.WithRotationCount(3),
rotatelogs.WithRotationTime(time.Second),
)
log.SetOutput(rl)
for i := 0; i < 1000; i++ {
fmt.Printf("Console print %d\n", i)
log.Printf("Test content %d\n", i)
time.Sleep(time.Second)
}
log.Printf("Ending pose!\n")
}
Result accurate to minutes
ls -l /tmp/logs/
total 24
lrwxr-xr-x 1 kane wheel 23 8 24 22:29 access_log -> access_log.202108242229
-rw-r--r-- 1 kane wheel 2160 8 24 22:27 access_log.202108242227
-rw-r--r-- 1 kane wheel 2161 8 24 22:28 access_log.202108242228
-rw-r--r-- 1 kane wheel 1887 8 24 22:29 access_log.202108242229
No pattern no rotationTime
"/tmp/logs/access_log.%Y%m%d%H%M" -> "/tmp/logs/access_log"
package main
import (
"fmt"
"log"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)
func main() {
rl, _ := rotatelogs.New(
"/tmp/logs/access_log",
// rotatelogs.WithLinkName("/tmp/logs/access_log"),
rotatelogs.WithRotationCount(3),
rotatelogs.WithRotationTime(time.Second),
)
log.SetOutput(rl)
for i := 0; i < 1000; i++ {
fmt.Printf("Console print %d\n", i)
log.Printf("Test content %d\n", i)
time.Sleep(time.Second)
}
log.Printf("Ending pose!\n")
}
kane-MacBook:file-rotatelogs zhangkai$ ls -l /tmp/logs/
total 8
-rw-r--r-- 1 kane wheel 1070 8 24 23:05 access_log
kane-MacBook:file-rotatelogs zhangkai$ ls -l /tmp/logs/
total 8
-rw-r--r-- 1 kane wheel 1106 8 24 23:05 access_log
kane-MacBook:file-rotatelogs zhangkai$ ls -l /tmp/logs/
total 8
-rw-r--r-- 1 kane wheel 1142 8 24 23:05 access_log
kane-MacBook:file-rotatelogs zhangkai$ ls -l /tmp/logs/
total 8
-rw-r--r-- 1 kane wheel 1178 8 24 23:05 access_log
kane-MacBook:file-rotatelogs zhangkai$ ls -l /tmp/logs/
total 8
-rw-r--r-- 1 kane wheel 1394 8 24 23:05 access_log
Improvement
A large number of logs will be output only under abnormal circumstances, but the logs cannot be stuck.
You can set “WithRotationTime(time.Second)” to avoid problem. However, there are very few historical logs under normal circumstances
Fix the log output is stuck in the ninth
func TestTooMuchLog(t *testing.T) {
var (
testDir = "test_much_log"
testLogPath = filepath.Join(testDir, "access_log")
rotationCount = 3
N = 12 // N > 10
)
err := os.Mkdir(testDir, 0777)
assert.Nil(t, err)
defer os.RemoveAll(testDir)
assert.Nil(t, err)
rl, err := rotatelogs.New(
testLogPath+".%Y%m%d%H%M",
rotatelogs.WithLinkName(testLogPath),
rotatelogs.WithRotationCount(uint(rotationCount)),
rotatelogs.WithRotationSize(12), // Log contentSize > 12
)
assert.Nil(t, err)
log.SetOutput(rl)
for i := 0; i < N; i++ {
log.Printf("Test content %d\n", i)
}
files, _ := ioutil.ReadDir(testDir)
assert.Equal(t, rotationCount+1, len(files))
bytez, err := ioutil.ReadFile(testLogPath)
assert.Nil(t, err)
assert.Equal(t, strconv.Itoa(N-1), string(bytez[len(bytez)-3:len(bytez)-1]))
}
RotationTime determined according to the pattern
https://github.com/lestrrat-go/file-rotatelogs was archived,and there are some small defects.
lestrrat-go/file-rotatelogs defects
No longer output log, when too much log output rotated by size
You can use "log" or "logrus" test
The log output is stuck in the ninth, expected to be the 1000th
ls -l /tmp/logs/ total 24 lrwxr-xr-x 1 kane wheel 26 8 22 15:33 access_log -> access_log.202108220000.10 -rw-r--r-- 1 kane wheel 35 8 22 15:33 access_log.202108220000.7 -rw-r--r-- 1 kane wheel 35 8 22 15:33 access_log.202108220000.8 -rw-r--r-- 1 kane wheel 35 8 22 15:33 access_log.202108220000.9 cat /tmp/logs/access_log.202108220000.9 2021/08/22 15:33:04 Test content 9 cat /tmp/logs/access_log cat: /tmp/logs/access_log: No such file or directory cat /tmp/logs/access_log.202108220000.10 cat: /tmp/logs/access_log.202108220000.10: No such file or directoryThe log file name is ambiguous
You expect the log name to be accurate to minutes
But it's actually accurate to days,because the "rotationTime" is 1 day by default
lrwxr-xr-x 1 kane wheel 26 8 22 15:33 access_log -> access_log.202108220000.10RotationTime doesn't work
“%Y%m%d%H%M” accurate to minutes,“rotatelogs.WithRotationTime(time.Second)” accurate to seconds invalid
Result accurate to minutes
ls -l /tmp/logs/ total 24 lrwxr-xr-x 1 kane wheel 23 8 24 22:29 access_log -> access_log.202108242229 -rw-r--r-- 1 kane wheel 2160 8 24 22:27 access_log.202108242227 -rw-r--r-- 1 kane wheel 2161 8 24 22:28 access_log.202108242228 -rw-r--r-- 1 kane wheel 1887 8 24 22:29 access_log.202108242229No pattern no rotationTime
"/tmp/logs/access_log.%Y%m%d%H%M" -> "/tmp/logs/access_log"
Improvement
A large number of logs will be output only under abnormal circumstances, but the logs cannot be stuck.
You can set “WithRotationTime(time.Second)” to avoid problem. However, there are very few historical logs under normal circumstances
Fix the log output is stuck in the ninth
RotationTime determined according to the pattern