Implement --loglevel option ; Use logger instead of log everywhere

This commit is contained in:
ppom 2023-10-18 12:00:00 +02:00
parent 22ac3764e4
commit 3822e854b6
2 changed files with 55 additions and 18 deletions

View File

@ -6,6 +6,8 @@ import (
"fmt"
"os"
"regexp"
"framagit.org/ppom/reaction/logger"
)
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)
}
func addLevelFlag(f *flag.FlagSet) *string {
return addStringFlag([]string{"l", "loglevel"}, "INFO", f)
}
func subCommandParse(f *flag.FlagSet, maxRemainingArgs int) {
help := addBoolFlag([]string{"h", "help"}, f)
f.Parse(os.Args[2:])
@ -70,6 +76,8 @@ func basicUsage() {
# options:
-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
# (default: /run/reaction/reaction.sock)
@ -102,7 +110,7 @@ var exampleConf string
func Main() {
if len(os.Args) <= 1 {
fmt.Println("No argument provided")
logger.Fatalln("No argument provided")
basicUsage()
os.Exit(1)
} else if os.Args[1] == "-h" || os.Args[1] == "--help" {
@ -121,12 +129,20 @@ func Main() {
case "start":
SocketPath = addSocketFlag(f)
confFilename := addConfFlag(f)
logLevel := addLevelFlag(f)
subCommandParse(f, 0)
if *confFilename == "" {
fmt.Println("no configuration file provided")
logger.Fatalln("no configuration file provided")
basicUsage()
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)
case "show":
@ -135,12 +151,12 @@ func Main() {
limit := addLimitFlag(f)
subCommandParse(f, 0)
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()
os.Exit(1)
}
if *limit != "" {
fmt.Println("for now, -l/--limit is not supported")
logger.Fatalln("for now, -l/--limit is not supported")
os.Exit(1)
}
// f.Arg(0) is "" if there is no remaining argument
@ -152,17 +168,17 @@ func Main() {
limit := addLimitFlag(f)
subCommandParse(f, 1)
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()
os.Exit(1)
}
if f.Arg(0) == "" {
fmt.Println("subcommand flush takes one TARGET argument")
logger.Fatalln("subcommand flush takes one TARGET argument")
basicUsage()
os.Exit(1)
}
if *limit != "" {
fmt.Println("for now, -l/--limit is not supported")
logger.Fatalln("for now, -l/--limit is not supported")
os.Exit(1)
}
ClientFlush(f.Arg(0), *limit, *queryFormat)
@ -171,17 +187,17 @@ func Main() {
// socket not needed, no interaction with the daemon
subCommandParse(f, 2)
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()
os.Exit(1)
}
regex, err := regexp.Compile(f.Arg(0))
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)
}
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)
} else {
@ -189,7 +205,7 @@ func Main() {
}
default:
fmt.Println("subcommand not recognized")
logger.Fatalln("subcommand not recognized")
basicUsage()
os.Exit(1)
}

View File

@ -5,6 +5,7 @@ import "log"
type Level int
const (
UNKNOWN = Level(-1)
DEBUG = Level(1)
INFO = Level(2)
WARN = Level(3)
@ -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
func SetLogLevel(level Level) {
@ -37,13 +55,16 @@ func SetLogLevel(level Level) {
func Println(level Level, args ...any) {
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) {
if level >= LogLevel {
log.Printf(level.String()+format, args)
log.Printf(level.String()+format, args...)
}
}