/
logger_js.go
99 lines (82 loc) · 2.09 KB
/
logger_js.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// +build js
package logger
import (
"encoding/json"
"fmt"
"time"
"honnef.co/go/js/console"
)
func printLog(prefix string, printTrace bool, message string, v ...interface{}) {
mess := fmt.Sprintf("%s %s %s", time.Now().Format(timeFormat), prefix, fmt.Sprintf(message, v...))
switch prefix {
case WarnPrefix:
console.Warn(mess)
case ErrPrefix:
console.Error(mess)
default:
console.Log(mess)
}
}
// Info prints info log (green if colored)
func Info(message string, v ...interface{}) {
printLog(InfoPrefix, false, message, v...)
}
// Warning prints warning log (yellow if colored)
func Warning(message string, v ...interface{}) {
printLog(WarnPrefix, true, message, v...)
}
// Error prints error log (red if colored)
func Error(message string, v ...interface{}) {
printLog(ErrPrefix, true, message, v...)
}
// Debug prints debug log (blue if colored)
func Debug(message string, v ...interface{}) {
if pdebug {
printLog(DebugPrefix, false, message, v...)
}
}
// Fatal prints error log and finish proccess by os.Exit(1)
func Fatal(message string, v ...interface{}) {
printLog(ErrPrefix, true, message, v...)
}
// Panic prints error log and call panic
func Panic(message string, v ...interface{}) {
DisableBTrace()
printLog(ErrPrefix, true, message, v...)
panic(fmt.Sprintf(message, v...))
}
// PanicRecover recovers panic an print error message
func PanicRecover() {
if err := recover(); err != nil {
Error("PANIC: %s", err)
}
}
// ErrorErr prints err as error log and returns true if err!=nil
func ErrorErr(err error) bool {
if err != nil {
printLog(ErrPrefix, true, "%v", err)
return true
}
return false
}
// WarningErr prints err as warning log and returns true if err!=nil
func WarningErr(err error) bool {
if err != nil {
printLog(WarnPrefix, true, "%v", err)
return true
}
return false
}
// FatalErr prints err as fatal log
func FatalErr(err error) {
if err != nil {
printLog(ErrPrefix, true, "%v", err)
}
}
// JSONDebug prints object in json format
func JSONDebug(a interface{}) {
buf, err := json.MarshalIndent(a, "", " ")
if !WarningErr(err) {
Debug("%s", buf)
}
}