Skip to content

Commit

Permalink
Merge pull request #896 from signal18/log-management
Browse files Browse the repository at this point in the history
Write stacktrace when panic occured
  • Loading branch information
caffeinated92 authored Oct 3, 2024
2 parents 3927f02 + c628c7c commit 69e996b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,13 @@ func (repman *ReplicationManager) GenerateKeygen() error {
func (repman *ReplicationManager) Run() error {
var err error

// Defer to recover and log panics
defer func() {
if r := recover(); r != nil {
repman.LogPanicToFile(r)
}
}()

repman.Version = Version
repman.Fullversion = FullVersion
repman.Arch = GoArch
Expand Down
102 changes: 102 additions & 0 deletions server/server_log.go
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")
}

0 comments on commit 69e996b

Please sign in to comment.