-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #896 from signal18/log-management
Write stacktrace when panic occured
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"runtime/debug" | ||
"time" | ||
|
||
"github.com/signal18/replication-manager/config" | ||
"github.com/signal18/replication-manager/utils/s18log" | ||
"github.com/sirupsen/logrus" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// State Levels | ||
const ( | ||
StateWarn = "WARNING" | ||
StateErr = "ERROR" | ||
) | ||
|
||
/* | ||
This function is for printing log based on module log level | ||
set forcingLog = true if you want to force print | ||
*/ | ||
func (repman *ReplicationManager) LogModulePrintf(forcingLog bool, module int, level string, format string, args ...interface{}) int { | ||
line := 0 | ||
stamp := fmt.Sprint(time.Now().Format("2006/01/02 15:04:05")) | ||
padright := func(str, pad string, lenght int) string { | ||
for { | ||
str += pad | ||
if len(str) > lenght { | ||
return str[0:lenght] | ||
} | ||
} | ||
} | ||
|
||
tag := config.GetTagsForLog(module) | ||
cliformat := format | ||
format = "[monitor] [" + tag + "] " + padright(level, " ", 5) + " - " + format | ||
|
||
eligible := repman.Conf.IsEligibleForPrinting(module, level) | ||
//Write to htlog and tlog | ||
if eligible || forcingLog { | ||
// line = repman.LogPrintf(level, format, args...) | ||
if repman.tlog.Len > 0 { | ||
repman.tlog.Add(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
if repman.Conf.HttpServ { | ||
httpformat := fmt.Sprintf("[%s] %s", tag, cliformat) | ||
msg := s18log.HttpMessage{ | ||
Group: "none", | ||
Level: level, | ||
Timestamp: stamp, | ||
Text: fmt.Sprintf(httpformat, args...), | ||
} | ||
line = repman.Logs.Add(msg) | ||
} | ||
|
||
if repman.Conf.Daemon { | ||
// wrap logrus levels | ||
switch level { | ||
case "ERROR": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "log", "module": tag}).Errorf(cliformat, args...) | ||
case "INFO": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "log", "module": tag}).Infof(cliformat, args...) | ||
case "DEBUG": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "log", "module": tag}).Debugf(cliformat, args...) | ||
case "WARN": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "log", "module": tag}).Warnf(cliformat, args...) | ||
case "TEST": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "test", "channel": "StdOut", "module": tag}).Infof(cliformat, args...) | ||
case "BENCH": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "benchmark", "channel": "StdOut", "module": tag}).Infof(cliformat, args...) | ||
case "ALERT": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "alert", "channel": "StdOut", "module": tag}).Errorf(cliformat, args...) | ||
case "START": | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "alert", "channel": "StdOut", "module": tag}).Warnf(cliformat, args...) | ||
case "STATE": | ||
status := cliformat[0:6] | ||
code := cliformat[7:15] | ||
err := cliformat[18:] | ||
if status == "OPENED" { | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "state", "status": status, "code": code, "channel": "StdOut"}).Warnf(err, args...) | ||
} else { | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "state", "status": status, "code": code, "channel": "StdOut"}).Warnf(err, args...) | ||
} | ||
|
||
default: | ||
repman.Logrus.WithFields(log.Fields{"cluster": "none", "type": "log", "module": tag}).Printf(cliformat, args...) | ||
} | ||
} | ||
} | ||
|
||
return line | ||
} | ||
|
||
func (repman *ReplicationManager) LogPanicToFile(r interface{}) { | ||
repman.Logrus.WithFields(logrus.Fields{ | ||
"panic": r, | ||
"stacktrace": string(debug.Stack()), | ||
}).Error("Application terminated unexpectedly") | ||
} |