Implement --loglevel option ; Use logger instead of log everywhere
This commit is contained in:
parent
22ac3764e4
commit
3822e854b6
38
app/main.go
38
app/main.go
@ -6,6 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"framagit.org/ppom/reaction/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func addStringFlag(names []string, defvalue string, f *flag.FlagSet) *string {
|
func addStringFlag(names []string, defvalue string, f *flag.FlagSet) *string {
|
||||||
@ -42,6 +44,10 @@ func addLimitFlag(f *flag.FlagSet) *string {
|
|||||||
return addStringFlag([]string{"l", "limit"}, "", f)
|
return addStringFlag([]string{"l", "limit"}, "", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addLevelFlag(f *flag.FlagSet) *string {
|
||||||
|
return addStringFlag([]string{"l", "loglevel"}, "INFO", f)
|
||||||
|
}
|
||||||
|
|
||||||
func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) {
|
func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) {
|
||||||
help := addBoolFlag([]string{"h", "help"}, f)
|
help := addBoolFlag([]string{"h", "help"}, f)
|
||||||
f.Parse(os.Args[2:])
|
f.Parse(os.Args[2:])
|
||||||
@ -70,6 +76,8 @@ func basicUsage() {
|
|||||||
|
|
||||||
# options:
|
# options:
|
||||||
-c/--config CONFIG_FILE # configuration file in json, jsonnet or yaml format (required)
|
-c/--config CONFIG_FILE # configuration file in json, jsonnet or yaml format (required)
|
||||||
|
-l/--loglevel LEVEL # minimum log level to show, in DEBUG, INFO, WARN, ERROR, FATAL
|
||||||
|
# (default: INFO)
|
||||||
-s/--socket SOCKET # path to the client-daemon communication socket
|
-s/--socket SOCKET # path to the client-daemon communication socket
|
||||||
# (default: /run/reaction/reaction.sock)
|
# (default: /run/reaction/reaction.sock)
|
||||||
|
|
||||||
@ -102,7 +110,7 @@ var exampleConf string
|
|||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
if len(os.Args) <= 1 {
|
if len(os.Args) <= 1 {
|
||||||
fmt.Println("No argument provided")
|
logger.Fatalln("No argument provided")
|
||||||
basicUsage()
|
basicUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else if os.Args[1] == "-h" || os.Args[1] == "--help" {
|
} else if os.Args[1] == "-h" || os.Args[1] == "--help" {
|
||||||
@ -121,12 +129,20 @@ func Main() {
|
|||||||
case "start":
|
case "start":
|
||||||
SocketPath = addSocketFlag(f)
|
SocketPath = addSocketFlag(f)
|
||||||
confFilename := addConfFlag(f)
|
confFilename := addConfFlag(f)
|
||||||
|
logLevel := addLevelFlag(f)
|
||||||
subCommandParse(f, 0)
|
subCommandParse(f, 0)
|
||||||
if *confFilename == "" {
|
if *confFilename == "" {
|
||||||
fmt.Println("no configuration file provided")
|
logger.Fatalln("no configuration file provided")
|
||||||
basicUsage()
|
basicUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
logLevelType := logger.FromString(*logLevel)
|
||||||
|
if logLevelType == logger.UNKNOWN {
|
||||||
|
logger.Fatalf("Log Level %v not recognized", logLevel)
|
||||||
|
basicUsage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
logger.SetLogLevel(logLevelType)
|
||||||
Daemon(*confFilename)
|
Daemon(*confFilename)
|
||||||
|
|
||||||
case "show":
|
case "show":
|
||||||
@ -135,12 +151,12 @@ func Main() {
|
|||||||
limit := addLimitFlag(f)
|
limit := addLimitFlag(f)
|
||||||
subCommandParse(f, 0)
|
subCommandParse(f, 0)
|
||||||
if *queryFormat != "yaml" && *queryFormat != "json" {
|
if *queryFormat != "yaml" && *queryFormat != "json" {
|
||||||
fmt.Println("only yaml and json formats are supported")
|
logger.Fatalln("only yaml and json formats are supported")
|
||||||
f.PrintDefaults()
|
f.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if *limit != "" {
|
if *limit != "" {
|
||||||
fmt.Println("for now, -l/--limit is not supported")
|
logger.Fatalln("for now, -l/--limit is not supported")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
// f.Arg(0) is "" if there is no remaining argument
|
// f.Arg(0) is "" if there is no remaining argument
|
||||||
@ -152,17 +168,17 @@ func Main() {
|
|||||||
limit := addLimitFlag(f)
|
limit := addLimitFlag(f)
|
||||||
subCommandParse(f, 1)
|
subCommandParse(f, 1)
|
||||||
if *queryFormat != "yaml" && *queryFormat != "json" {
|
if *queryFormat != "yaml" && *queryFormat != "json" {
|
||||||
fmt.Println("only yaml and json formats are supported")
|
logger.Fatalln("only yaml and json formats are supported")
|
||||||
f.PrintDefaults()
|
f.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if f.Arg(0) == "" {
|
if f.Arg(0) == "" {
|
||||||
fmt.Println("subcommand flush takes one TARGET argument")
|
logger.Fatalln("subcommand flush takes one TARGET argument")
|
||||||
basicUsage()
|
basicUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if *limit != "" {
|
if *limit != "" {
|
||||||
fmt.Println("for now, -l/--limit is not supported")
|
logger.Fatalln("for now, -l/--limit is not supported")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
ClientFlush(f.Arg(0), *limit, *queryFormat)
|
ClientFlush(f.Arg(0), *limit, *queryFormat)
|
||||||
@ -171,17 +187,17 @@ func Main() {
|
|||||||
// socket not needed, no interaction with the daemon
|
// socket not needed, no interaction with the daemon
|
||||||
subCommandParse(f, 2)
|
subCommandParse(f, 2)
|
||||||
if f.Arg(0) == "" {
|
if f.Arg(0) == "" {
|
||||||
fmt.Println("subcommand test-regex takes at least one REGEX argument")
|
logger.Fatalln("subcommand test-regex takes at least one REGEX argument")
|
||||||
basicUsage()
|
basicUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
regex, err := regexp.Compile(f.Arg(0))
|
regex, err := regexp.Compile(f.Arg(0))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERROR the specified regex is invalid: %v", err)
|
logger.Fatalln("ERROR the specified regex is invalid: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if f.Arg(1) == "" {
|
if f.Arg(1) == "" {
|
||||||
fmt.Println("INFO no second argument: reading from stdin")
|
logger.Println(logger.INFO, "no second argument: reading from stdin")
|
||||||
|
|
||||||
MatchStdin(regex)
|
MatchStdin(regex)
|
||||||
} else {
|
} else {
|
||||||
@ -189,7 +205,7 @@ func Main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Println("subcommand not recognized")
|
logger.Fatalln("subcommand not recognized")
|
||||||
basicUsage()
|
basicUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,12 @@ import "log"
|
|||||||
type Level int
|
type Level int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DEBUG = Level(1)
|
UNKNOWN = Level(-1)
|
||||||
INFO = Level(2)
|
DEBUG = Level(1)
|
||||||
WARN = Level(3)
|
INFO = Level(2)
|
||||||
ERROR = Level(4)
|
WARN = Level(3)
|
||||||
FATAL = Level(5)
|
ERROR = Level(4)
|
||||||
|
FATAL = Level(5)
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l Level) String() string {
|
func (l Level) String() string {
|
||||||
@ -29,6 +30,23 @@ func (l Level) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FromString(s string) Level {
|
||||||
|
switch s {
|
||||||
|
case "DEBUG":
|
||||||
|
return DEBUG
|
||||||
|
case "INFO":
|
||||||
|
return INFO
|
||||||
|
case "WARN":
|
||||||
|
return WARN
|
||||||
|
case "ERROR":
|
||||||
|
return ERROR
|
||||||
|
case "FATAL":
|
||||||
|
return FATAL
|
||||||
|
default:
|
||||||
|
return UNKNOWN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var LogLevel Level = 2
|
var LogLevel Level = 2
|
||||||
|
|
||||||
func SetLogLevel(level Level) {
|
func SetLogLevel(level Level) {
|
||||||
@ -37,13 +55,16 @@ func SetLogLevel(level Level) {
|
|||||||
|
|
||||||
func Println(level Level, args ...any) {
|
func Println(level Level, args ...any) {
|
||||||
if level >= LogLevel {
|
if level >= LogLevel {
|
||||||
log.Println(level, args)
|
newargs := make([]any, 0)
|
||||||
|
newargs = append(newargs, level)
|
||||||
|
newargs = append(newargs, args...)
|
||||||
|
log.Println(newargs...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Printf(level Level, format string, args ...any) {
|
func Printf(level Level, format string, args ...any) {
|
||||||
if level >= LogLevel {
|
if level >= LogLevel {
|
||||||
log.Printf(level.String()+format, args)
|
log.Printf(level.String()+format, args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user