peroxide: Implement logfiles and logfile rotation

create-reload-action
Lukasz Janyst 4 years ago
parent f79a42ff86
commit c5f1ce4054
No known key found for this signature in database
GPG Key ID: 32DE641041F17A9A
  1. 32
      cmd/peroxide/main.go

@ -20,6 +20,8 @@ package main
import ( import (
"flag" "flag"
"os" "os"
"os/signal"
"syscall"
"github.com/ljanyst/peroxide/pkg/bridge" "github.com/ljanyst/peroxide/pkg/bridge"
"github.com/ljanyst/peroxide/pkg/logging" "github.com/ljanyst/peroxide/pkg/logging"
@ -28,12 +30,42 @@ import (
var config = flag.String("config", "/etc/peroxide.conf", "configuration file") var config = flag.String("config", "/etc/peroxide.conf", "configuration file")
var logLevel = flag.String("log-level", "Warning", "account name") var logLevel = flag.String("log-level", "Warning", "account name")
var logFile = flag.String("log-file", "", "output file for diagnostics")
func setLogFile(filePath string) *os.File {
logFile, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic("Cannot open log file: " + err.Error())
}
logrus.SetOutput(logFile)
return logFile
}
func rotateLogFile(filePath string) {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGHUP)
f := setLogFile(filePath)
for {
select {
case <-signalCh:
f.Close()
f = setLogFile(filePath)
logrus.Debug("Logfile rotated")
}
}
}
func main() { func main() {
flag.Parse() flag.Parse()
logging.SetLevel(*logLevel) logging.SetLevel(*logLevel)
if *logFile != "" {
go rotateLogFile(*logFile)
}
b := &bridge.Bridge{} b := &bridge.Bridge{}
if err := b.Configure(*config); err != nil { if err := b.Configure(*config); err != nil {

Loading…
Cancel
Save