From b3d0abe5c752afec512706322e841ffcac73c79e Mon Sep 17 00:00:00 2001
From: yanghui <yanghui@aiotlink.com>
Date: 星期二, 27 四月 2021 15:03:18 +0800
Subject: [PATCH] consolidate log
---
logc.go | 60 ++++++++++++++----------------
scheduleLog.go | 10 +++++
operationlog.go | 10 +++++
3 files changed, 48 insertions(+), 32 deletions(-)
diff --git a/logc.go b/logc.go
index 5946d85..e3f36f5 100644
--- a/logc.go
+++ b/logc.go
@@ -26,6 +26,12 @@
TypeSysInfo //绯荤粺鍙傛暟鍙樻洿,ip,server name,寮�鍏虫満淇℃伅
)
+type LogPrinter interface {
+ Marshal() ([]byte, error)
+ Topic() string
+}
+
+
type LogInfo struct {
ID string `gorm:"column:id;primary_key;unique" json:"id"`
Timestamp string `gorm:"column:timestamp" json:"timestamp"` // 2020-12-03 14:39:41
@@ -47,16 +53,14 @@
type LogReportCallback func(*LogRegister)
var (
- opChan chan *OperationLog
- schedualeChan chan *ScheduleLog
+ logCh chan LogPrinter
logger *Log
ProcName string
ProcID string
)
func Init(flogWriter LogReportCallback, log *Log, procId string, procName string, wg *sync.WaitGroup, done chan struct{}) bool {
- opChan = make(chan *OperationLog, 100)
- schedualeChan = make(chan *ScheduleLog)
+ logCh = make(chan LogPrinter, 300)
if nil != log {
logger = log
@@ -80,17 +84,7 @@
log.ProcName = ProcName
log.ProcID = ProcID
- select {
- case opChan <- log:
- return
- case <-time.After(timeout):
- var info string
- b, err := json.Marshal(log)
- if nil == err {
- info = string(b)
- }
- logger.Fatal("SaveOperationLog failed to save log", info)
- }
+ deliverLog(log, timeout)
}
func SaveScheduleLog(category, level int, timeout time.Duration, v ...interface{}) {
@@ -112,19 +106,22 @@
Info: msg,
}
+ deliverLog(log, timeout)
+}
+
+func deliverLog(l LogPrinter, timeout time.Duration) {
select {
- case schedualeChan <- log:
+ case logCh <- l:
return
case <-time.After(timeout):
var info string
- b, err := json.Marshal(log)
+ b, err := l.Marshal()
if nil == err {
info = string(b)
}
- logger.Fatal("SaveScheduleLog failed to save log", info)
+ logger.Fatal("SaveScheduleLog failed to save log", info, l.Topic())
}
}
-
//func Save(level int, logType int, v ...interface{}) {
// cache(level, logType, "", "", v)
@@ -165,6 +162,7 @@
const (
OperationLogTopic = "operationLogSaveTopic"
+ ScheduleLogTopic = "scheduleLogSaveTopic"
)
func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done chan struct{}) {
@@ -178,24 +176,22 @@
select {
case <- done:
return
- case data := <- opChan:
- payload, err := json.Marshal(data)
+ case log := <- logCh:
+ payload, err := log.Marshal()
if err != nil {
- fmt.Println("json.Marshal(operation data) error:", data)
+ logger.Error("failed to Marshal", log)
} else {
- var nodes []bhomebus.NetNode
- nodes = append(nodes, bhomebus.NetNode{})
+ var nodes []bhomebus.NetNode
+ nodes = append(nodes, bhomebus.NetNode{})
- reg := &LogRegister {
- nodes,
- OperationLogTopic,
- payload,
- }
+ reg := &LogRegister {
+ nodes,
+ log.Topic(),
+ payload,
+ }
- logCallback(reg)
+ logCallback(reg)
}
- default:
- time.Sleep(10*time.Millisecond)
}
}
}
diff --git a/operationlog.go b/operationlog.go
index 083e228..019fec6 100644
--- a/operationlog.go
+++ b/operationlog.go
@@ -1,5 +1,7 @@
package logc
+import "encoding/json"
+
type OperationLog struct {
ID string `gorm:"column:id;primaryKey;unique;autoIncrement" json:"id"` // 涓婚敭id
Timestamp int64 `gorm:"column:timestamp" json:"timestamp"` // 鏃堕棿鎴砋nix time
@@ -14,4 +16,12 @@
Ip string `gorm:"column:ip" json:"ip"` // 璇︽儏
}
+func (l * OperationLog)Marshal()([]byte, error) {
+ return json.Marshal(l)
+}
+
+func (l * OperationLog)Topic() string {
+ return OperationLogTopic
+}
+
diff --git a/scheduleLog.go b/scheduleLog.go
index 0cd045f..229c474 100644
--- a/scheduleLog.go
+++ b/scheduleLog.go
@@ -1,5 +1,7 @@
package logc
+import "encoding/json"
+
type ScheduleLog struct {
ID string `gorm:"column:id;primaryKey;unique;autoIncrement" json:"id"` // 涓婚敭id
Timestamp int64 `gorm:"column:timestamp" json:"timestamp"` // 鏃堕棿鎴砋nix time
@@ -9,3 +11,11 @@
Type int `gorm:"column:type" json:"type"` // 鎿嶄綔绫诲瀷:浜哄伐鎿嶄綔,鐧诲綍閫�鍑�,杞惊鎽勫儚鏈哄強瀵瑰簲绠楁硶,鏁版嵁鏍堝鐞嗘儏鍐�,寮傚父鎯呭喌绛�
Info string `gorm:"column:info" json:"info"` // 璇︽儏
}
+
+func (l * ScheduleLog)Marshal()([]byte, error) {
+ return json.Marshal(l)
+}
+
+func (l * ScheduleLog)Topic() string {
+ return ScheduleLogTopic
+}
--
Gitblit v1.8.0