在服务中往往磁盘I/O会成为性能瓶颈,我想知道在golang项目中利用log包记录日志的时候有没有必要通过协程实现?还是说同步执行就好,几乎在所有项目中都是同步写的
改协程比较好
```go
package dogger
import (
"log"
"net/http"
"bytes"
)
type Ding struct {
WebHookUrl string
}
func (d *Ding) Write(p []byte) (n int, err error) {
//remove last \n byte
p = p[:len(p)-2]
buf := bytes.NewBufferString(`{"msgtype":"text","text":{"content":"`)
buf.Write(p)
buf.WriteString(`"}}`)
//发功日志到钉钉群机器人
go http.Post(d.WebHookUrl, "application/json", buf)
return 0, err
}
var logger *log.Logger
func NewLogger(hookUrl string) *log.Logger {
dingDingTalker := Ding{hookUrl}
logger = log.New(&dingDingTalker, "eric:", log.Lshortfile | log.LstdFlags)
return logger
}
func Println(v ...interface{}) {
if logger == nil {
log.Fatal("请设置钉钉bot Web Hook Url")
}
logger.Println(v)
}
```
#1