forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
70 lines (55 loc) · 1.26 KB
/
trace.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package trace
import (
"github.com/cloudfoundry/gofileutils/fileutils"
"io"
"log"
"os"
)
const CF_TRACE = "CF_TRACE"
type Printer interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}
type nullLogger struct{}
func (*nullLogger) Print(v ...interface{}) {}
func (*nullLogger) Printf(format string, v ...interface{}) {}
func (*nullLogger) Println(v ...interface{}) {}
var stdOut io.Writer = os.Stdout
var Logger Printer
func init() {
Logger = NewLogger("")
}
func EnableTrace() {
Logger = newStdoutLogger()
}
func DisableTrace() {
Logger = new(nullLogger)
}
func SetStdout(s io.Writer) {
stdOut = s
}
func NewLogger(cf_trace string) Printer {
switch cf_trace {
case "", "false":
Logger = new(nullLogger)
case "true":
Logger = newStdoutLogger()
default:
Logger = newFileLogger(cf_trace)
}
return Logger
}
func newStdoutLogger() Printer {
return log.New(stdOut, "", 0)
}
func newFileLogger(path string) Printer {
file, err := fileutils.Open(path)
if err != nil {
logger := newStdoutLogger()
logger.Printf(T("CF_TRACE ERROR CREATING LOG FILE {{.Path}}:\n{{.Err}}",
map[string]interface{}{"Path": path, "Err": err}))
return logger
}
return log.New(file, "", 0)
}