mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge #3380: Filter logs by log level in TF_LOG
This commit is contained in:
commit
11aa0cda5a
@ -33,7 +33,7 @@ func configDir() (string, error) {
|
|||||||
func homeDir() (string, error) {
|
func homeDir() (string, error) {
|
||||||
// First prefer the HOME environmental variable
|
// First prefer the HOME environmental variable
|
||||||
if home := os.Getenv("HOME"); home != "" {
|
if home := os.Getenv("HOME"); home != "" {
|
||||||
log.Printf("Detected home directory from env var: %s", home)
|
log.Printf("[DEBUG] Detected home directory from env var: %s", home)
|
||||||
return home, nil
|
return home, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
log.go
59
log.go
@ -2,28 +2,65 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/logutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// These are the environmental variables that determine if we log, and if
|
// These are the environmental variables that determine if we log, and if
|
||||||
// we log whether or not the log should go to a file.
|
// we log whether or not the log should go to a file.
|
||||||
const EnvLog = "TF_LOG" //Set to True
|
const (
|
||||||
const EnvLogFile = "TF_LOG_PATH" //Set to a file
|
EnvLog = "TF_LOG" // Set to True
|
||||||
|
EnvLogFile = "TF_LOG_PATH" // Set to a file
|
||||||
|
)
|
||||||
|
|
||||||
// logOutput determines where we should send logs (if anywhere).
|
var validLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
|
||||||
|
|
||||||
|
// logOutput determines where we should send logs (if anywhere) and the log level.
|
||||||
func logOutput() (logOutput io.Writer, err error) {
|
func logOutput() (logOutput io.Writer, err error) {
|
||||||
logOutput = nil
|
logOutput = nil
|
||||||
if os.Getenv(EnvLog) != "" {
|
envLevel := os.Getenv(EnvLog)
|
||||||
logOutput = os.Stderr
|
if envLevel == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if logPath := os.Getenv(EnvLogFile); logPath != "" {
|
logOutput = os.Stderr
|
||||||
var err error
|
if logPath := os.Getenv(EnvLogFile); logPath != "" {
|
||||||
logOutput, err = os.Create(logPath)
|
var err error
|
||||||
if err != nil {
|
logOutput, err = os.Create(logPath)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This was the default since the beginning
|
||||||
|
logLevel := logutils.LogLevel("TRACE")
|
||||||
|
|
||||||
|
if isValidLogLevel(envLevel) {
|
||||||
|
// allow following for better ux: info, Info or INFO
|
||||||
|
logLevel = logutils.LogLevel(strings.ToUpper(envLevel))
|
||||||
|
} else {
|
||||||
|
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
|
||||||
|
envLevel, validLevels)
|
||||||
|
}
|
||||||
|
|
||||||
|
logOutput = &logutils.LevelFilter{
|
||||||
|
Levels: validLevels,
|
||||||
|
MinLevel: logLevel,
|
||||||
|
Writer: logOutput,
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValidLogLevel(level string) bool {
|
||||||
|
for _, l := range validLevels {
|
||||||
|
if strings.ToUpper(level) == string(l) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -88,7 +88,7 @@ func CleanupClients() {
|
|||||||
}(client)
|
}(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("waiting for all plugin processes to complete...")
|
log.Println("[DEBUG] waiting for all plugin processes to complete...")
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -326,7 +326,7 @@ func (c *Client) logStderr(r io.Reader) {
|
|||||||
c.config.Stderr.Write([]byte(line))
|
c.config.Stderr.Write([]byte(line))
|
||||||
|
|
||||||
line = strings.TrimRightFunc(line, unicode.IsSpace)
|
line = strings.TrimRightFunc(line, unicode.IsSpace)
|
||||||
log.Printf("%s: %s", filepath.Base(c.config.Cmd.Path), line)
|
log.Printf("[DEBUG] %s: %s", filepath.Base(c.config.Cmd.Path), line)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
|
@ -513,7 +513,7 @@ func (c *Context) releaseRun(ch chan<- struct{}) {
|
|||||||
func (c *Context) walk(
|
func (c *Context) walk(
|
||||||
graph *Graph, operation walkOperation) (*ContextGraphWalker, error) {
|
graph *Graph, operation walkOperation) (*ContextGraphWalker, error) {
|
||||||
// Walk the graph
|
// Walk the graph
|
||||||
log.Printf("[INFO] Starting graph walk: %s", operation.String())
|
log.Printf("[DEBUG] Starting graph walk: %s", operation.String())
|
||||||
walker := &ContextGraphWalker{Context: c, Operation: operation}
|
walker := &ContextGraphWalker{Context: c, Operation: operation}
|
||||||
return walker, graph.Walk(walker)
|
return walker, graph.Walk(walker)
|
||||||
}
|
}
|
||||||
|
@ -49,9 +49,12 @@ func (n *EvalValidateCount) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RETURN:
|
RETURN:
|
||||||
return nil, &EvalValidateError{
|
if len(errs) != 0 {
|
||||||
Errors: errs,
|
err = &EvalValidateError{
|
||||||
|
Errors: errs,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EvalValidateProvider is an EvalNode implementation that validates
|
// EvalValidateProvider is an EvalNode implementation that validates
|
||||||
|
Loading…
Reference in New Issue
Block a user