Bladeren bron

重写log标准库,将日志同时输出到控制台和文件

main
pppscn 4 jaren geleden
bovenliggende
commit
9e87050434
  1. 1
      .gitignore
  2. 2
      cmd/jdTdudfp.go
  3. 2
      cmd/login.go
  4. 2
      cmd/logout.go
  5. 2
      cmd/reserve.go
  6. 2
      cmd/seckill.go
  7. 2
      go.mod
  8. 2
      jd_seckill/seckill.go
  9. 2
      jd_seckill/session.go
  10. 2
      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"

2
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"
)

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"

2
go.mod

@ -10,6 +10,7 @@ require (
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
@ -19,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"

2
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"

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"
)

Laden…
Annuleren
Opslaan