forked from vancluever/terraform-provider-acme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
56 lines (48 loc) · 1.99 KB
/
logger.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"log"
"strings"
legoLog "github.com/go-acme/lego/v4/log"
)
// legoLogger is a proxy for lego log messages. This allows messages
// for lego itself to be printed as actual plugin debug logs.
//
// Note that this log proxy only passes through messages, ignores log
// levels, and does not exit on Fatal or its similar functions. All
// messages are logged at the debug level.
type legoLogger struct{}
// initLegoLogger initializes the logger and sets it up as an
// override for the lego standard logger, which is ignored by
// Terraform. This should be run in main, after lego has had a chance
// to initialize the package singleton.
func initLegoLogger() {
l := &legoLogger{}
legoLog.Logger = l
l.log("Messages from the lego library will show up as DEBUG messages.")
}
func (l *legoLogger) Fatal(args ...interface{}) { l.log(args) }
func (l *legoLogger) Fatalln(args ...interface{}) { l.log(args) }
func (l *legoLogger) Fatalf(format string, args ...interface{}) { l.log(fmt.Sprintf(format, args...)) }
func (l *legoLogger) Print(args ...interface{}) { l.log(args) }
func (l *legoLogger) Println(args ...interface{}) { l.log(args) }
func (l *legoLogger) Printf(format string, args ...interface{}) { l.log(fmt.Sprintf(format, args...)) }
// log logs the raw message sent to it to the Terraform logger with a
// prefix indicating it came from lego.
//
// All messages are logged at the debug level.
func (l *legoLogger) log(args ...interface{}) {
// Strip any lego-based log level from the string. This should
// always be in the first argument.
if len(args) > 0 {
if _, ok := args[0].(string); ok {
switch {
case strings.HasPrefix(args[0].(string), "[INFO] "):
args[0] = strings.TrimPrefix(args[0].(string), "[INFO] ")
case strings.HasPrefix(args[0].(string), "[WARN] "):
args[0] = strings.TrimPrefix(args[0].(string), "[WARN] ")
}
}
}
log.Println(append([]interface{}{"[DEBUG]", "lego:"}, args...)...)
}