Add log file close and reopen on receipt of SIGUSR1

This commit is contained in:
Richard Shepherd 2017-08-11 11:04:58 +01:00 committed by Ludovic Fernandez
parent 64b8fc52c3
commit 4e5fcac9cb
12 changed files with 343 additions and 25 deletions

View file

@ -2,14 +2,18 @@ package log
import (
"bufio"
"fmt"
"io"
"os"
"runtime"
"github.com/Sirupsen/logrus"
)
var (
logger *logrus.Entry
logger *logrus.Entry
logFilePath string
logFile *os.File
)
func init() {
@ -190,6 +194,49 @@ func Fatalln(args ...interface{}) {
logger.Fatalln(args...)
}
// OpenFile opens the log file using the specified path
func OpenFile(path string) error {
logFilePath = path
var err error
logFile, err = os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err == nil {
SetOutput(logFile)
}
return err
}
// CloseFile closes the log and sets the Output to stdout
func CloseFile() error {
logrus.SetOutput(os.Stdout)
if logFile != nil {
return logFile.Close()
}
return nil
}
// RotateFile closes and reopens the log file to allow for rotation
// by an external source. If the log isn't backed by a file then
// it does nothing.
func RotateFile() error {
if logFile == nil && logFilePath == "" {
Debug("Traefik log is not writing to a file, ignoring rotate request")
return nil
}
if err := CloseFile(); err != nil {
return fmt.Errorf("error closing log file: %s", err)
}
if err := OpenFile(logFilePath); err != nil {
return fmt.Errorf("error opening log file: %s", err)
}
return nil
}
// Writer logs writer (Level Info)
func Writer() *io.PipeWriter {
return WriterLevel(logrus.InfoLevel)