Skip to content

Commit

Permalink
新增:日志上传到FOPS中心
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 31, 2023
1 parent d3a3efe commit 4b14731
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion flog/compositionLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CompositionLogger struct {
}

// 调用所有日志记录器的实现
func (r *CompositionLogger) log(log *logData) {
func (r *CompositionLogger) log(log *LogData) {
for i := 0; i < len(r.loggerPersistentList); i++ {
if r.loggerPersistentList[i].IsEnabled(log.LogLevel) {
r.loggerPersistentList[i].Log(log.LogLevel, log, nil)
Expand Down
2 changes: 1 addition & 1 deletion flog/consoleProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *consoleLoggerPersistent) IsEnabled(logLevel eumLogLevel.Enum) bool {
return logLevel >= r.logLevel
}

func (r *consoleLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *logData, exception error) {
func (r *consoleLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *LogData, exception error) {
if log.newLine {
fmt.Println(r.formatter.Formatter(log))
} else {
Expand Down
2 changes: 1 addition & 1 deletion flog/fileProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *fileLoggerPersistent) IsEnabled(logLevel eumLogLevel.Enum) bool {
return logLevel >= r.logLevel
}

func (r *fileLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *logData, exception error) {
func (r *fileLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *LogData, exception error) {
var logContent string
if log.newLine {
logContent = r.formatter.Formatter(log) + "\r\n"
Expand Down
22 changes: 7 additions & 15 deletions flog/fopsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/farseer-go/fs"
"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/core/eumLogLevel"
"github.com/farseer-go/fs/dateTime"
"github.com/farseer-go/fs/parse"
"github.com/farseer-go/fs/trace"
"net/http"
"time"
)
Expand All @@ -21,7 +17,7 @@ type FopsProvider struct {
}

func (r *FopsProvider) CreateLogger(categoryName string, formatter IFormatter, logLevel eumLogLevel.Enum) ILoggerPersistent {
persistent := &fopsLoggerPersistent{formatter: formatter, fopsServer: configure.GetFopsServer(), queue: make(chan *logData, 10000)}
persistent := &fopsLoggerPersistent{formatter: formatter, fopsServer: configure.GetFopsServer(), queue: make(chan *LogData, 10000)}
// 异步开启上传
go persistent.enableUpload()
return persistent
Expand All @@ -30,21 +26,21 @@ func (r *FopsProvider) CreateLogger(categoryName string, formatter IFormatter, l
type fopsLoggerPersistent struct {
formatter IFormatter
fopsServer string // fops服务端
queue chan *logData // 待上传的列表
queue chan *LogData // 待上传的列表
}

func (r *fopsLoggerPersistent) IsEnabled(logLevel eumLogLevel.Enum) bool {
return true
}

func (r *fopsLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *logData, exception error) {
func (r *fopsLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *LogData, exception error) {
r.queue <- log
}

// 开启上传
func (r *fopsLoggerPersistent) enableUpload() {
for range time.NewTicker(3 * time.Second).C {
var lst []*logData
var lst []*LogData
// 当队列中有数据 且 取出的数量<1000时,则继续取出
for len(r.queue) > 0 && len(lst) < 1000 {
lst = append(lst, <-r.queue)
Expand All @@ -62,25 +58,21 @@ func (r *fopsLoggerPersistent) enableUpload() {
r.queue <- lst[i]
}
// 不能使用flog.Error,如果此处执行了,会一直产生无用的错误信息
fmt.Println(r.formatter.Formatter(&logData{CreateAt: dateTime.Now(), LogLevel: eumLogLevel.Warning, Component: "", Content: err.Error(), newLine: true}))
fmt.Println(r.formatter.Formatter(&LogData{CreateAt: dateTime.Now(), LogLevel: eumLogLevel.Warning, Component: "", Content: err.Error(), newLine: true}))
}
}
}

type UploadRequest struct {
List []*logData
List []*LogData
}

func (r *fopsLoggerPersistent) upload(lstLog []*logData) error {
func (r *fopsLoggerPersistent) upload(lstLog []*LogData) error {
bodyByte, _ := json.Marshal(UploadRequest{List: lstLog})
url := r.fopsServer + "flog/upload"
newRequest, _ := http.NewRequest("POST", url, bytes.NewReader(bodyByte))
newRequest.Header.Set("Content-Type", "application/json")
// 链路追踪
if traceContext := container.Resolve[trace.IManager]().GetCurTrace(); traceContext != nil {
newRequest.Header.Set("Trace-Id", parse.ToString(traceContext.GetTraceId()))
newRequest.Header.Set("Trace-App-Name", fs.AppName)
}
client := &http.Client{}
rsp, err := client.Do(newRequest)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions flog/iFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

// IFormatter 日志格式
type IFormatter interface {
Formatter(log *logData) string
Formatter(log *LogData) string
}

// JsonFormatter json格式输出
type JsonFormatter struct {
}

func (r JsonFormatter) Formatter(log *logData) string {
marshal, _ := json.Marshal(logData{
func (r JsonFormatter) Formatter(log *LogData) string {
marshal, _ := json.Marshal(LogData{
CreateAt: log.CreateAt,
LogLevel: log.LogLevel,
Component: log.Component,
Expand All @@ -31,7 +31,7 @@ type TextFormatter struct {
config levelFormat
}

func (r TextFormatter) Formatter(log *logData) string {
func (r TextFormatter) Formatter(log *LogData) string {
var logLevelString string
if log.LogLevel != eumLogLevel.NoneLevel {
logLevelString = Colors[log.LogLevel]("[" + log.LogLevel.ToString() + "]")
Expand Down
2 changes: 1 addition & 1 deletion flog/iLoggerProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ type ILoggerPersistent interface {
// IsEnabled 根据日志等级确定是否需要记录
IsEnabled(logLevel eumLogLevel.Enum) bool
// Log 日志记录
Log(LogLevel eumLogLevel.Enum, log *logData, exception error)
Log(LogLevel eumLogLevel.Enum, log *LogData, exception error)
}
10 changes: 5 additions & 5 deletions flog/logData.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (
var regexStr = "\u001b\\[[\\d;]*m"
var mustCompile = regexp.MustCompile(regexStr)

// 日志结构
type logData struct {
// LogData 日志结构
type LogData struct {
CreateAt dateTime.DateTime
LogLevel eumLogLevel.Enum
Component string // 组件名称
Content string
newLine bool // 是否需要换行
}

func newLogData(logLevel eumLogLevel.Enum, content string, component string) *logData {
return &logData{Content: content, CreateAt: dateTime.Now(), LogLevel: logLevel, Component: component, newLine: true}
func newLogData(logLevel eumLogLevel.Enum, content string, component string) *LogData {
return &LogData{Content: content, CreateAt: dateTime.Now(), LogLevel: logLevel, Component: component, newLine: true}
}

//// 清除颜色
//func (receiver *logData) clearColor() {
//func (receiver *LogData) clearColor() {
// receiver.Content = mustCompile.ReplaceAllString(receiver.Content, "")
//}
2 changes: 1 addition & 1 deletion trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestTrace(t *testing.T) {
emptyManager.TraceMq("", "", "")
emptyManager.EntryFSchedule("", 0, 0)
emptyManager.EntryMqConsumer("", "", "")
emptyManager.EntryQueueConsumer("")
emptyManager.EntryQueueConsumer("", "")
emptyManager.EntryTask("")
emptyManager.EntryWatchKey("")
emptyManager.EntryWebApi("", "", "", "", nil, "", "")
Expand Down

0 comments on commit 4b14731

Please sign in to comment.