-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
53 lines (43 loc) · 1.11 KB
/
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
50
51
52
53
package golog
import (
"fmt"
c "github.com/Ifkarsyah/golog/color"
"log"
"os"
"runtime"
)
var isDebug bool
func init() {
isDebug = os.Getenv("APP_ENV") == "DEV"
}
func Success(msg string) {
log.Println(commonLog("SUCCESS", c.Green, msg))
}
func Info(msg string) {
log.Println(commonLog("INFO", c.Blue, msg))
}
func Warn(msg string) {
log.Println(commonLog("INFO", c.Yellow, msg))
}
func Debug(err error) {
if isDebug {
log.Println(commonError("DEBUG", c.Red, err))
}
}
func Fatal(err error) {
if isDebug {
log.Fatal(commonError("FATAL", c.Purple, err))
}
}
func commonError(tag string, color string, err error) string {
function, file, line, _ := runtime.Caller(2)
msg := fmt.Sprintf("[%s][%s:%s:%d]: '%s'", tag, baseFile(file), runtime.FuncForPC(function).Name(), line, err)
colorizedMsg := c.Ize(color, msg)
return colorizedMsg
}
func commonLog(tag string, color string, msg string) string {
function, file, line, _ := runtime.Caller(2)
msg = fmt.Sprintf("[%s][%s:%s:%d]: '%s'", tag, baseFile(file), runtime.FuncForPC(function).Name(), line, msg)
colorizedMsg := c.Ize(color, msg)
return colorizedMsg
}