easycz / logrotate Goto Github PK
View Code? Open in Web Editor NEWConcurrency safe log rotating writer for Golang
Home Page: https://godoc.org/github.com/easyCZ/logrotate
License: MIT License
Concurrency safe log rotating writer for Golang
Home Page: https://godoc.org/github.com/easyCZ/logrotate
License: MIT License
Sample code, running on Microsoft Windows, which panics:
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"time"
"github.com/easyCZ/logrotate"
jww "github.com/spf13/jwalterweatherman"
)
func main() {
jww.SetLogThreshold(jww.LevelTrace)
jww.SetStdoutThreshold(jww.LevelTrace)
writer, err := logrotate.New(jww.TRACE, logrotate.Options{
// Where should the writer be outputting files?
// If the directory does not exist, it will be created.
// Required.
Directory: getExecutableDir(),
// What is the maximum size of each file?
// Optional. Use 0 for unlimited.
MaximumFileSize: 1024 * 1024 * 1024,
// How often should a new file be created, based on time?
// Optional. Use 0 to disable time based log rotation.
MaximumLifetime: time.Hour,
// How would you like to name your files?
// Invoked each time a new file is being created.
FileNameFunc: logrotate.DefaultFilenameFunc,
})
if err != nil {
fatal(err)
}
jww.SetLogOutput(writer)
jww.INFO.Println("This is a test info log.")
jww.TRACE.Println("This is a test trace log.")
jww.ERROR.Println("This is a test error log.")
jww.FATAL.Println("This is a test fatal log.")
// Ensure all messages are flushed to files before exiting
err = writer.Close()
if err != nil {
fatal(err)
}
}
func fatal(err error) {
jww.FATAL.Println(err)
fmt.Print("Press the Enter key to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
os.Exit(1)
}
func getExecutableDir() string {
ex, err := os.Executable()
if err != nil {
jww.FATAL.Println(err)
panic("Failed to find the executable directory!")
}
dir := filepath.Dir(ex)
return dir
}
Though, on the playground, it works.
On Microsoft Windows it fails with:
INFO 2021/07/22 16:23:43 This is a test info log.
TRACE 2021/07/22 16:23:43 This is a test trace log.
ERROR 2021/07/22 16:23:43 This is a test error log.
FATAL 2021/07/22 16:23:43 This is a test fatal log.
TRACE 2021/07/22 16:23:43 Failed to create log file failed to create new file at D:\2021-07-22T14:23:43Z-spD.log: open D:\2021-07-22T14:23:43Z-spD.log: The filename, directory name, or volume label syntax is incorrect.
TRACE 2021/07/22 16:23:43 Failed to rotate log file failed to create new file at D:\2021-07-22T14:23:43Z-dry.log: open D:\2021-07-22T14:23:43Z-dry.log: The filename, directory name, or volume label syntax is incorrect.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0xebbd79]
goroutine 6 [running]:
bufio.(*Writer).Available(...)
C:/Program Files/Go/src/bufio/bufio.go:624
bufio.(*Writer).Write(0x0, 0xc000010200, 0x32, 0x40, 0xe10, 0x0, 0x1)
C:/Program Files/Go/src/bufio/bufio.go:634 +0x79
github.com/easyCZ/logrotate.(*Writer).listen(0xc0000a6000)
D:/SOME/PATH/vendor/github.com/easyCZ/logrotate/writer.go:139 +0x13b
created by github.com/easyCZ/logrotate.New
D:/SOME/PATH/vendor/github.com/easyCZ/logrotate/writer.go:206 +0x199
There is currently no setting for deleting files. If there are too many files, is it better to set a parameter “MaximumFiles” to delete them?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.