forked from xlvector/dlog
/
dlog.go
124 lines (113 loc) · 2.58 KB
/
dlog.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
122
123
124
package dlog
import (
"fmt"
"io"
"log"
"net/smtp"
"os"
"runtime"
"strings"
"sync"
"time"
)
type Logger struct {
mu sync.Mutex
date string
filename string
hourAgo string
hFilename string
file *os.File
out io.Writer
}
func time33(s string) int64 {
var ret int64
for _, c := range []byte(s) {
ret *= 33
ret += int64(c)
}
if ret > 0 {
return ret
}
return -1 * ret
}
func (l *Logger) header(tm time.Time, file string, line int, s string) string {
newPath := ""
paths := strings.Split(file, "/")
length := len(paths)
if length > 1 {
newPath = paths[length-2] + "/" + paths[length-1]
} else if length == 1 {
newPath = paths[0]
}
return fmt.Sprintf("%s %s line %d ", tm.Format("2006-01-02 15:04:05"), newPath, line)
}
func (l *Logger) Output(calldepth int, s string, email bool) error {
now := time.Now() // get this early.
var file string
var line int
l.mu.Lock()
defer l.mu.Unlock()
var ok bool
_, file, line, ok = runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
head := l.header(now, file, line, s)
buf := make([]byte, 0, len(head))
buf = append(buf, head...)
for _, c := range []byte(s) {
if c != '\n' {
buf = append(buf, c)
} else {
buf = append(buf, '\n')
_, err := l.out.Write(buf)
if err != nil {
return err
}
_, _ = l.file.Write(buf)
buf = buf[:0]
buf = append(buf, head...)
}
}
if len(buf) > len(head) {
buf = append(buf, '\n')
_, err := l.out.Write(buf)
if err != nil {
return err
}
_, _ = l.file.Write(buf)
}
if email {
target := head + s
emailCache.lock.Lock()
flag := true
for _, cache := range emailCache.cache {
rate := Similarity(target, cache)
if rate > 0.9 {
flag = false
}
}
emailCache.cache = append(emailCache.cache, target)
if flag {
SendMail("程序实时报警", target)
}
emailCache.lock.Unlock()
}
return nil
}
func SendMail(subject, body string) {
sendToMail("host:port", "email@xxx", "", "email@xxx;email@xxx", "plain", subject, body)
}
//smtp服务发送邮件, mailType表示邮件格式是普通文件还是html或其他
func sendToMail(host, user, password, to, mailType, subject, body string) {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
content_type := "Content-Type: text/" + mailType + "; charset=UTF-8"
msg := []byte("To: " + to + "\r\nFrom: " + user + "\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
if err != nil {
log.Println("SendMail error: ", err)
}
}