Skip to content

Commit

Permalink
Support to reopen file based logs for rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed Nov 22, 2016
1 parent cda285a commit 1c32f28
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
27 changes: 26 additions & 1 deletion server/log.go
@@ -1,10 +1,12 @@
// Copyright 2012-2015 Apcera Inc. All rights reserved.
// Copyright 2012-2016 Apcera Inc. All rights reserved.

package server

import (
"sync"
"sync/atomic"

"github.com/nats-io/gnatsd/logger"
)

// Package globals for performance checks
Expand Down Expand Up @@ -53,6 +55,29 @@ func (s *Server) SetLogger(logger Logger, debugFlag, traceFlag bool) {
log.Unlock()
}

// If the logger is a file based logger, close and re-open the file.
// This allows for file rotation by 'mv'ing the file then signalling
// the process to trigger this function.
func (s *Server) ReOpenLogFile() {
// Check to make sure this is a file logger.
log.Lock()
ll := log.logger
log.Unlock()

if ll == nil {
Noticef("File log re-open ignored, no logger")
return
}
if s.opts.LogFile == "" {
Noticef("File log re-open ignored, not a file logger")
} else {
fileLog := logger.NewFileLogger(s.opts.LogFile,
s.opts.Logtime, s.opts.Debug, s.opts.Trace, true)
s.SetLogger(fileLog, s.opts.Debug, s.opts.Trace)
Noticef("File log re-opened")
}
}

// Noticef logs a notice statement
func Noticef(format string, v ...interface{}) {
executeLogCall(func(logger Logger, format string, v ...interface{}) {
Expand Down
18 changes: 13 additions & 5 deletions server/server.go
Expand Up @@ -15,6 +15,7 @@ import (
"runtime"
"strconv"
"sync"
"syscall"
"time"

// Allow dynamic profiling.
Expand Down Expand Up @@ -187,13 +188,20 @@ func (s *Server) handleSignals() {
return
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

signal.Notify(c, syscall.SIGINT, syscall.SIGUSR1)

go func() {
for sig := range c {
Debugf("Trapped Signal; %v", sig)
// FIXME, trip running?
Noticef("Server Exiting..")
os.Exit(0)
Debugf("Trapped %q signal", sig)
switch sig {
case syscall.SIGINT:
Noticef("Server Exiting..")
os.Exit(0)
case syscall.SIGUSR1:
// File log re-open for rotating file logs.
s.ReOpenLogFile()
}
}
}()
}
Expand Down

0 comments on commit 1c32f28

Please sign in to comment.