Skip to content

Commit

Permalink
新增日志
Browse files Browse the repository at this point in the history
  • Loading branch information
rroy233 committed Dec 21, 2022
1 parent cb29c0f commit 82bc6b9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
config.json
/.idea
/bin
/bin
/log
init.go
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module genshinBirthdayHelper

go 1.18

require (
github.com/rroy233/logger v1.0.5 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rroy233/logger v1.0.5 h1:kB3xCAIJPjtfmn0/7cyz7ctnkzh2SP7enwMw2d0dQyI=
github.com/rroy233/logger v1.0.5/go.mod h1:0CJ4dfE/+wGffshyYFFVm20lUu1pigic/gcsCe/SfFY=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 5 additions & 2 deletions helper/helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helper

import (
"github.com/rroy233/logger"
"log"
)

Expand Down Expand Up @@ -36,14 +37,16 @@ func (h *Helper) Do() error {
if err := h.GetBirthdayRole(); err != nil {
log.Fatalln(err)
}
//logger.Debug.Println(h.account.Cookie)

if len(h.roles.Data.Role) == 0 {
log.Println("今天没有角色生日")
logger.Info.Println("今天没有角色生日")
return nil
}

//获取生日贺卡
if err := h.PostBirthday(); err != nil {
log.Fatalln(err)
logger.FATAL.Fatalln(err)
}
return nil
}
10 changes: 5 additions & 5 deletions helper/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/rroy233/logger"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
Expand Down Expand Up @@ -54,7 +54,7 @@ func (h *Helper) Login() error {

respData, _ := ioutil.ReadAll(resp.Body)

//log.Println(string(respData))
//logger.Debug.Println(resp.Header)
h.info = new(RespInfo)
if err = json.Unmarshal(respData, h.info); err != nil {
return err
Expand Down Expand Up @@ -101,7 +101,7 @@ func (h *Helper) GetBirthdayRole() error {

respData, _ := ioutil.ReadAll(resp.Body)

//log.Println(string(respData))
//logger.Debug.Println(string(respData))

h.roles = new(RespBirthdayRole)
if err = json.Unmarshal(respData, h.roles); err != nil {
Expand Down Expand Up @@ -154,9 +154,9 @@ func (h *Helper) PostBirthday() error {
}

if res.Retcode == 0 {
log.Printf("[%d] - 今天是【%s】的生日!已成功获取生日贺卡【%s】!\n", i, r.Name, r.TakePicture)
logger.Info.Printf("[%d] - 今天是【%s】的生日!已成功获取生日贺卡【%s】!\n", i, r.Name, r.TakePicture)
} else if res.Retcode == -512009 {
log.Printf("[%d] - 今天是【%s】的生日!你之前已领取!\n", i, r.Name)
logger.Info.Printf("[%d] - 今天是【%s】的生日!你之前已领取!\n", i, r.Name)
} else {
return errors.New("请求返回错误:" + h.roles.Message)
}
Expand Down
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import (
"encoding/json"
"errors"
"genshinBirthdayHelper/helper"
"io/ioutil"
"github.com/rroy233/logger"
"log"
"os"
)

type ConfigStruct struct {
Accounts []helper.Account `json:"accounts"`
Logger struct {
Enabled bool `json:"enabled"`
Report bool `json:"report"`
ReportUrl string `json:"reportUrl"`
QueryKey string `json:"queryKey"`
} `json:"logger"`
}

var config *ConfigStruct
Expand All @@ -22,15 +28,26 @@ func main() {
log.Fatalln("读取配置发生错误:", err)
}

//日志服务
logger.New(&logger.Config{
StdOutput: true,
StoreLocalFile: config.Logger.Enabled,
StoreRemote: config.Logger.Report,
RemoteConfig: logger.RemoteConfigStruct{
RequestUrl: config.Logger.ReportUrl,
QueryKey: config.Logger.QueryKey,
},
})

for i, account := range config.Accounts {
//初始化
h, err := helper.New(account)
if err != nil {
log.Fatalf("【账号-%d】初始化失败:%s", i, err.Error())
logger.FATAL.Fatalf("【账号-%d】初始化失败:%s", i, err.Error())
}
err = h.Do()
if err != nil {
log.Fatalf("【账号-%d】执行失败:%s", i, err.Error())
logger.FATAL.Fatalf("【账号-%d】执行失败:%s", i, err.Error())
}
}

Expand All @@ -41,12 +58,12 @@ func loadConfig() error {
_, err := os.Open("./config.json")
if err != nil {
if os.IsNotExist(err) == true {
ioutil.WriteFile("./config.json", []byte("{\n\t\"accounts\": [\n\t\t{\n\t\t\t\"server\": \"cn_gf01\",\n\t\t\t\"uid\": \"\",\n\t\t\t\"mys-cookie\": \"\"\n\t\t}\n\t]\n}"), 0755)
os.WriteFile("./config.json", []byte("{\"accounts\":[{\"server\":\"cn_gf01\",\"uid\":\"\",\"mys-cookie\":\"\"}],\"logger\":{\"enabled\":true,\"report\":false,\"reportUrl\":\"http://127.0.0.1:8990/log\",\"queryKey\":\"?key=\"}}"), 0755)
return errors.New("配置文件不存在,已自动创建,请手动填入cookie。")
}
}

data, err := ioutil.ReadFile("./config.json")
data, err := os.ReadFile("./config.json")
if err != nil {
return err
}
Expand Down

0 comments on commit 82bc6b9

Please sign in to comment.