-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
47 lines (40 loc) · 982 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
package logger
import (
"errors"
"log"
"os"
"strings"
helpers "github.com/nerdynz/helpers"
)
func LogPath() (string, error) {
logPath := os.Getenv("LOG_FOLDER")
if logPath == "" {
logPath = os.Getenv("ATTACHMENTS_FOLDER") + "logs/"
}
if logPath == "" {
logPath = "./attachments/logs/"
}
if logsFolder := os.Getenv("LOGS_FOLDER"); logsFolder != "" {
logPath = logsFolder
}
// time := time.Now()
// fullpath := logPath + time.Format("012006") + "/"
if _, fullErr := os.Stat(logPath); os.IsNotExist(fullErr) {
return "", errors.New("Log folder is unavailable: " + fullErr.Error())
}
return logPath, nil
}
func Log(filekey string, msg string) {
logpath, err := LogPath()
if err != nil {
panic(err)
}
f, err := os.OpenFile(logpath+strings.ToLower(helpers.KebabCase(filekey)), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
// dont care logging shouldn't break anything
return
}
defer f.Close()
log.SetOutput(f)
log.Println(msg)
}