Notice and Logger messages

This commit is contained in:
Máximo Cuadros Ortiz
2014-10-16 01:16:21 +02:00
parent d99c6aeead
commit 7c7578ae38
11 changed files with 73 additions and 54 deletions

View File

@@ -11,7 +11,8 @@ type Logger struct {
logger *log.Logger
debug bool
trace bool
logLabel string
infoLabel string
errorLabel string
fatalLabel string
debugLabel string
traceLabel string
@@ -61,22 +62,28 @@ func NewFileLogger(filename string, time, debug, trace bool) *Logger {
}
func setPlainLabelFormats(l *Logger) {
l.logLabel = "[LOG] "
l.debugLabel = "[DBG] "
l.fatalLabel = "[ERR] "
l.traceLabel = "[TRA] "
l.infoLabel = "[INFO] "
l.debugLabel = "[DEBUG] "
l.errorLabel = "[ERROR] "
l.fatalLabel = "[FATAL] "
l.traceLabel = "[TRACE] "
}
func setColoredLabelFormats(l *Logger) {
colorFormat := "[\x1b[%dm%s\x1b[0m] "
l.logLabel = fmt.Sprintf(colorFormat, 32, "LOG")
l.debugLabel = fmt.Sprintf(colorFormat, 36, "DBG")
l.fatalLabel = fmt.Sprintf(colorFormat, 31, "ERR")
l.traceLabel = fmt.Sprintf(colorFormat, 33, "TRA")
l.infoLabel = fmt.Sprintf(colorFormat, 32, "INFO")
l.debugLabel = fmt.Sprintf(colorFormat, 36, "DEBUG")
l.errorLabel = fmt.Sprintf(colorFormat, 31, "ERROR")
l.fatalLabel = fmt.Sprintf(colorFormat, 35, "FATAL")
l.traceLabel = fmt.Sprintf(colorFormat, 33, "TRACE")
}
func (l *Logger) Log(format string, v ...interface{}) {
l.logger.Printf(l.logLabel+format, v...)
func (l *Logger) Notice(format string, v ...interface{}) {
l.logger.Printf(l.infoLabel+format, v...)
}
func (l *Logger) Error(format string, v ...interface{}) {
l.logger.Printf(l.errorLabel+format, v...)
}
func (l *Logger) Fatal(format string, v ...interface{}) {

View File

@@ -43,25 +43,25 @@ func TestStdLoggerWithDebugTraceAndTime(t *testing.T) {
}
}
func TestStdLoggerLog(t *testing.T) {
func TestStdLoggerNotice(t *testing.T) {
expectOutput(t, func() {
logger := NewStdLogger(false, false, false, false)
logger.Log("foo")
}, "[LOG] foo\n")
logger.Notice("foo")
}, "[INFO] foo\n")
}
func TestStdLoggerLogWithColor(t *testing.T) {
func TestStdLoggerNoticeWithColor(t *testing.T) {
expectOutput(t, func() {
logger := NewStdLogger(false, false, false, true)
logger.Log("foo")
}, "[\x1b[32mLOG\x1b[0m] foo\n")
logger.Notice("foo")
}, "[\x1b[32mINFO\x1b[0m] foo\n")
}
func TestStdLoggerDebug(t *testing.T) {
expectOutput(t, func() {
logger := NewStdLogger(false, true, false, false)
logger.Debug("foo %s", "bar")
}, "[DBG] foo bar\n")
}, "[DEBUG] foo bar\n")
}
func TestStdLoggerDebugWithOutDebug(t *testing.T) {
@@ -75,7 +75,7 @@ func TestStdLoggerTrace(t *testing.T) {
expectOutput(t, func() {
logger := NewStdLogger(false, false, true, false)
logger.Trace("foo")
}, "[TRA] foo\n")
}, "[TRACE] foo\n")
}
func TestStdLoggerTraceWithOutDebug(t *testing.T) {
@@ -96,7 +96,7 @@ func TestFileLogger(t *testing.T) {
file.Close()
logger := NewFileLogger(file.Name(), false, false, false)
logger.Log("foo")
logger.Notice("foo")
buf, err := ioutil.ReadFile(file.Name())
if err != nil {
@@ -106,8 +106,8 @@ func TestFileLogger(t *testing.T) {
t.Fatal("Expected a non-zero length logfile")
}
if string(buf) != "[LOG] foo\n" {
t.Fatalf("Expected '%s', received '%s'\n", "[LOG] foo", string(buf))
if string(buf) != "[INFO] foo\n" {
t.Fatalf("Expected '%s', received '%s'\n", "[INFO] foo", string(buf))
}
}

View File

@@ -59,7 +59,7 @@ func getNetworkAndAddr(fqn string) (network, addr string) {
return
}
func (l *SysLogger) Log(format string, v ...interface{}) {
func (l *SysLogger) Notice(format string, v ...interface{}) {
l.writer.Notice(fmt.Sprintf(format, v...))
}
@@ -67,6 +67,10 @@ func (l *SysLogger) Fatal(format string, v ...interface{}) {
l.writer.Crit(fmt.Sprintf(format, v...))
}
func (l *SysLogger) Error(format string, v ...interface{}) {
l.writer.Err(fmt.Sprintf(format, v...))
}
func (l *SysLogger) Debug(format string, v ...interface{}) {
if l.debug {
l.writer.Debug(fmt.Sprintf(format, v...))
@@ -75,6 +79,6 @@ func (l *SysLogger) Debug(format string, v ...interface{}) {
func (l *SysLogger) Trace(format string, v ...interface{}) {
if l.trace {
l.writer.Info(fmt.Sprintf(format, v...))
l.writer.Notice(fmt.Sprintf(format, v...))
}
}

View File

@@ -49,12 +49,12 @@ func TestRemoteSysLogger(t *testing.T) {
}
}
func TestRemoteSysLoggerLog(t *testing.T) {
func TestRemoteSysLoggerNotice(t *testing.T) {
done := make(chan string)
startServer(done)
logger := NewRemoteSysLogger(serverFQN, true, true)
logger.Log("foo %s", "bar")
logger.Notice("foo %s", "bar")
expectSyslogOutput(t, <-done, "foo bar\n")
}

View File

@@ -144,7 +144,7 @@ func (c *client) readLoop() {
return
}
if err := c.parse(b[:n]); err != nil {
Log("Error reading from client: %s", err.Error(), c)
Error("Error reading from client: %s", err.Error(), c)
// Auth was handled inline
if err != ErrAuthorization {
c.sendErr("Parser Error")
@@ -236,7 +236,7 @@ func (c *client) processInfo(arg []byte) error {
}
func (c *client) processErr(errStr string) {
Log("Client error %s", errStr, c)
Error("Client error %s", errStr, c)
c.closeConnection()
}
@@ -628,7 +628,7 @@ writeErr:
client.mu.Unlock()
if ne, ok := err.(net.Error); ok && ne.Timeout() {
Log("Slow Consumer Detected", c)
Notice("Slow Consumer Detected", c)
client.closeConnection()
} else {
Debug("Error writing msg: %v", err, c)

View File

@@ -16,8 +16,9 @@ var log = struct {
}{}
type Logger interface {
Log(format string, v ...interface{})
Notice(format string, v ...interface{})
Fatal(format string, v ...interface{})
Error(format string, v ...interface{})
Debug(format string, v ...interface{})
Trace(format string, v ...interface{})
}
@@ -36,9 +37,15 @@ func (s *Server) SetLogger(logger Logger, d, t bool) {
log.logger = logger
}
func Log(format string, v ...interface{}) {
func Notice(format string, v ...interface{}) {
executeLogCall(func(logger Logger, format string, v ...interface{}) {
logger.Log(format, v...)
logger.Notice(format, v...)
}, format, v...)
}
func Error(format string, v ...interface{}) {
executeLogCall(func(logger Logger, format string, v ...interface{}) {
logger.Error(format, v...)
}, format, v...)
}

View File

@@ -24,7 +24,8 @@ func TestSetLogger(t *testing.T) {
type DummyLogger struct{}
func (l *DummyLogger) Log(format string, v ...interface{}) {}
func (l *DummyLogger) Fatal(format string, v ...interface{}) {}
func (l *DummyLogger) Debug(format string, v ...interface{}) {}
func (l *DummyLogger) Trace(format string, v ...interface{}) {}
func (l *DummyLogger) Notice(format string, v ...interface{}) {}
func (l *DummyLogger) Error(format string, v ...interface{}) {}
func (l *DummyLogger) Fatal(format string, v ...interface{}) {}
func (l *DummyLogger) Debug(format string, v ...interface{}) {}
func (l *DummyLogger) Trace(format string, v ...interface{}) {}

View File

@@ -88,7 +88,7 @@ func (s *Server) HandleConnz(w http.ResponseWriter, r *http.Request) {
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
Log("Error marshalling response to /connz request: %v", err)
Error("Error marshalling response to /connz request: %v", err)
}
w.Write(b)
}
@@ -114,7 +114,7 @@ func (s *Server) HandleSubsz(w http.ResponseWriter, r *http.Request) {
b, err := json.MarshalIndent(st, "", " ")
if err != nil {
Log("Error marshalling response to /subscriptionsz request: %v", err)
Error("Error marshalling response to /subscriptionsz request: %v", err)
}
w.Write(b)
}
@@ -161,7 +161,7 @@ func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
Log("Error marshalling response to /varz request: %v", err)
Error("Error marshalling response to /varz request: %v", err)
}
w.Write(b)
}

View File

@@ -225,7 +225,7 @@ func RemoveSelfReference(clusterPort int, routes []*url.URL) ([]*url.URL, error)
}
if cport == port && isIpInList(selfIPs, getUrlIp(host)) {
Log("Self referencing IP found: ", r)
Notice("Self referencing IP found: ", r)
continue
}
cleanRoutes = append(cleanRoutes, r)
@@ -256,7 +256,7 @@ func getUrlIp(ipStr string) []net.IP {
hostAddr, err := net.LookupHost(ipStr)
if err != nil {
Log("Error looking up host with route hostname: ", err)
Error("Error looking up host with route hostname: ", err)
return ipList
}
for _, addr := range hostAddr {
@@ -273,7 +273,7 @@ func getInterfaceIPs() []net.IP {
interfaceAddr, err := net.InterfaceAddrs()
if err != nil {
Log("Error getting self referencing address: ", err)
Error("Error getting self referencing address: ", err)
return localIPs
}
@@ -282,7 +282,7 @@ func getInterfaceIPs() []net.IP {
if net.ParseIP(interfaceIP.String()) != nil {
localIPs = append(localIPs, interfaceIP)
} else {
Log("Error parsing self referencing address: ", err)
Error("Error parsing self referencing address: ", err)
}
}
return localIPs

View File

@@ -46,7 +46,7 @@ func (c *client) sendConnect() {
}
b, err := json.Marshal(cinfo)
if err != nil {
Log("Error marshalling CONNECT to route: %v\n", err)
Error("Error marshalling CONNECT to route: %v\n", err)
c.closeConnection()
}
c.bw.WriteString(fmt.Sprintf(conProto, b))
@@ -234,7 +234,7 @@ func (s *Server) broadcastUnSubscribe(sub *subscription) {
func (s *Server) routeAcceptLoop(ch chan struct{}) {
hp := fmt.Sprintf("%s:%d", s.opts.ClusterHost, s.opts.ClusterPort)
Log("Listening for route connections on %s", hp)
Notice("Listening for route connections on %s", hp)
l, e := net.Listen("tcp", hp)
if e != nil {
Fatal("Error listening on router port: %d - %v", s.opts.Port, e)
@@ -263,7 +263,7 @@ func (s *Server) routeAcceptLoop(ch chan struct{}) {
tmpDelay = ACCEPT_MAX_SLEEP
}
} else if s.isRunning() {
Log("Accept error: %v", err)
Notice("Accept error: %v", err)
}
continue
}

View File

@@ -142,7 +142,7 @@ func (s *Server) handleSignals() {
for sig := range c {
Debug("Trapped Signal; %v", sig)
// FIXME, trip running?
Log("Server Exiting..")
Notice("Server Exiting..")
os.Exit(0)
}
}()
@@ -166,7 +166,7 @@ func (s *Server) logPid() {
// Start up the server, this will block.
// Start via a Go routine if needed.
func (s *Server) Start() {
Log("Starting gnatsd version %s", VERSION)
Notice("Starting gnatsd version %s", VERSION)
s.running = true
// Log the pid to a file
@@ -261,14 +261,14 @@ func (s *Server) Shutdown() {
// AcceptLoop is exported for easier testing.
func (s *Server) AcceptLoop() {
hp := fmt.Sprintf("%s:%d", s.opts.Host, s.opts.Port)
Log("Listening for client connections on %s", hp)
Notice("Listening for client connections on %s", hp)
l, e := net.Listen("tcp", hp)
if e != nil {
Fatal("Error listening on port: %d - %v", s.opts.Port, e)
return
}
Log("gnatsd is ready")
Notice("gnatsd is ready")
// Setup state that can enable shutdown
s.mu.Lock()
@@ -302,20 +302,20 @@ func (s *Server) AcceptLoop() {
tmpDelay = ACCEPT_MAX_SLEEP
}
} else if s.isRunning() {
Log("Accept error: %v", err)
Notice("Accept error: %v", err)
}
continue
}
tmpDelay = ACCEPT_MIN_SLEEP
s.createClient(conn)
}
Log("Server Exiting..")
Notice("Server Exiting..")
s.done <- true
}
// StartProfiler is called to enable dynamic profiling.
func (s *Server) StartProfiler() {
Log("Starting profiling on http port %d", s.opts.ProfPort)
Notice("Starting profiling on http port %d", s.opts.ProfPort)
hp := fmt.Sprintf("%s:%d", s.opts.Host, s.opts.ProfPort)
go func() {
@@ -328,7 +328,7 @@ func (s *Server) StartProfiler() {
// StartHTTPMonitoring will enable the HTTP monitoring port.
func (s *Server) StartHTTPMonitoring() {
Log("Starting http monitor on port %d", s.opts.HTTPPort)
Notice("Starting http monitor on port %d", s.opts.HTTPPort)
hp := fmt.Sprintf("%s:%d", s.opts.Host, s.opts.HTTPPort)