log rotation with lumberjack
This commit is contained in:
parent
1aad6429ae
commit
6df7facc6c
1
go.mod
1
go.mod
|
@ -23,6 +23,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/swaywm/swaybg v1.2.1 // indirect
|
||||
github.com/xyproto/wallutils v0.0.0-20241029150533-f105e9c53323
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -13,6 +13,10 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
|||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||
github.com/natefinch/lumberjack/v3 v3.0.0-alpha h1:HZ2AJF20D1lo9S0F/rpgkFbPGam5dgR3X0KUtZA5mlY=
|
||||
github.com/natefinch/lumberjack/v3 v3.0.0-alpha/go.mod h1:rPTlHhMjhrvPAhqKh0FC57E0pXZoanrXgMDj4yv5wcM=
|
||||
github.com/stretchr/powerwalk v0.0.0-20151124150408-bceb9d014549 h1:zjTKDjwZy1IVyLos+s70iYMwL/ZVojUbIDX5kghHa1Q=
|
||||
github.com/stretchr/powerwalk v0.0.0-20151124150408-bceb9d014549/go.mod h1:RhJzAYfVBD/ULOCeUUaTn0CHDRQBYHjDY7yYpSt6+4M=
|
||||
github.com/swaywm/swaybg v1.2.1 h1:TvQYiifR9JW4dANxxb7HMbEaZdGnZ4CWynevENocIWU=
|
||||
|
|
28
main.go
28
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/natefinch/lumberjack"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
@ -53,15 +54,6 @@ func main() {
|
|||
waitGroup.Wait()
|
||||
}
|
||||
|
||||
func openLogfile() (*os.File, error) {
|
||||
//TODO: log rotation
|
||||
f, err := os.OpenFile(config.Cache+"logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func cleanExit(code int) {
|
||||
if code == 1 {
|
||||
log.Println("shutting down gracefully after errors")
|
||||
|
@ -133,15 +125,17 @@ func setupLogging(debug bool) {
|
|||
clog.Printf("debug: %v", debug)
|
||||
//SETUP LOGGERS
|
||||
if debug {
|
||||
logfile, err := openLogfile()
|
||||
if err != nil {
|
||||
elog.Println("failed to load config", err)
|
||||
os.Exit(1)
|
||||
logfile := config.Cache + "logfile"
|
||||
clog.Printf("Logging to %v", logfile)
|
||||
output := &lumberjack.Logger{
|
||||
Filename: logfile,
|
||||
MaxSize: 1, // megabytes after which new file is created
|
||||
MaxBackups: 3, // number of backups
|
||||
MaxAge: 28, //days
|
||||
}
|
||||
clog.Printf("Logging to %v", logfile.Name())
|
||||
log.SetOutput(logfile)
|
||||
clog.SetOutput(logfile)
|
||||
elog.SetOutput(logfile)
|
||||
log.SetOutput(output)
|
||||
clog.SetOutput(output)
|
||||
elog.SetOutput(output)
|
||||
}
|
||||
elog.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
log.SetFlags(log.LstdFlags)
|
||||
|
|
Loading…
Reference in New Issue