mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
commit
68d6da5c61
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
ctop
|
ctop
|
||||||
.idea
|
.idea
|
||||||
/vendor/
|
/vendor/
|
||||||
|
*.log
|
@ -54,3 +54,18 @@ CTOP_DEBUG=1 CTOP_DEBUG_TCP=1 ./ctop
|
|||||||
```
|
```
|
||||||
|
|
||||||
A TCP listener for streaming log messages will be started on the default listen address(`0.0.0.0:9000`)
|
A TCP listener for streaming log messages will be started on the default listen address(`0.0.0.0:9000`)
|
||||||
|
|
||||||
|
## Log to file
|
||||||
|
|
||||||
|
You can also log to a file by specifying `CTOP_DEBUG_FILE=/path/to/ctop.log` environment variable:
|
||||||
|
```sh
|
||||||
|
CTOP_DEBUG=1 CTOP_DEBUG_FILE=ctop.log ./ctop
|
||||||
|
```
|
||||||
|
|
||||||
|
This is useful for GoLand to see logs right in debug panel:
|
||||||
|
* Edit Run configuration
|
||||||
|
* Go to Logs tab
|
||||||
|
* Specify this log file in "Log file to be shown in console".
|
||||||
|
Then during debugging you'll see the log tab in debug panel:
|
||||||
|
|
||||||
|
![Debug in GoLand](img/goland_debug.png)
|
||||||
|
BIN
_docs/img/goland_debug.png
Normal file
BIN
_docs/img/goland_debug.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 143 KiB |
@ -29,6 +29,7 @@ type statusMsg struct {
|
|||||||
type CTopLogger struct {
|
type CTopLogger struct {
|
||||||
*logging.Logger
|
*logging.Logger
|
||||||
backend *logging.MemoryBackend
|
backend *logging.MemoryBackend
|
||||||
|
logFile *os.File
|
||||||
sLog []statusMsg
|
sLog []statusMsg
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,18 +59,37 @@ func Init() *CTopLogger {
|
|||||||
Log = &CTopLogger{
|
Log = &CTopLogger{
|
||||||
logging.MustGetLogger("ctop"),
|
logging.MustGetLogger("ctop"),
|
||||||
logging.NewMemoryBackend(size),
|
logging.NewMemoryBackend(size),
|
||||||
|
nil,
|
||||||
[]statusMsg{},
|
[]statusMsg{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if debugMode() {
|
debugMode := debugMode()
|
||||||
|
if debugMode {
|
||||||
level = logging.DEBUG
|
level = logging.DEBUG
|
||||||
StartServer()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
backendLvl := logging.AddModuleLevel(Log.backend)
|
backendLvl := logging.AddModuleLevel(Log.backend)
|
||||||
backendLvl.SetLevel(level, "")
|
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()
|
||||||
|
}
|
||||||
Log.Notice("logger initialized")
|
Log.Notice("logger initialized")
|
||||||
}
|
}
|
||||||
return Log
|
return Log
|
||||||
@ -102,8 +122,12 @@ func (log *CTopLogger) tail() chan string {
|
|||||||
|
|
||||||
func (log *CTopLogger) Exit() {
|
func (log *CTopLogger) Exit() {
|
||||||
exited = true
|
exited = true
|
||||||
|
if log.logFile != nil {
|
||||||
|
_ = log.logFile.Close()
|
||||||
|
}
|
||||||
StopServer()
|
StopServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func debugMode() bool { return os.Getenv("CTOP_DEBUG") == "1" }
|
func debugMode() bool { return os.Getenv("CTOP_DEBUG") == "1" }
|
||||||
func debugModeTCP() bool { return os.Getenv("CTOP_DEBUG_TCP") == "1" }
|
func debugModeTCP() bool { return os.Getenv("CTOP_DEBUG_TCP") == "1" }
|
||||||
|
func debugModeFile() string { return os.Getenv("CTOP_DEBUG_FILE") }
|
||||||
|
Loading…
Reference in New Issue
Block a user