/
im.go
121 lines (105 loc) · 2.31 KB
/
im.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package xim
import (
"fmt"
"os"
"strings"
"github.com/hyacinthus/x/xlog"
)
var log = xlog.Get()
var debug bool
// 企业微信机器人的 key
var (
infoKey string
warnKey string
errorKey string
)
// 注意这要在程序运行前存在环境变量才有效
func init() {
de := os.Getenv("APP_DEBUG")
if de == "true" || de == "TRUE" || de == "True" || de == "1" {
SetDebug()
}
infoKey = os.Getenv("IM_INFO_KEY")
warnKey = os.Getenv("IM_WARN_KEY")
errorKey = os.Getenv("IM_ERROR_KEY")
// 只要配置不齐,就设置成调试模式
if infoKey == "" || warnKey == "" || errorKey == "" {
SetDebug()
}
}
// SetDebug 设置为调试模式,通知打印到日志
func SetDebug() {
debug = true
}
// Error 企业微信错误通知,调试模式下只打日志
func Error(args ...interface{}) {
if debug {
log.Error(args...)
} else {
wError(args...)
}
}
// Errorf 企业微信错误通知,调试模式下只打日志
func Errorf(format string, a ...interface{}) {
if debug {
log.Errorf(format, a...)
} else {
wError(fmt.Sprintf(format, a...))
}
}
// ErrorMD Markdown 格式企业微信出错通知,调试模式下只打日志
func ErrorMD(lines []string) {
if debug {
log.Info(strings.Join(lines, "\n"))
} else {
wErrorMD(lines)
}
}
// Warn 企业微信重要通知,调试模式下只打日志
func Warn(args ...interface{}) {
if debug {
log.Warn(args...)
} else {
wWarn(args...)
}
}
// Warnf 企业微信重要通知,调试模式下只打日志
func Warnf(format string, a ...interface{}) {
if debug {
log.Warnf(format, a...)
} else {
wWarn(fmt.Sprintf(format, a...))
}
}
// WarnMD Markdown 格式企业微信重要通知,调试模式下只打日志
func WarnMD(lines []string) {
if debug {
log.Warn(strings.Join(lines, "\n"))
} else {
wWarnMD(lines)
}
}
// Info 企业微信通知,调试模式下只打日志
func Info(args ...interface{}) {
if debug {
log.Info(args...)
} else {
wInfo(args...)
}
}
// Infof 企业微信通知,调试模式下只打日志
func Infof(format string, a ...interface{}) {
if debug {
log.Infof(format, a...)
} else {
wInfo(fmt.Sprintf(format, a...))
}
}
// InfoMD Markdown 格式企业微信一般通知,调试模式下只打日志
func InfoMD(lines []string) {
if debug {
log.Info(strings.Join(lines, "\n"))
} else {
wInfoMD(lines)
}
}