Skip to content

Commit

Permalink
重写log标准库,将日志同时输出到控制台和文件
Browse files Browse the repository at this point in the history
  • Loading branch information
pppscn committed Jan 6, 2021
1 parent 1b2269d commit 9e87050
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
/cookie.txt
*.bak
go.sum
/同步代码.sh
2 changes: 1 addition & 1 deletion cmd/jdTdudfp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/reserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/seckill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion jd_seckill/seckill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion jd_seckill/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion jd_seckill/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
66 changes: 66 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion service/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion service/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package service
import (
"github.com/unknwon/goconfig"
"gopkg.in/gomail.v2"
"log"
"github.com/ztino/jd_seckill/log"
"strconv"
)

Expand Down
2 changes: 1 addition & 1 deletion service/wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down

0 comments on commit 9e87050

Please sign in to comment.