Browse Source

ADD:添加日志最大保存时间

zoie 8 months ago
parent
commit
8f2a335760
7 changed files with 108 additions and 5 deletions
  1. 34 0
      .gitignore
  2. 2 0
      go.mod
  3. 5 0
      go.sum
  4. 15 5
      pkg/logger/log.go
  5. 8 0
      pkg/logger/options.go
  6. 42 0
      pkg/sms/sms.go
  7. 2 0
      sdk/config/logger.go

+ 34 - 0
.gitignore

@@ -0,0 +1,34 @@
+# ---> Go
+# Compiled Object files, Static and Dynamic libs (Shared Objects)
+*.o
+*.a
+*.so
+
+# Folders
+_obj
+_test
+
+# Architecture specific extensions/prefixes
+*.[568vq]
+[568vq].out
+
+*.cgo1.go
+*.cgo2.c
+_cgo_defun.c
+_cgo_gotypes.go
+_cgo_export.*
+
+_testmain.go
+
+*.exe
+*.test
+*.prof
+
+# local
+.idea
+/_
+/.cache
+.vscode/
+.DS_Store
+
+

+ 2 - 0
go.mod

@@ -63,6 +63,8 @@ require (
 	github.com/jtolds/gls v4.20.0+incompatible // indirect
 	github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
 	github.com/leodido/go-urn v1.2.1 // indirect
+	github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
+	github.com/lestrrat-go/strftime v1.0.6 // indirect
 	github.com/mattn/go-colorable v0.1.7 // indirect
 	github.com/mattn/go-isatty v0.0.14 // indirect
 	github.com/mattn/goveralls v0.0.2 // indirect

+ 5 - 0
go.sum

@@ -158,6 +158,11 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
 github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
+github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
+github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
+github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
+github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
+github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
 github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
 github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
 github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=

+ 15 - 5
pkg/logger/log.go

@@ -1,10 +1,11 @@
 package logger
 
 import (
+	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
 	"io"
 	"os"
-
-	"gogs.baozhida.cn/zoie/OAuth-core/debug/writer"
+	"time"
+	//"gogs.baozhida.cn/zoie/OAuth-core/debug/writer"
 	"gogs.baozhida.cn/zoie/OAuth-core/logger"
 	log "gogs.baozhida.cn/zoie/OAuth-core/logger"
 	"gogs.baozhida.cn/zoie/OAuth-core/pkg"
@@ -27,9 +28,18 @@ func SetupLogger(opts ...Option) logger.Logger {
 	var output io.Writer
 	switch op.stdout {
 	case "file":
-		output, err = writer.NewFileWriter(
-			writer.WithPath(op.path),
-			writer.WithCap(op.cap<<10),
+		//output, err = writer.NewFileWriter(
+		//	writer.WithPath(op.path),
+		//	writer.WithCap(op.cap<<10),
+		//)
+		//if err != nil {
+		//	log.Fatal("logger setup error: %s", err.Error())
+		//}
+		output, err = rotatelogs.New(
+			op.path+"/%Y-%m-%d.log",
+			rotatelogs.WithLinkName(op.path+"/log.log"),
+			rotatelogs.WithMaxAge(7*24*time.Hour),
+			rotatelogs.WithRotationTime(24*time.Hour),
 		)
 		if err != nil {
 			log.Fatal("logger setup error: %s", err.Error())

+ 8 - 0
pkg/logger/options.go

@@ -14,6 +14,7 @@ type options struct {
 	path   string
 	level  string
 	stdout string
+	maxDays int
 	cap    uint
 }
 
@@ -23,6 +24,7 @@ func setDefault() options {
 		path:   "temp/logs",
 		level:  "warn",
 		stdout: "default",
+		maxDays: 7,
 	}
 }
 
@@ -55,3 +57,9 @@ func WithCap(n uint) Option {
 		o.cap = n
 	}
 }
+
+func WithMaxDays(n int) Option {
+	return func(o *options) {
+		o.maxDays = n
+	}
+}

+ 42 - 0
pkg/sms/sms.go

@@ -28,6 +28,13 @@ type SendRes struct {
 	Msg     string `json:"msg"`
 	Code    int    `json:"code"`
 }
+type XSendRes struct {
+	Status  string  `json:"status"`
+	Send_id string  `json:"send_id"`
+	Fee     float64 `json:"fee"`
+	Msg     string  `json:"msg"`
+	Code    string  `json:"code"`
+}
 
 // 短信发送
 func (t *SMS) Send(to, content string) (SendRes, error) {
@@ -54,3 +61,38 @@ func (t *SMS) Send(to, content string) (SendRes, error) {
 	}
 	return temp, nil
 }
+
+// 短信模板发送
+func (t *SMS) SmsXSend(to, addr string) (XSendRes, error) {
+
+	type Vars struct {
+		Addr string `json:"addr"`
+	}
+
+	vars := Vars{Addr: addr}
+
+	b, _ := json.Marshal(vars)
+
+	client := resty.New()
+	resp, err := client.R().
+		SetHeader("Content-Type", "application/x-www-form-urlencoded").
+		SetFormData(map[string]string{
+			"appid":     t.Appid,
+			"signature": t.Signature,
+			"to":        to,
+			"project":   "ZZ7fYr",
+			"vars":      string(b),
+		}).
+		SetResult(&XSendRes{}).
+		Post("https://api-v4.mysubmail.com/sms/xsend")
+
+	if err != nil {
+		return XSendRes{}, err
+	}
+
+	temp := XSendRes{}
+	if err = json.Unmarshal(resp.Body(), &temp); err != nil {
+		return XSendRes{}, err
+	}
+	return temp, nil
+}

+ 2 - 0
sdk/config/logger.go

@@ -9,6 +9,7 @@ type Logger struct {
 	Stdout    string
 	EnabledDB bool
 	Cap       uint
+	MaxDays   int
 }
 
 // Setup 设置logger
@@ -19,6 +20,7 @@ func (e Logger) Setup() {
 		logger.WithLevel(e.Level),
 		logger.WithStdout(e.Stdout),
 		logger.WithCap(e.Cap),
+		logger.WithMaxDays(e.MaxDays),
 	)
 }