Skip to content

Commit

Permalink
增加日志文件下载功能,可查看每日日志文件,零点切割日志,重新写入新的日志文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoxusheng committed Sep 23, 2023
1 parent 26c06b7 commit c7261fc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Ginterface/GoBat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ type GoBat interface {
Read()
// 监听全局错误
Err()
//日志文件
Log()
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [x] 学习通课表提送功能 (必须含有关键字课表)
- [x] 私人消息防撤回
- [x] 自动同意添加好友
- [x] 可直接收到每天的日志文件
- [x] 群聊
- [x] 撤回消息
- [x] 聊天(与chatgpt聊天)
Expand Down
11 changes: 9 additions & 2 deletions api/Deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Private struct {
t timing
c collyBaidu
d Ddns
f File
}

// Group 群聊
Expand All @@ -50,7 +51,6 @@ type Picture struct {

// 文字
func (t *Text) Controls(s config.Messages) {
fmt.Println(s.NoticeType, s.MessageType == "group")
if s.MessageType == "private" || s.NoticeType != "" {
t.m = new(Private)
t.m.MessageDeal(s, "t")
Expand All @@ -65,7 +65,6 @@ func (t *Text) Controls(s config.Messages) {

// 图片
func (p *Picture) Controls(s config.Messages) {
fmt.Println("8fjj", s.NoticeType, s.MessageType == "group")
if s.MessageType == "private" || s.NoticeType == "friend_recall" {
p.m = new(Private)
p.m.MessageDeal(s, "p")
Expand Down Expand Up @@ -189,6 +188,7 @@ func (p *Private) MessageDeal(s config.Messages, m string) {
fmt.Println(" p.class.w:", p.class.w)
message.Message = p.class.GetClass()
}

if strings.Contains(s.Message, "元神启动") {
go p.class.SetTime()
}
Expand All @@ -198,13 +198,19 @@ func (p *Private) MessageDeal(s config.Messages, m string) {
fmt.Println("p.m.c.Message", M.Data.Message)
message.Message = "[CQ:at," + "qq=" + strconv.FormatInt(s.UserId, 10) + "] 撤回消息" + "\n" + M.Data.Message
}

if strings.Contains(s.Message, "CQ:face") || strings.Contains(s.Message, "CQ:image") {
if m == "p" {
m = "t"
}
message.Message = s.Message
}

if strings.Contains(s.Message, "日志") && s.UserId == config.K.Bat.QQ {
p.f.Upload()
return
}

if config.K.Mode.Chatgpt && message.Message == "" && !strings.Contains(s.Message, "[CQ:image,") {
err := p.chatGpt.Limit(s)
if err != "" {
Expand All @@ -221,6 +227,7 @@ func (p *Private) MessageDeal(s config.Messages, m string) {
config.SendChan <- message
return
}

if message.Message == "" {
message.Message = s.Message
}
Expand Down
42 changes: 42 additions & 0 deletions api/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import (
"Go-Bat/config"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"time"
)

type File struct {
UserId int64 `json:"user_id"`
File string `json:"file"`
Name string `json:"name"`
}

func (f *File) Upload() {
f.UserId = config.K.Bat.QQ
f.Name = time.Now().Format(time.DateOnly) + ".log"
f.File = "/root/GoBatRoot/log/" + time.Now().Format(time.DateOnly) + ".log"
marshal, err := json.Marshal(f)
if err != nil {
log.Println(err)
return
}
resp, err := http.Post("http://127.0.0.1:"+strconv.Itoa(config.K.Server.Port)+"/upload_private_file", "application/json", bytes.NewReader(marshal))
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
all, err := io.ReadAll(resp.Body)
if err != nil {
return
}
fmt.Println(string(all))

}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func main() {
GoBat.Start()
// netstat -tunlp | grep 5700 5700端口占用进程n
//去掉调试信息,减小可执行文件体积
// go build -ldflags="-s -w" -a -v -o xxx main.go
// go build -o GoBat -ldflags "-s -w"

}
66 changes: 46 additions & 20 deletions message/message.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package message

import (
"Go-Bat/config"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"io"
"log"
"net/http"
"os"
"strconv"
"time"

"Go-Bat/config"

"github.com/gorilla/websocket"
)

type GoBat struct {
Expand All @@ -38,38 +40,25 @@ func (b *GoBat) Websocket(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
for {
err := conn.ReadJSON(&Mess)
if err != nil {
continue
}
log.Println("解析mess", Mess.PostType)
if Mess.PostType == "meta_event" {
log.Println("chan还剩", 100-len(config.MessageChan))
Mess = config.Messages{}
continue
}
log.Println("解析mess", Mess.PostType)
config.MessageChan <- Mess
Mess = config.Messages{}
}

}

// Start 开始监听
func (b *GoBat) Start() {
b.Err()
//启动websocket服务
go b.Service()
// 启动读协程
go b.ReadMessage()
// 启动写协程
go b.WriteMessage()
//已读消息
go b.Read()
select {}
}

// ReadMessage 读取
func (b *GoBat) ReadMessage() {
//读取管道消息
Expand All @@ -96,10 +85,10 @@ func (b *GoBat) WriteMessage() {
c.UserId = 3096407768
c.MessageType = "private"
}
fmt.Println("读取到数据", c.Message)
log.Println("读取到数据", c.Message)

go func() {
fmt.Println("发送", c)
log.Println("发送", c)
marshal, err := json.Marshal(c)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -161,7 +150,7 @@ func (b *GoBat) Err() {
// Service 服务
func (b *GoBat) Service() {
//记录日志
logFile, err := os.OpenFile("GoBat.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
logFile, err := os.OpenFile("./log/"+time.Now().Format(time.DateOnly)+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
Expand All @@ -175,3 +164,40 @@ func (b *GoBat) Service() {
return
}
}

func (b *GoBat) Log() {
t1 := time.Now()
t2 := time.Date(t1.Year(), t1.Month(), t1.Day()+1, 0, 0, 0, 0, t1.Location())
t3 := time.NewTimer(t2.Sub(t1))
for {
select {
case <-t3.C:
logFile, err := os.OpenFile("./log/"+time.Now().Format(time.DateOnly)+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
log.SetOutput(io.MultiWriter(logFile, os.Stderr))
t1 = time.Now()
//一天以后执行
t2 = time.Date(t1.Year(), t1.Month(), t1.Day()+1, 0, 0, 0, 0, t1.Location())
t3 = time.NewTimer(t2.Sub(t1))
log.Println("任务启动," + t2.Sub(t1).String() + "后开始执行")
}
}
}

// Start 开始监听
func (b *GoBat) Start() {
b.Err()
//启动websocket服务
go b.Service()
// 启动读协程
go b.ReadMessage()
// 启动写协程
go b.WriteMessage()
//已读消息
go b.Read()
//记录日志
go b.Log()
select {}
}

0 comments on commit c7261fc

Please sign in to comment.