Log levels support

This commit is contained in:
Sotirios Mantziaris 2024-12-30 21:40:33 +02:00 committed by Manuel Rüger
parent 2ba35118bf
commit f25d8876fc
3 changed files with 76 additions and 21 deletions

View File

@ -794,8 +794,7 @@ GLOBAL OPTIONS:
--minor-edit don't send notifications while updating Confluence page. (default: false) [$MARK_MINOR_EDIT]
--version-message value add a message to the page version, to explain the edit (default: "") [$MARK_VERSION_MESSAGE]
--color value display logs in color. Possible values: auto, never. (default: "auto") [$MARK_COLOR]
--debug enable debug logs. (default: false) [$MARK_DEBUG]
--trace enable trace logs. (default: false) [$MARK_TRACE]
--log-level value set the log level. Possible values: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL. (default: "info") [$MARK_LOG_LEVEL]
--username value, -u value use specified username for updating Confluence page. [$MARK_USERNAME]
--password value, -p value use specified token for updating Confluence page. Specify - as password to read password from stdin, or your Personal access token. Username is not mandatory if personal access token is provided. For more info please see: https://developer.atlassian.com/server/confluence/confluence-server-rest-api/#authentication. [$MARK_PASSWORD]
--target-url value, -l value edit specified Confluence page. If -l is not specified, file should contain metadata (see above). [$MARK_TARGET_URL]

49
main.go
View File

@ -105,17 +105,11 @@ var flags = []cli.Flag{
Usage: "display logs in color. Possible values: auto, never.",
EnvVars: []string{"MARK_COLOR"},
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "debug",
Value: false,
Usage: "enable debug logs.",
EnvVars: []string{"MARK_DEBUG"},
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "trace",
Value: false,
Usage: "enable trace logs.",
EnvVars: []string{"MARK_TRACE"},
altsrc.NewStringFlag(&cli.StringFlag{
Name: "log-level",
Value: "info",
Usage: "set the log level. Possible values: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL.",
EnvVars: []string{"MARK_LOG_LEVEL"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "username",
@ -230,13 +224,8 @@ func main() {
}
func RunMark(cCtx *cli.Context) error {
if cCtx.Bool("debug") {
log.SetLevel(lorg.LevelDebug)
}
if cCtx.Bool("trace") {
log.SetLevel(lorg.LevelTrace)
if err := setLogLevel(cCtx); err != nil {
return err
}
if cCtx.String("color") == "never" {
@ -551,7 +540,6 @@ func processFile(
}
func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *metadata.Meta) {
labelInfo, err := api.GetPageLabels(target, "global")
if err != nil {
log.Fatal(err)
@ -619,3 +607,26 @@ func configFilePath() string {
}
return filepath.Join(fp, "mark")
}
func setLogLevel(cCtx *cli.Context) error {
logLevel := cCtx.String("log-level")
switch logLevel {
case "TRACE":
log.SetLevel(lorg.LevelTrace)
case "DEBUG":
log.SetLevel(lorg.LevelDebug)
case "INFO":
log.SetLevel(lorg.LevelInfo)
case "WARNING":
log.SetLevel(lorg.LevelWarning)
case "ERROR":
log.SetLevel(lorg.LevelError)
case "FATAL":
log.SetLevel(lorg.LevelFatal)
default:
return fmt.Errorf("unknown log level: %s", logLevel)
}
log.GetLevel()
return nil
}

45
main_test.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"flag"
"testing"
"github.com/reconquest/pkg/log"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)
func Test_setLogLevel(t *testing.T) {
type args struct {
lvl string
}
tests := map[string]struct {
args args
want log.Level
expectedErr string
}{
"invalid": {args: args{lvl: "INVALID"}, want: log.LevelInfo, expectedErr: "unknown log level: INVALID"},
"empty": {args: args{lvl: ""}, want: log.LevelInfo, expectedErr: "unknown log level: "},
"info": {args: args{lvl: log.LevelInfo.String()}, want: log.LevelInfo},
"debug": {args: args{lvl: log.LevelDebug.String()}, want: log.LevelDebug},
"trace": {args: args{lvl: log.LevelTrace.String()}, want: log.LevelTrace},
"warning": {args: args{lvl: log.LevelWarning.String()}, want: log.LevelWarning},
"error": {args: args{lvl: log.LevelError.String()}, want: log.LevelError},
"fatal": {args: args{lvl: log.LevelFatal.String()}, want: log.LevelFatal},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
set := flag.NewFlagSet("test", flag.ContinueOnError)
set.String("log-level", tt.args.lvl, "")
cliCtx := cli.NewContext(nil, set, nil)
err := setLogLevel(cliCtx)
if tt.expectedErr != "" {
assert.EqualError(t, err, tt.expectedErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, log.GetLevel())
}
})
}
}