-
Notifications
You must be signed in to change notification settings - Fork 443
/
logger.go
64 lines (56 loc) · 997 Bytes
/
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
57
58
59
60
61
62
63
64
package cliutil
import (
"io"
"os"
"path/filepath"
"sync"
"k8s.io/client-go/util/homedir"
)
const (
glooDir = ".gloo"
glooLogs = "debug.log"
)
var (
glooPath string
glooLogsPath string
logger io.Writer
mutex sync.Once
)
func init() {
home := homedir.HomeDir()
glooPath = filepath.Join(home, glooDir)
glooLogsPath = filepath.Join(glooPath, glooLogs)
}
func GetLogsPath() string {
return glooLogsPath
}
func GetLogger() io.Writer {
Initialize()
return logger
}
func Initialize() error {
var initError error
mutex.Do(func() {
if _, err := os.ReadDir(glooPath); err != nil {
if !os.IsNotExist(err) {
initError = err
return
}
err = os.Mkdir(glooPath, 0755)
if err != nil {
initError = err
return
}
}
file, err := os.OpenFile(glooLogsPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
initError = err
return
}
logger = file
})
if initError != nil {
logger = os.Stdout
}
return initError
}