add cli for debug

This commit is contained in:
pakintada@gmail.com 2023-09-24 18:21:11 +07:00
parent 05fb58e438
commit 899b888d82
5 changed files with 61 additions and 36 deletions

View file

@ -0,0 +1,44 @@
package cli
import (
"bufio"
"os"
"recipe-manager/services/logger"
"strings"
"go.uber.org/zap"
)
var (
log_inst = logger.GetInstance()
disable_cli = false
debug = logger.GetDbgState()
)
func CommandLineListener() {
debug = logger.GetDbgState()
reader := bufio.NewReader(os.Stdin)
for !disable_cli {
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
switch input {
case "debug":
logger.EnableDebug(!logger.GetDbgState())
debug = logger.GetDbgState()
// log_inst.Info("Debug mode enable from cli", zap.Bool("enable", logger.GetDbgState()))
case "ctl":
if debug {
log_inst.Debug("CMD > ", zap.String("CMD", input))
}
default:
if debug {
// log_inst.Debug("CMD > ", zap.String("CMD", input))
// Add functions here!
} else {
log_inst.Error("INVALID CMD or CMD DISABLED", zap.String("CMD", input))
}
}
}
}

View file

@ -22,11 +22,14 @@ var (
LocalTime: true,
})
json_enc = zapcore.NewJSONEncoder(zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "error",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
})
console_enc = zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
TimeKey: "timestamp",
@ -66,4 +69,9 @@ func GetInstance() *zap.Logger {
func EnableDebug(state bool) {
enable_debug = state
log_inst.Debug("EnableDebug", zap.Bool("enable", state))
}
func GetDbgState() bool {
return enable_debug
}

View file

@ -1,6 +0,0 @@
{"level":"info","timestamp":"2023-09-22T10:58:35.135+0700","addr":":8080"}
{"level":"info","timestamp":"2023-09-22T10:58:47.594+0700","user":"pakin tadatanasakul"}
{"level":"info","timestamp":"2023-09-22T10:59:55.661+0700","user":"pakin tadatanasakul"}
{"level":"info","timestamp":"2023-09-22T10:59:59.995+0700","user":"pakin tadatanasakul"}
{"level":"info","timestamp":"2023-09-22T11:00:00.056+0700","user":"pakin tadatanasakul"}
{"level":"info","timestamp":"2023-09-22T11:00:07.558+0700","user":"pakin tadatanasakul"}