mirror of
https://github.com/grafana/grafana.git
synced 2024-11-28 03:34:15 -06:00
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
This commit is contained in:
parent
dac2c62545
commit
a6aa219895
@ -96,13 +96,17 @@ func main() {
|
||||
|
||||
func listenToSystemSignals(server *GrafanaServerImpl) {
|
||||
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)
|
||||
|
||||
select {
|
||||
case sig := <-signalChan:
|
||||
server.Shutdown(fmt.Sprintf("System signal: %s", sig))
|
||||
for {
|
||||
select {
|
||||
case _ = <-sighupChan:
|
||||
log.Reload()
|
||||
case sig := <-signalChan:
|
||||
server.Shutdown(fmt.Sprintf("System signal: %s", sig))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -236,3 +236,20 @@ func (w *FileLogWriter) Close() {
|
||||
func (w *FileLogWriter) Flush() {
|
||||
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
|
||||
type DisposableHandler interface {
|
||||
Close()
|
||||
}
|
||||
|
||||
type ReloadableHandler interface {
|
||||
Reload()
|
||||
}
|
||||
|
@ -21,10 +21,12 @@ import (
|
||||
|
||||
var Root log15.Logger
|
||||
var loggersToClose []DisposableHandler
|
||||
var loggersToReload []ReloadableHandler
|
||||
var filters map[string]log15.Lvl
|
||||
|
||||
func init() {
|
||||
loggersToClose = make([]DisposableHandler, 0)
|
||||
loggersToReload = make([]ReloadableHandler, 0)
|
||||
Root = log15.Root()
|
||||
Root.SetHandler(log15.DiscardHandler())
|
||||
}
|
||||
@ -115,6 +117,12 @@ func Close() {
|
||||
loggersToClose = make([]DisposableHandler, 0)
|
||||
}
|
||||
|
||||
func Reload() {
|
||||
for _, logger := range loggersToReload {
|
||||
logger.Reload()
|
||||
}
|
||||
}
|
||||
|
||||
func GetLogLevelFor(name string) Lvl {
|
||||
if level, ok := filters[name]; ok {
|
||||
switch level {
|
||||
@ -230,6 +238,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
|
||||
fileHandler.Init()
|
||||
|
||||
loggersToClose = append(loggersToClose, fileHandler)
|
||||
loggersToReload = append(loggersToReload, fileHandler)
|
||||
handler = fileHandler
|
||||
case "syslog":
|
||||
sysLogHandler := NewSyslog(sec, format)
|
||||
|
Loading…
Reference in New Issue
Block a user