From b693e768222a9924c1768d73298722db4f39a328 Mon Sep 17 00:00:00 2001 From: Don Date: Fri, 11 Oct 2019 11:34:47 -0700 Subject: [PATCH] Add logging for urfave --- go.mod | 5 ++++- go.sum | 9 +++++++++ pkg/urfave/logging.go | 34 ++++++++++++++++++++++++++++++++++ pkg/urfave/urfave.go | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 pkg/urfave/logging.go diff --git a/go.mod b/go.mod index 18c0c22..080aba9 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/drone-plugins/drone-plugin-lib go 1.13 -require github.com/urfave/cli v1.21.0 +require ( + github.com/sirupsen/logrus v1.4.2 // indirect + github.com/urfave/cli v1.21.0 +) diff --git a/go.sum b/go.sum index 6de7d0f..d7bdccd 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,14 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE= github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/urfave/logging.go b/pkg/urfave/logging.go new file mode 100644 index 0000000..ab91492 --- /dev/null +++ b/pkg/urfave/logging.go @@ -0,0 +1,34 @@ +// Copyright (c) 2019, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package urfave + +import ( + "github.com/sirupsen/logrus" + "github.com/urfave/cli" +) + +const logLevelFlag = "log-level" + +func loggingFlags() []cli.Flag { + return []cli.Flag{ + cli.StringFlag{ + Name: logLevelFlag, + Usage: "logging-level", + }, + } +} + +// LoggingFromContext sets the logrus logging level. +func LoggingFromContext(ctx *cli.Context) { + lvl, err := logrus.ParseLevel(ctx.String(logLevelFlag)) + + if err != nil { + lvl = logrus.InfoLevel + } + + logrus.SetLevel(lvl) + logrus.WithField("level", lvl.String()).Info("setup logging") +} diff --git a/pkg/urfave/urfave.go b/pkg/urfave/urfave.go index bc72994..579f456 100644 --- a/pkg/urfave/urfave.go +++ b/pkg/urfave/urfave.go @@ -57,6 +57,7 @@ func PipelineFlags() []cli.Flag { flags = append(flags, stageFlags()...) flags = append(flags, stepFlags()...) flags = append(flags, semVerFlags()...) + flags = append(flags, loggingFlags()...) return flags }