Files
nats-server/server/log.go
Tyler Treat 28160f1de2 Remove global logger
gnatsd currently uses a global logger. This can cause some problems
(especially around the config-reload work), but global variables are
also just an anti-pattern in general. The current behavior is
particularly surprising because the global logger is configured through
calls to the Server.

This addresses issue #500 by removing the global logger and making it a
field on Server.
2017-05-31 16:06:31 -05:00

123 lines
3.0 KiB
Go

// Copyright 2012-2017 Apcera Inc. All rights reserved.
package server
import (
"sync/atomic"
"github.com/nats-io/gnatsd/logger"
)
// Logger interface of the NATS Server
type Logger interface {
// Log a notice statement
Noticef(format string, v ...interface{})
// Log a fatal error
Fatalf(format string, v ...interface{})
// Log an error
Errorf(format string, v ...interface{})
// Log a debug statement
Debugf(format string, v ...interface{})
// Log a trace statement
Tracef(format string, v ...interface{})
}
// SetLogger sets the logger of the server
func (s *Server) SetLogger(logger Logger, debugFlag, traceFlag bool) {
if debugFlag {
atomic.StoreInt32(&s.logging.debug, 1)
} else {
atomic.StoreInt32(&s.logging.debug, 0)
}
if traceFlag {
atomic.StoreInt32(&s.logging.trace, 1)
} else {
atomic.StoreInt32(&s.logging.trace, 0)
}
s.logging.Lock()
s.logging.logger = logger
s.logging.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 signaling
// the process to trigger this function.
func (s *Server) ReOpenLogFile() {
// Check to make sure this is a file logger.
s.logging.RLock()
ll := s.logging.logger
s.logging.RUnlock()
if ll == nil {
s.Noticef("File log re-open ignored, no logger")
return
}
if s.opts.LogFile == "" {
s.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)
s.Noticef("File log re-opened")
}
}
// Noticef logs a notice statement
func (s *Server) Noticef(format string, v ...interface{}) {
s.executeLogCall(func(logger Logger, format string, v ...interface{}) {
logger.Noticef(format, v...)
}, format, v...)
}
// Errorf logs an error
func (s *Server) Errorf(format string, v ...interface{}) {
s.executeLogCall(func(logger Logger, format string, v ...interface{}) {
logger.Errorf(format, v...)
}, format, v...)
}
// Fatalf logs a fatal error
func (s *Server) Fatalf(format string, v ...interface{}) {
s.executeLogCall(func(logger Logger, format string, v ...interface{}) {
logger.Fatalf(format, v...)
}, format, v...)
}
// Debugf logs a debug statement
func (s *Server) Debugf(format string, v ...interface{}) {
if atomic.LoadInt32(&s.logging.debug) == 0 {
return
}
s.executeLogCall(func(logger Logger, format string, v ...interface{}) {
logger.Debugf(format, v...)
}, format, v...)
}
// Tracef logs a trace statement
func (s *Server) Tracef(format string, v ...interface{}) {
if atomic.LoadInt32(&s.logging.trace) == 0 {
return
}
s.executeLogCall(func(logger Logger, format string, v ...interface{}) {
logger.Tracef(format, v...)
}, format, v...)
}
func (s *Server) executeLogCall(f func(logger Logger, format string, v ...interface{}), format string, args ...interface{}) {
s.logging.RLock()
defer s.logging.RUnlock()
if s.logging.logger == nil {
return
}
f(s.logging.logger, format, args...)
}