-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypolaslogger.go
79 lines (67 loc) · 2.14 KB
/
hypolaslogger.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package hypolaslogger
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
)
// VarDebug is a function for variable debugging
type VarDebug func(info interface{}, name string)
// HypolasLogger struct for logger
type HypolasLogger struct {
Info *log.Logger
Warn *log.Logger
Err *log.Logger
Debug *log.Logger
VarDebug VarDebug // Debug variable. Print value, name and struct in log file
LogFile *os.File
LogDebug bool // Enable debug variable
}
// NewLogger create logger. PATH is the file path where logs will be store
func NewLogger(pathToLogFile string) HypolasLogger {
if pathToLogFile == "" {
pathToLogFile = os.Getenv("HYPOLAS_LOGS_FILE")
}
if pathToLogFile == "" {
log.Println("No path defined for NewLogger(pathToLogFile string).")
log.Fatalln("Define environnement variable HYPOLAS_LOGS_FILE or call function with pathToLogFile not empty.")
}
var err error
var l HypolasLogger
l = HypolasLogger{
VarDebug: func(info interface{}, name string) {
var deb string
deb = fmt.Sprintf("Name: %s | ", name)
deb = deb + fmt.Sprintf("Type: %+v | ", reflect.TypeOf(info))
varType := fmt.Sprintf("%+v", reflect.TypeOf(info))
if varType == "[]uint8" {
varValue := string(reflect.ValueOf(info).Bytes())
deb = deb + fmt.Sprintf("Value: %s | ", varValue)
} else {
deb = deb + fmt.Sprintf("Value: %s | ", reflect.ValueOf(info))
}
l.Debug.Println(deb)
},
}
// Create logs directory if not exist
createLogsFolder(filepath.Dir(pathToLogFile))
l.LogFile, err = os.OpenFile(pathToLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
l.Info = log.New(l.LogFile, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
l.Warn = log.New(l.LogFile, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile)
l.Err = log.New(l.LogFile, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
l.Debug = log.New(l.LogFile, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
return l
}
func createLogsFolder(path string) {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println("ERROR create directory:", err)
}
}
}