mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
logging: log to file
New env var CTOP_DEBUG_FILE to specify a path to log file
This commit is contained in:
@@ -29,6 +29,7 @@ type statusMsg struct {
|
||||
type CTopLogger struct {
|
||||
*logging.Logger
|
||||
backend *logging.MemoryBackend
|
||||
logFile *os.File
|
||||
sLog []statusMsg
|
||||
}
|
||||
|
||||
@@ -58,6 +59,7 @@ func Init() *CTopLogger {
|
||||
Log = &CTopLogger{
|
||||
logging.MustGetLogger("ctop"),
|
||||
logging.NewMemoryBackend(size),
|
||||
nil,
|
||||
[]statusMsg{},
|
||||
}
|
||||
|
||||
@@ -68,7 +70,22 @@ func Init() *CTopLogger {
|
||||
backendLvl := logging.AddModuleLevel(Log.backend)
|
||||
backendLvl.SetLevel(level, "")
|
||||
|
||||
logging.SetBackend(backendLvl)
|
||||
logFilePath := debugModeFile()
|
||||
if logFilePath == "" {
|
||||
logging.SetBackend(backendLvl)
|
||||
} else {
|
||||
logFile, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
logging.SetBackend(backendLvl)
|
||||
Log.Error("Unable to create log file: %s", err.Error())
|
||||
} else {
|
||||
backendFile := logging.NewLogBackend(logFile, "", 0)
|
||||
backendFileLvl := logging.AddModuleLevel(backendFile)
|
||||
backendFileLvl.SetLevel(level, "")
|
||||
logging.SetBackend(backendLvl, backendFileLvl)
|
||||
Log.logFile = logFile
|
||||
}
|
||||
}
|
||||
|
||||
if debugMode {
|
||||
StartServer()
|
||||
@@ -105,8 +122,12 @@ func (log *CTopLogger) tail() chan string {
|
||||
|
||||
func (log *CTopLogger) Exit() {
|
||||
exited = true
|
||||
if log.logFile != nil {
|
||||
_ = log.logFile.Close()
|
||||
}
|
||||
StopServer()
|
||||
}
|
||||
|
||||
func debugMode() bool { return os.Getenv("CTOP_DEBUG") == "1" }
|
||||
func debugModeTCP() bool { return os.Getenv("CTOP_DEBUG_TCP") == "1" }
|
||||
func debugMode() bool { return os.Getenv("CTOP_DEBUG") == "1" }
|
||||
func debugModeTCP() bool { return os.Getenv("CTOP_DEBUG_TCP") == "1" }
|
||||
func debugModeFile() string { return os.Getenv("CTOP_DEBUG_FILE") }
|
||||
|
||||
Reference in New Issue
Block a user