带有日志系统的

This commit is contained in:
giaogiao 2023-07-28 15:26:22 +08:00
parent 81cf3e7b64
commit 75079e8c9b
5 changed files with 67 additions and 7 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module hello
go 1.20
require (
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/parnurzeal/gorequest v0.2.16 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.uber.org/atomic v1.11.0 // indirect

2
go.sum
View File

@ -1,4 +1,6 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/parnurzeal/gorequest v0.2.16 h1:T/5x+/4BT+nj+3eSknXmCTnEVGSzFzPGdpqmUVVZXHQ=
github.com/parnurzeal/gorequest v0.2.16/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

0
logs/test.log Normal file
View File

18
main.go
View File

@ -2,7 +2,7 @@
* @Author: giaogiao giaogiao
* @Date: 2023-07-28 08:53:13
* @LastEditors: giaogiao giaogiao
* @LastEditTime: 2023-07-28 15:10:40
* @LastEditTime: 2023-07-28 15:23:27
* @FilePath: \go\main.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
@ -11,13 +11,13 @@ package main
import (
"encoding/json"
"fmt"
"hello/setup"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/parnurzeal/gorequest"
"go.uber.org/zap"
)
func read_a_file() {
@ -50,21 +50,25 @@ func read_a_file() {
}
}
func text_url(remarks string, country string, port int) {
sugar := zap.NewExample().Sugar()
sugar.Infof("start_testing country:%s,port:%d", country, port)
setup.CwLog().Infof("start_testing country:%s,port:%d", country, port)
request := gorequest.New().Proxy("socks5://23.224.102.3:" + strconv.Itoa(port))
resp, body, errs := request.Get("https://ip.shagain.club/").End()
if errs != nil {
// fmt.Print(errs)
sugar.Errorf("start_testing country:%s,port:%d,error:%s", country, port, errs)
sugar.Infof("start_switching_ip country:%s,port:%d", country, port)
setup.CwLog().Errorf("start_testing country:%s,port:%d,error:%s", country, port, errs)
setup.CwLog().Infof("start_switching_ip country:%s,port:%d", country, port)
// resp, err := http.Get("http://127.0.0.1:9049/v1/ips?num=1&country=" + country + "&state=all&city=all&zip=all&t=txt&port=" + strconv.Itoa(port) + "&isp=all&start=&end=")
// sugar.Infof("switch_ip_to_return resp:%s,err:%s", resp, err)
}
sugar.Infof("requestToReturnTheResult resp:%s,body:%s", resp, body)
setup.CwLog().Infof("requestToReturnTheResult resp:%s,body:%s", resp, body)
setup.CwLog().Infof("the_current_port_is_fine port:%d", port)
}
func init() {
setup.InitLogger() //初始化log
}
func main() {
setup.CwLog().Warn("start") //测试log
for {
time.Sleep(5000)
read_a_file()

53
setup/CwLogger.go Normal file
View File

@ -0,0 +1,53 @@
/*
* @Author: giaogiao giaogiao
* @Date: 2023-07-28 15:15:11
* @LastEditors: giaogiao giaogiao
* @LastEditTime: 2023-07-28 15:15:20
* @FilePath: \go\setup\CwLogger.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
package setup
import (
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.SugaredLogger
/*
setJSONEncoder 设置logger编码
*/
func setJSONEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder //转换编码的时间戳
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder //编码级别调整为大写的级别输出
return zapcore.NewConsoleEncoder(encoderConfig)
}
/*
setLoggerWrite 设置logger写入文件
*/
func setLoggerWrite() zapcore.WriteSyncer {
l := &lumberjack.Logger{
Filename: "./logs/test.log",
MaxSize: 1,
MaxBackups: 1,
MaxAge: 30,
Compress: true,
LocalTime: true,
}
return zapcore.AddSync(l)
}
/*
InitLogger 初始化 logger
*/
func InitLogger() {
core := zapcore.NewCore(setJSONEncoder(), setLoggerWrite(), zap.InfoLevel)
logger = zap.New(core, zap.AddCaller()).Sugar()
}
func CwLog() *zap.SugaredLogger {
return logger
}