forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
41 lines (36 loc) · 950 Bytes
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package goss
import (
"fmt"
"io"
"log"
"os"
"strings"
"time"
"github.com/goss-org/goss/util"
"github.com/hashicorp/logutils"
)
func setLogLevel(c *util.Config) error {
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"},
MinLevel: logutils.LogLevel("INFO"),
Writer: os.Stderr,
}
log.SetFlags(0) // Turn off standard timestamp flags
log.SetOutput(×tampedWriter{filter})
for _, lvl := range filter.Levels {
cLvl := strings.ToUpper(c.LogLevel)
if string(lvl) == cLvl {
filter.MinLevel = lvl
log.Printf("[DEBUG] Setting log level to %v", cLvl)
return nil
}
}
return fmt.Errorf("Unsupported log level: %s", c.LogLevel)
}
type timestampedWriter struct {
wrappedWriter io.Writer
}
func (t *timestampedWriter) Write(b []byte) (int, error) {
timestamp := time.Now().UTC().Format(time.RFC3339)
return fmt.Fprintf(t.wrappedWriter, "%s %s", timestamp, b)
}