1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-21 00:20:49 +00:00
Beego/logs/alils/alils.go

187 lines
3.4 KiB
Go
Raw Normal View History

2016-12-27 01:58:42 +00:00
package alils
import (
"encoding/json"
"strings"
"sync"
"time"
2017-04-28 13:38:08 +00:00
"github.com/astaxie/beego/logs"
"github.com/gogo/protobuf/proto"
2016-12-27 01:58:42 +00:00
)
const (
2017-04-30 14:41:23 +00:00
// CacheSize set the flush size
CacheSize int = 64
// Delimiter define the topic delimiter
2016-12-27 01:58:42 +00:00
Delimiter string = "##"
)
2017-04-30 14:41:23 +00:00
// Config is the Config for Ali Log
type Config struct {
2016-12-27 01:58:42 +00:00
Project string `json:"project"`
Endpoint string `json:"endpoint"`
KeyID string `json:"key_id"`
KeySecret string `json:"key_secret"`
LogStore string `json:"log_store"`
Topics []string `json:"topics"`
Source string `json:"source"`
Level int `json:"level"`
FlushWhen int `json:"flush_when"`
}
// aliLSWriter implements LoggerInterface.
// it writes messages in keep-live tcp connection.
type aliLSWriter struct {
store *LogStore
group []*LogGroup
withMap bool
groupMap map[string]*LogGroup
lock *sync.Mutex
2017-04-30 14:41:23 +00:00
Config
2016-12-27 01:58:42 +00:00
}
2017-04-30 14:41:23 +00:00
// NewAliLS create a new Logger
2016-12-27 01:58:42 +00:00
func NewAliLS() logs.Logger {
alils := new(aliLSWriter)
alils.Level = logs.LevelTrace
return alils
}
2017-04-30 14:41:23 +00:00
// Init parse config and init struct
2016-12-27 01:58:42 +00:00
func (c *aliLSWriter) Init(jsonConfig string) (err error) {
json.Unmarshal([]byte(jsonConfig), c)
if c.FlushWhen > CacheSize {
c.FlushWhen = CacheSize
}
prj := &LogProject{
Name: c.Project,
Endpoint: c.Endpoint,
2017-04-30 14:41:23 +00:00
AccessKeyID: c.KeyID,
2016-12-27 01:58:42 +00:00
AccessKeySecret: c.KeySecret,
}
c.store, err = prj.GetLogStore(c.LogStore)
if err != nil {
return err
}
2017-04-30 14:41:23 +00:00
// Create default Log Group
2016-12-27 01:58:42 +00:00
c.group = append(c.group, &LogGroup{
Topic: proto.String(""),
Source: proto.String(c.Source),
Logs: make([]*Log, 0, c.FlushWhen),
})
2017-04-30 14:41:23 +00:00
// Create other Log Group
2016-12-27 01:58:42 +00:00
c.groupMap = make(map[string]*LogGroup)
for _, topic := range c.Topics {
lg := &LogGroup{
Topic: proto.String(topic),
Source: proto.String(c.Source),
Logs: make([]*Log, 0, c.FlushWhen),
}
c.group = append(c.group, lg)
c.groupMap[topic] = lg
}
if len(c.group) == 1 {
c.withMap = false
} else {
c.withMap = true
}
c.lock = &sync.Mutex{}
return nil
}
// WriteMsg write message in connection.
// if connection is down, try to re-connect.
func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error) {
if level > c.Level {
return nil
}
var topic string
var content string
var lg *LogGroup
if c.withMap {
2017-04-30 14:41:23 +00:00
// TopicLogGroup
2016-12-27 01:58:42 +00:00
strs := strings.SplitN(msg, Delimiter, 2)
if len(strs) == 2 {
pos := strings.LastIndex(strs[0], " ")
topic = strs[0][pos+1 : len(strs[0])]
content = strs[0][0:pos] + strs[1]
lg = c.groupMap[topic]
}
2017-04-30 14:41:23 +00:00
// send to empty Topic
2016-12-27 01:58:42 +00:00
if lg == nil {
content = msg
lg = c.group[0]
}
} else {
content = msg
lg = c.group[0]
}
2017-04-30 14:41:23 +00:00
c1 := &LogContent{
2016-12-27 01:58:42 +00:00
Key: proto.String("msg"),
Value: proto.String(content),
}
l := &Log{
2017-04-30 14:41:23 +00:00
Time: proto.Uint32(uint32(when.Unix())),
Contents: []*LogContent{
2016-12-27 01:58:42 +00:00
c1,
},
}
c.lock.Lock()
lg.Logs = append(lg.Logs, l)
c.lock.Unlock()
if len(lg.Logs) >= c.FlushWhen {
c.flush(lg)
}
return nil
}
// Flush implementing method. empty.
func (c *aliLSWriter) Flush() {
2017-04-30 14:41:23 +00:00
// flush all group
2016-12-27 01:58:42 +00:00
for _, lg := range c.group {
c.flush(lg)
}
}
// Destroy destroy connection writer and close tcp listener.
func (c *aliLSWriter) Destroy() {
}
func (c *aliLSWriter) flush(lg *LogGroup) {
c.lock.Lock()
defer c.lock.Unlock()
err := c.store.PutLogs(lg)
if err != nil {
return
}
lg.Logs = make([]*Log, 0, c.FlushWhen)
}
func init() {
logs.Register(logs.AdapterAliLS, NewAliLS)
}