Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add log level support #42

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 33 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
path "path/filepath"
"runtime"
"strings"

"github.com/silenceper/log"
)

var (
Expand All @@ -19,6 +21,7 @@ var (
cmdArgs string
showVersion bool
showHelp bool
logLevel string

started chan bool
)
Expand All @@ -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")
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
}

Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import "fmt"

const version = "1.5.1"
const version = "1.5.2"

func printVersion() {
fmt.Println(version)
Expand Down