-
Notifications
You must be signed in to change notification settings - Fork 11
/
log.go
49 lines (41 loc) · 871 Bytes
/
log.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
package gomaxcompute
import (
"fmt"
"io"
"os"
"path"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
func init() {
logDir := getEnv("GOMAXCOMPUTE_log_dir", "")
logLevel := getEnv("GOMAXCOMPUTE_log_level", "info")
ll, e := logrus.ParseLevel(logLevel)
if e != nil {
ll = logrus.InfoLevel
}
var f io.Writer
if logDir != "" {
e = os.MkdirAll(logDir, 0744)
if e != nil {
log.Panicf("create log directory failed: %v", e)
}
f, e = os.Create(path.Join(logDir, fmt.Sprintf("gomaxcompute-%d.log", os.Getpid())))
if e != nil {
log.Panicf("open log file failed: %v", e)
}
} else {
f = os.Stdout
}
lg := logrus.New()
lg.SetOutput(f)
lg.SetLevel(ll)
log = lg.WithFields(logrus.Fields{"driver": "odps"})
}