-
Notifications
You must be signed in to change notification settings - Fork 44
/
logs.go
64 lines (55 loc) · 1.45 KB
/
logs.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 main
import (
"os"
"github.com/go-zoox/core-utils/fmt"
"github.com/go-zoox/fs"
"github.com/go-zoox/logger"
"github.com/go-zoox/logger/components/transport"
"github.com/go-zoox/logger/transport/file"
)
type Logs struct {
Dir string
Level string
//
accessLogFile *os.File
errorLogFile *os.File
debugLogFile *os.File
}
func (l *Logs) Setup() (err error) {
if ok := fs.IsExist(l.Dir); !ok {
if err := fs.Mkdirp(l.Dir); err != nil {
return fmt.Errorf("failed to create log directory for : %v", err)
}
}
accessLog := fs.JoinPath(l.Dir, "access.log")
if l.accessLogFile, err = fs.Open(accessLog); err != nil {
return fmt.Errorf("failed to open access log(%s): %v", accessLog, err)
}
errorLog := fs.JoinPath(l.Dir, "error.log")
if l.errorLogFile, err = fs.Open(errorLog); err != nil {
return fmt.Errorf("failed to open error log(%s): %v", accessLog, err)
}
debugLog := fs.JoinPath(l.Dir, "debug.log")
if l.debugLogFile, err = fs.Open(debugLog); err != nil {
return fmt.Errorf("failed to open debug log(%s): %v", accessLog, err)
}
logger.AppendTransports(map[string]transport.Transport{
"access": file.New(&file.Config{
Level: "info",
File: l.accessLogFile,
Exact: true,
}),
"error": file.New(&file.Config{
Level: "error",
File: l.errorLogFile,
Exact: true,
}),
"debug": file.New(&file.Config{
Level: "debug",
File: l.debugLogFile,
Exact: true,
}),
})
logger.SetLevel(l.Level)
return nil
}