Browse Source

Merge pull request #35 from pppscn/main

重写log标准库,将日志同时输出到控制台和文件
main
ztino 4 years ago
committed by GitHub
parent
commit
e7afd8ca01
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 2
      cmd/jdTdudfp.go
  3. 3
      cmd/login.go
  4. 2
      cmd/logout.go
  5. 2
      cmd/reserve.go
  6. 2
      cmd/seckill.go
  7. 3
      go.mod
  8. 2
      jd_seckill/seckill.go
  9. 2
      jd_seckill/session.go
  10. 14
      jd_seckill/user.go
  11. 66
      log/log.go
  12. 18
      main.go
  13. 2
      service/dingtalk.go
  14. 2
      service/email.go
  15. 2
      service/wechat.go

1
.gitignore

@ -21,3 +21,4 @@
/cookie.txt
*.bak
go.sum
/同步代码.sh

2
cmd/jdTdudfp.go

@ -10,7 +10,7 @@ import (
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/jd_seckill"
"log"
"github.com/ztino/jd_seckill/log"
"net/url"
"os"
"time"

3
cmd/login.go

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/jd_seckill"
"log"
"github.com/ztino/jd_seckill/log"
"os"
"time"
)
@ -30,6 +30,7 @@ func startLogin(cmd *cobra.Command, args []string) {
return
}
user:=jd_seckill.NewUser(common.Client,common.Config)
user.DelQrCode()
log.Println("登录成功")
userInfo,_:=user.GetUserInfo()
log.Println("用户:"+userInfo)

2
cmd/logout.go

@ -3,7 +3,7 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/ztino/jd_seckill/common"
"log"
"github.com/ztino/jd_seckill/log"
"os"
)

2
cmd/reserve.go

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/jd_seckill"
"log"
"github.com/ztino/jd_seckill/log"
)
func init() {

2
cmd/seckill.go

@ -8,7 +8,7 @@ import (
"github.com/tidwall/gjson"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/jd_seckill"
"log"
"github.com/ztino/jd_seckill/log"
"net/http"
"os"
"strconv"

3
go.mod

@ -4,11 +4,13 @@ go 1.15
require (
github.com/Albert-Zhan/httpc v0.0.0-20190712132051-aed72924b5e6
github.com/CodyGuo/win v0.0.0-20170113125346-08e6b7208274
github.com/PuerkitoBio/goquery v1.6.0
github.com/blinkbean/dingtalk v0.0.0-20201231030509-45a553a84503
github.com/chromedp/cdproto v0.0.0-20201204063249-be40c824ad18
github.com/chromedp/chromedp v0.5.4
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gookit/color v1.3.6
github.com/json-iterator/go v1.1.10 // indirect
github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
@ -18,6 +20,7 @@ require (
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014 // indirect
golang.org/x/text v0.3.4
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

2
jd_seckill/seckill.go

@ -9,7 +9,7 @@ import (
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/service"
"log"
"github.com/ztino/jd_seckill/log"
"net/http"
"strconv"
"strings"

2
jd_seckill/session.go

@ -5,8 +5,8 @@ import (
"errors"
"github.com/Albert-Zhan/httpc"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/log"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"

14
jd_seckill/user.go

@ -7,7 +7,7 @@ import (
"github.com/tidwall/gjson"
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/common"
"log"
"github.com/ztino/jd_seckill/log"
"net/http"
"os"
"path/filepath"
@ -85,6 +85,7 @@ func (this *User) TicketInfo(ticket string) (string,error) {
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Referer","https://passport.jd.com/uc/login?ltype=logout")
resp,body,err:=req.SetUrl("https://passport.jd.com/uc/qrCodeTicketValidation?t="+ticket).SetMethod("get").Send().End()
defer this.DelQrCode()
if err!=nil || resp.StatusCode!=http.StatusOK {
log.Println("二维码信息校验失败")
return "",errors.New("二维码信息校验失败")
@ -135,4 +136,13 @@ func (this *User) GetUserInfo() (string,error) {
}
b,_:=common.GbkToUtf8([]byte(nickName))
return string(b), nil
}
}
func (this *User) DelQrCode() {
dir, _ := os.Getwd()
qrPath := filepath.Join(dir, `./qr_code.png`)
log.Println(qrPath)
if _, err := os.Stat(qrPath); !os.IsNotExist(err) {
os.Remove(qrPath)
}
}

66
log/log.go

@ -0,0 +1,66 @@
package log
import (
"github.com/gookit/color"
"io"
"log"
"os"
"time"
)
//重写log标准库,需要多少个方法就加多少个
var file = "./logs/jd_seckill_" + time.Now().Format("20060102") + ".log"
//将日志同时输出到控制台和文件
func Println(v ...interface{}) {
logFile, logErr := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if logErr != nil {
panic(logErr)
}
defer logFile.Close()
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
//log.SetPrefix("[jd_seckill]")
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
log.Println(v...)
}
//将日志同时输出到控制台和文件
func ColorPrintln(color2 color.Color, v ...interface{}) {
logFile, logErr := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if logErr != nil {
panic(logErr)
}
defer logFile.Close()
log.SetOutput(logFile)
//log.SetPrefix("[jd_seckill]")
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
log.Println(v...)
color2.Light().Println(v...)
}
func Fatal(v ...interface{}) {
log.Fatal(v...)
}
func Printf(format string, v ...interface{}) {
log.Printf(format, v...)
}
func Success(v ...interface{}) {
ColorPrintln(color.Green, v...)
}
func Info(v ...interface{}) {
ColorPrintln(color.LightCyan, v...)
}
func Warning(v ...interface{}) {
ColorPrintln(color.Yellow, v...)
}
func Error(v ...interface{}) {
ColorPrintln(color.FgLightRed, v...)
os.Exit(0)
}

18
main.go

@ -5,28 +5,16 @@ import (
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/cmd"
"github.com/ztino/jd_seckill/common"
"io"
"log"
"github.com/ztino/jd_seckill/log"
"os"
"runtime"
"time"
)
func init() {
//日志初始化,将日志同时输出到控制台和文件
//日志初始化
if !common.IsDir("./logs/") {
_=os.Mkdir("./logs/",0777)
_ = os.Mkdir("./logs/", 0777)
}
file := "./logs/jd_seckill_" + time.Now().Format("20060102") + ".log"
logFile, logErr := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if logErr != nil {
panic(logErr)
}
defer logFile.Close()
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
log.SetPrefix("[jd_seckill] ")
log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
//客户端设置初始化
common.Client=httpc.NewHttpClient()

2
service/dingtalk.go

@ -3,7 +3,7 @@ package service
import (
"github.com/blinkbean/dingtalk"
"github.com/unknwon/goconfig"
"log"
"github.com/ztino/jd_seckill/log"
)
type Dingtalk struct {

2
service/email.go

@ -3,7 +3,7 @@ package service
import (
"github.com/unknwon/goconfig"
"gopkg.in/gomail.v2"
"log"
"github.com/ztino/jd_seckill/log"
"strconv"
)

2
service/wechat.go

@ -6,7 +6,7 @@ import (
"github.com/Albert-Zhan/httpc"
"github.com/tidwall/gjson"
"github.com/unknwon/goconfig"
"log"
"github.com/ztino/jd_seckill/log"
"net/http"
)

Loading…
Cancel
Save