Add new logger for terraform plugin

main
Elie 2021-02-10 14:25:07 +01:00
parent 6997971e40
commit 31a3cf52d1
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
6 changed files with 141 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))
}

127
logger/plugin_logger.go Normal file
View File

@ -0,0 +1,127 @@
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)
}
var levelMap = map[hclog.Level]logrus.Level{
hclog.Trace: logrus.TraceLevel,
hclog.Debug: logrus.DebugLevel,
hclog.Info: logrus.InfoLevel,
hclog.Warn: logrus.WarnLevel,
hclog.Error: logrus.ErrorLevel,
}
func resolveHCLLogLevels(level hclog.Level) logrus.Level {
return levelMap[level]
}
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.logger.Debug(msg, args)
}
func (t TerraformPluginLogger) Info(msg string, args ...interface{}) {
t.logger.Info(msg, args)
}
func (t TerraformPluginLogger) Warn(msg string, args ...interface{}) {
t.logger.Warn(msg, args)
}
func (t TerraformPluginLogger) Error(msg string, args ...interface{}) {
t.logger.Error(msg, args)
}
func (t TerraformPluginLogger) IsTrace() bool {
return t.logger.IsLevelEnabled(logrus.TraceLevel)
}
func (t TerraformPluginLogger) IsDebug() bool {
return t.logger.IsLevelEnabled(logrus.DebugLevel)
}
func (t TerraformPluginLogger) IsInfo() bool {
return t.logger.IsLevelEnabled(logrus.InfoLevel)
}
func (t TerraformPluginLogger) IsWarn() bool {
return t.logger.IsLevelEnabled(logrus.WarnLevel)
}
func (t TerraformPluginLogger) IsError() bool {
return t.logger.IsLevelEnabled(logrus.ErrorLevel)
}
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) {
t.logger.SetLevel(resolveHCLLogLevels(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(resolveHCLLogLevels(level), msg, args)
}
func (t TerraformPluginLogger) ImpliedArgs() []interface{} {
return nil
}
func (t TerraformPluginLogger) Name() string {
return "TerraformPlugin"
}

View File

@ -29,7 +29,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 {