From 193ac764b208dbd73008c0aa4da0cead00992d96 Mon Sep 17 00:00:00 2001 From: zhenlinwen Date: Fri, 3 Dec 2021 21:12:45 +0800 Subject: [PATCH] add log level support --- README.md | 3 +++ config.go | 2 ++ main.go | 33 +++++++++++++++++++++++++++++++++ version.go | 2 +- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 38062ae..cb54955 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Gowatch will watch for file events, and every time you create/modify/delete a fi - -p : Not required, specify the package to be built (can also be a single file) - -args: Not required, specify program runtime parameters, for example: -args = '-host =: 8080, -name = demo' - -v: Not required, display gowatch version information +- -l: Not required, specify the log level, default is debug - -h: Not required, show usage example: @@ -109,6 +110,8 @@ build_tags: "" # Whether to prohibit automatic operation disable_run: false +# log level, support debug, info, warn, error, fatal +log_level: "debug" ``` ## Author diff --git a/config.go b/config.go index 0bd80f9..45083dc 100644 --- a/config.go +++ b/config.go @@ -39,6 +39,8 @@ type config struct { DisableRun bool `yaml:"disable_run"` // commands when build finished to run RunCmd string `yaml:"run_cmd"` + // log level, support: debug, info, warn, error, fatal + LogLevel string `yaml:"log_level"` } func parseConfig() *config { diff --git a/main.go b/main.go index 77c230c..0182f48 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( path "path/filepath" "runtime" "strings" + + "github.com/silenceper/log" ) var ( @@ -19,6 +21,7 @@ var ( cmdArgs string showVersion bool showHelp bool + logLevel string started chan bool ) @@ -28,6 +31,7 @@ func init() { flag.StringVar(&buildPkg, "p", "", "go build packages") flag.StringVar(&cmdArgs, "args", "", "app run args,separated by commas. like: -args='-host=:8080,-name=demo'") flag.BoolVar(&showVersion, "v", false, "show version") + flag.StringVar(&logLevel, "l", "", "log level: debug, info, warn, error, fatal") flag.BoolVar(&showHelp, "h", false, "help") } @@ -89,6 +93,9 @@ build_tags: "" # Whether to prohibit automatic operation disable_run: false + +# log level, support debug, info, warn, error, fatal +log_level: "debug" ` func main() { @@ -147,6 +154,15 @@ func main() { // File suffix to be watched cfg.WatchExts = append(cfg.WatchExts, ".go") + // set log level, default is debug + if cfg.LogLevel != "" { + setLogLevel(cfg.LogLevel) + } + // flags override config + if logLevel != "" { + setLogLevel(logLevel) + } + runApp() } @@ -170,3 +186,20 @@ func runApp() { <-exit runtime.Goexit() } + +func setLogLevel(level string) { + switch level { + case "debug": + log.SetLogLevel(log.LevelDebug) + case "info": + log.SetLogLevel(log.LevelInfo) + case "warn": + log.SetLogLevel(log.LevelWarning) + case "error": + log.SetLogLevel(log.LevelError) + case "fatal": + log.SetLogLevel(log.LevelFatal) + default: + log.SetLogLevel(log.LevelDebug) + } +} diff --git a/version.go b/version.go index 88c6c2e..db70c15 100644 --- a/version.go +++ b/version.go @@ -2,7 +2,7 @@ package main import "fmt" -const version = "1.5.1" +const version = "1.5.2" func printVersion() { fmt.Println(version)