-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_provider.go
46 lines (35 loc) · 1003 Bytes
/
logger_provider.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
package trace
import (
"io"
"os"
"path/filepath"
"strconv"
. "code.cloudfoundry.org/cli/cf/i18n"
)
func NewLogger(writer io.Writer, verbose bool, boolsOrPaths ...string) Printer {
LoggingToStdout = verbose
var printers []Printer
stdoutLogger := NewWriterPrinter(writer, true)
for _, path := range boolsOrPaths {
b, err := strconv.ParseBool(path)
LoggingToStdout = LoggingToStdout || b
if path != "" && err != nil {
var file *os.File
err = os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
if err == nil {
file, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
}
if err == nil {
printers = append(printers, NewWriterPrinter(file, false))
} else {
stdoutLogger.Printf(T("CF_TRACE ERROR CREATING LOG FILE {{.Path}}:\n{{.Err}}",
map[string]interface{}{"Path": path, "Err": err}))
LoggingToStdout = true
}
}
}
if LoggingToStdout {
printers = append(printers, stdoutLogger)
}
return CombinePrinters(printers)
}