Skip to content

Commit

Permalink
Merge pull request #42 from silenceper/f-log-level
Browse files Browse the repository at this point in the history
add log level support
  • Loading branch information
silenceper authored Dec 3, 2021
2 parents f7b7964 + 193ac76 commit c5a3351
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
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

0 comments on commit c5a3351

Please sign in to comment.