-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
56 lines (48 loc) · 1.41 KB
/
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
package main
import (
"fmt"
"log"
"os"
"sync"
)
var lock = &sync.Mutex{}
var logger *LoggerType
type AllLoggers struct {
Info *log.Logger
Warn *log.Logger
Err *log.Logger
Fatal *log.Logger
}
type LoggerType struct {
Info *log.Logger
Warn *log.Logger
Err *log.Logger
Fatal *log.Logger
}
func GetLoggerType() AllLoggers {
var allloggers AllLoggers
homedir, _ := os.UserHomeDir()
//@todo check if they exist first
Infolog, err := os.OpenFile(homedir+"/.flowcat/logs/info.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0775)
if err != nil {
fmt.Println("info log file not created", err.Error())
}
Errorlog, err := os.OpenFile(homedir+"/.flowcat/logs/error.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0775)
if err != nil {
fmt.Println("error log file not created", err.Error())
}
lock.Lock()
defer lock.Unlock()
if logger == nil {
logger = &LoggerType{}
}
logger.Info = log.New(Infolog, "[info]", log.LstdFlags|log.Lshortfile|log.Ldate|log.Ltime|log.LUTC)
logger.Warn = log.New(Infolog, "[warn]", log.LstdFlags|log.Lshortfile|log.Ldate|log.Ltime|log.LUTC)
logger.Err = log.New(Errorlog, "[ERROR]", log.LstdFlags|log.Lshortfile|log.Ldate|log.Ltime|log.LUTC)
logger.Fatal = log.New(Errorlog, "[FATAL]", log.LstdFlags|log.Lshortfile|log.Ldate|log.Ltime|log.LUTC)
allloggers.Info = logger.Info
allloggers.Warn = logger.Warn
allloggers.Err = logger.Err
allloggers.Fatal = logger.Fatal
return allloggers
}