wechat_ai/agent/main.go

59 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"encoding/json"
"fmt"
"sync"
"time"
)
var waitGroup sync.WaitGroup
type logEntry struct {
Level string `json:"level"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
}
// 创建一个函数启动一个协程每隔600ms输出一次日志信息到命令行界面
func logToConsole() {
defer waitGroup.Done()
for {
time.Sleep(600 * time.Millisecond)
fmt.Println(randomLogMsg())
}
}
// 生成随机日志信息,日志信息从以下列表中随机选择 日志格式为json格式格式{"level": "info", "message": "正在处理用户请求..."}日志级别有info、debug、error,warning, think五种日志信息有正在处理用户请求...、正在连接服务器...、正在加载数据...、正在分析用户输入...、正在生成回复...、正在优化模型...、正在更新知识库...等
func randomLogMsg() string {
logMsgs := []logEntry{
{Level: "info", Message: "正在处理用户请求..."},
{Level: "debug", Message: "正在连接服务器..."},
{Level: "error", Message: "正在加载数据..."},
{Level: "warning", Message: "正在分析用户输入..."},
{Level: "think", Message: "正在生成回复..."},
{Level: "info", Message: "正在优化模型..."},
{Level: "info", Message: "正在更新知识库..."},
}
logMsg := logMsgs[time.Now().UnixNano()%int64(len(logMsgs))]
logMsg.Timestamp = time.Now().Format("15:04:05")
data, err := json.Marshal(logMsg)
if err != nil {
return fmt.Sprintf(`{"level":"error","message":"%s","timestamp":"%s"}`, err.Error(), time.Now().Format("15:04:05"))
}
return string(data)
}
func main() {
waitGroup = sync.WaitGroup{}
// 启动日志协程
waitGroup.Add(1)
go logToConsole()
// 等待协程main执行完成
time.Sleep(50 * time.Second)
waitGroup.Wait()
}