Commit a6aa2198 by maicon Committed by Torkel Ödegaard

Reopen log files after receiving a SIGHUP signal (#13112)

* Reopen log files after receiving a SIGHUP signal

Implements a system signals listener in log.FileLogWriter.

After receiving a SIGHUP signal from the system, the listener will close the current log file and then open it again.

The listener will finishes after receiving a SIGINT, SIGKILL or SIGTERM signal.

Closes grafana/grafana#2497

* Move the SIGHUP handling to the main file
parent dac2c625
...@@ -96,13 +96,17 @@ func main() { ...@@ -96,13 +96,17 @@ func main() {
func listenToSystemSignals(server *GrafanaServerImpl) { func listenToSystemSignals(server *GrafanaServerImpl) {
signalChan := make(chan os.Signal, 1) signalChan := make(chan os.Signal, 1)
ignoreChan := make(chan os.Signal, 1) sighupChan := make(chan os.Signal, 1)
signal.Notify(ignoreChan, syscall.SIGHUP) signal.Notify(sighupChan, syscall.SIGHUP)
signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM) signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)
select { for {
case sig := <-signalChan: select {
server.Shutdown(fmt.Sprintf("System signal: %s", sig)) case _ = <-sighupChan:
log.Reload()
case sig := <-signalChan:
server.Shutdown(fmt.Sprintf("System signal: %s", sig))
}
} }
} }
...@@ -236,3 +236,20 @@ func (w *FileLogWriter) Close() { ...@@ -236,3 +236,20 @@ func (w *FileLogWriter) Close() {
func (w *FileLogWriter) Flush() { func (w *FileLogWriter) Flush() {
w.mw.fd.Sync() w.mw.fd.Sync()
} }
// Reload file logger
func (w *FileLogWriter) Reload() {
// block Logger's io.Writer
w.mw.Lock()
defer w.mw.Unlock()
// Close
fd := w.mw.fd
fd.Close()
// Open again
err := w.StartLogger()
if err != nil {
fmt.Fprintf(os.Stderr, "Reload StartLogger: %s\n", err)
}
}
...@@ -3,3 +3,7 @@ package log ...@@ -3,3 +3,7 @@ package log
type DisposableHandler interface { type DisposableHandler interface {
Close() Close()
} }
type ReloadableHandler interface {
Reload()
}
...@@ -21,10 +21,12 @@ import ( ...@@ -21,10 +21,12 @@ import (
var Root log15.Logger var Root log15.Logger
var loggersToClose []DisposableHandler var loggersToClose []DisposableHandler
var loggersToReload []ReloadableHandler
var filters map[string]log15.Lvl var filters map[string]log15.Lvl
func init() { func init() {
loggersToClose = make([]DisposableHandler, 0) loggersToClose = make([]DisposableHandler, 0)
loggersToReload = make([]ReloadableHandler, 0)
Root = log15.Root() Root = log15.Root()
Root.SetHandler(log15.DiscardHandler()) Root.SetHandler(log15.DiscardHandler())
} }
...@@ -115,6 +117,12 @@ func Close() { ...@@ -115,6 +117,12 @@ func Close() {
loggersToClose = make([]DisposableHandler, 0) loggersToClose = make([]DisposableHandler, 0)
} }
func Reload() {
for _, logger := range loggersToReload {
logger.Reload()
}
}
func GetLogLevelFor(name string) Lvl { func GetLogLevelFor(name string) Lvl {
if level, ok := filters[name]; ok { if level, ok := filters[name]; ok {
switch level { switch level {
...@@ -230,6 +238,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) { ...@@ -230,6 +238,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
fileHandler.Init() fileHandler.Init()
loggersToClose = append(loggersToClose, fileHandler) loggersToClose = append(loggersToClose, fileHandler)
loggersToReload = append(loggersToReload, fileHandler)
handler = fileHandler handler = fileHandler
case "syslog": case "syslog":
sysLogHandler := NewSyslog(sec, format) sysLogHandler := NewSyslog(sec, format)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment