Merge pull request #241 from cloudskiff/plugin_log_improvment

Better log handler for terraform providers
main
Elie 2021-02-11 15:00:58 +01:00 committed by GitHub
commit ffceac4fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 127 additions and 25 deletions

View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/viper"
)
func GetConfig() Config {
func getConfig() Config {
config := Config{
Level: logrus.WarnLevel,

View File

@ -1,7 +1,6 @@
package logger
import (
"io"
"log"
"github.com/sirupsen/logrus"
@ -13,17 +12,15 @@ type Config struct {
ReportCaller bool
}
func Init(loggerConfig Config) {
logrus.SetLevel(loggerConfig.Level)
logrus.SetReportCaller(loggerConfig.ReportCaller)
logrus.SetFormatter(loggerConfig.Formatter)
func Init() {
config := getConfig()
logrus.SetLevel(config.Level)
logrus.SetReportCaller(config.ReportCaller)
logrus.SetFormatter(config.Formatter)
// Libs that use logger (like grpc provider) will log at TRACE level
log.SetOutput(GetTraceWriter())
}
// Get a writer which will log at trace level
func GetTraceWriter() io.Writer {
redirectLogger := logrus.New()
return redirectLogger.WriterLevel(logrus.TraceLevel)
redirectLogger.SetLevel(config.Level)
redirectLogger.SetFormatter(config.Formatter)
log.SetOutput(redirectLogger.WriterLevel(logrus.TraceLevel))
}

113
logger/plugin_logger.go Normal file
View File

@ -0,0 +1,113 @@
package logger
import (
"io"
"log"
"github.com/hashicorp/go-hclog"
"github.com/sirupsen/logrus"
)
type terraformPluginFormatter struct {
logrus.Formatter
}
func (f *terraformPluginFormatter) Format(entry *logrus.Entry) ([]byte, error) {
entry.Message = "[TerraformPlugin] " + entry.Message
return f.Formatter.Format(entry)
}
type TerraformPluginLogger struct {
logger *logrus.Logger
}
func NewTerraformPluginLogger() TerraformPluginLogger {
config := getConfig()
logger := logrus.New()
logger.SetLevel(logrus.ErrorLevel)
logger.SetReportCaller(false)
logger.SetFormatter(&terraformPluginFormatter{Formatter: config.Formatter})
// Disable terraform provider log if we are not in trace level
if config.Level == logrus.TraceLevel {
logger.SetLevel(logrus.TraceLevel)
}
return TerraformPluginLogger{logger}
}
func (t TerraformPluginLogger) Trace(msg string, args ...interface{}) {
t.logger.Trace(msg, args)
}
func (t TerraformPluginLogger) Debug(msg string, args ...interface{}) {
t.Trace(msg, args)
}
func (t TerraformPluginLogger) Info(msg string, args ...interface{}) {
t.Trace(msg, args)
}
func (t TerraformPluginLogger) Warn(msg string, args ...interface{}) {
t.Trace(msg, args)
}
func (t TerraformPluginLogger) Error(msg string, args ...interface{}) {
t.Trace(msg, args)
}
func (t TerraformPluginLogger) IsTrace() bool {
return true
}
func (t TerraformPluginLogger) IsDebug() bool {
return false
}
func (t TerraformPluginLogger) IsInfo() bool {
return false
}
func (t TerraformPluginLogger) IsWarn() bool {
return false
}
func (t TerraformPluginLogger) IsError() bool {
return false
}
func (t TerraformPluginLogger) With(args ...interface{}) hclog.Logger {
return t
}
func (t TerraformPluginLogger) Named(name string) hclog.Logger {
return t
}
func (t TerraformPluginLogger) ResetNamed(name string) hclog.Logger {
return t
}
func (t TerraformPluginLogger) SetLevel(level hclog.Level) {}
func (t TerraformPluginLogger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
stdLogger := log.New(t.logger.Writer(), "", log.Flags())
stdLogger.SetOutput(t.logger.Writer())
return stdLogger
}
func (t TerraformPluginLogger) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
return t.logger.Writer()
}
func (t TerraformPluginLogger) Log(level hclog.Level, msg string, args ...interface{}) {
t.logger.Log(logrus.TraceLevel, msg, args)
}
func (t TerraformPluginLogger) ImpliedArgs() []interface{} {
return nil
}
func (t TerraformPluginLogger) Name() string {
return "TerraformPlugin"
}

View File

@ -30,7 +30,7 @@ func main() {
func run() int {
config.Init()
logger.Init(logger.GetConfig())
logger.Init()
driftctlCmd := cmd.NewDriftctlCmd(build.Build{})

View File

@ -1,31 +1,23 @@
package terraform
import (
"github.com/cloudskiff/driftctl/logger"
"os/exec"
"github.com/cloudskiff/driftctl/logger"
tfplugin "github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/terraform/plugin/discovery"
)
func ClientConfig(m discovery.PluginMeta) *plugin.ClientConfig {
// redirect plugin logger to trace level in logrus
pluginLogger := hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Level: hclog.Trace,
Output: logger.GetTraceWriter(),
})
logger := logger.NewTerraformPluginLogger()
return &plugin.ClientConfig{
Cmd: exec.Command(m.Path),
HandshakeConfig: tfplugin.Handshake,
VersionedPlugins: tfplugin.VersionedPlugins,
Managed: true,
Logger: pluginLogger,
Logger: logger,
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
AutoMTLS: true,
}

View File

@ -268,7 +268,7 @@ func Run(t *testing.T, c AccTestCase) {
}
}()
logger.Init(logger.GetConfig())
logger.Init()
err = c.createResultFile(t)
if err != nil {