perf: update struct

main
ZGGSONG 2 years ago
parent 51abe8f14d
commit 5cff81ee50

2
.gitignore vendored

@ -1,5 +1,5 @@
.idea .idea
.DS_Store .DS_Store
log/
*.log *.log
*.yml *.yml

@ -1,4 +1,4 @@
package config package common
import ( import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"

@ -1,4 +1,4 @@
package config package common
import ( import (
"sendmsg/global" "sendmsg/global"

@ -0,0 +1,53 @@
package common
import (
"fmt"
"io"
"log"
"os"
"strconv"
)
var Log *log.Logger
// InitLog
//
// @Description: 初始化原生 log
func InitLog() {
path := "./log"
_, err := os.Stat(path)
if os.IsNotExist(err) {
_ = os.Mkdir(path, 0755)
}
//SavePath := fmt.Sprintf("./log/log_%v.log", time.Now().Format("20060102_150405"))
SavePath := fmt.Sprintf("%v/log.log", path)
printConsole := true
for index, value := range os.Args {
if value == "-log_path" {
index += 1
SavePath = os.Args[index]
} else if value == "-log_ui" {
index += 1
va, err := strconv.Atoi(os.Args[index])
if err != nil {
fmt.Printf("cant parse -log_ui value ,current set value:", os.Args[index])
} else {
if va != 0 {
printConsole = true
} else {
printConsole = false
}
}
}
}
logFile, er := os.OpenFile(SavePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
Log = log.New(logFile, "", log.Ltime|log.Lshortfile|log.LstdFlags)
if er != nil {
fmt.Printf("log file init faild! can't open file:%s ,error msg:%s", SavePath, er)
} else {
if printConsole {
mw := io.MultiWriter(os.Stdout, logFile)
Log.SetOutput(mw)
}
}
}

@ -6,6 +6,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 github.com/fsnotify/fsnotify v1.6.0
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/spf13/viper v1.15.0 github.com/spf13/viper v1.15.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
) )
require ( require (

@ -463,6 +463,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

@ -2,7 +2,7 @@ package main
import ( import (
"log" "log"
"sendmsg/config" "sendmsg/common"
"sendmsg/global" "sendmsg/global"
"sendmsg/message" "sendmsg/message"
"sendmsg/model" "sendmsg/model"
@ -11,8 +11,12 @@ import (
) )
func init() { func init() {
/*日志初始化*/
common.InitLog()
/*配置初始化*/
global.GLO_CONF_CH = make(chan model.Config) global.GLO_CONF_CH = make(chan model.Config)
_conf, err := config.InitConfig() _conf, err := common.InitConfig()
if err != nil { if err != nil {
log.Fatalf("[init] Failed to initialize config: %v", err) log.Fatalf("[init] Failed to initialize config: %v", err)
} }
@ -23,7 +27,7 @@ func main() {
go func() { go func() {
var count int var count int
for { for {
if count > 5 { if count > 1 {
return return
} }
count++ count++
@ -32,7 +36,7 @@ func main() {
Content: "this is content, time is " + time.Now().Format("2006-01-02 15:04:05"), Content: "this is content, time is " + time.Now().Format("2006-01-02 15:04:05"),
}} }}
s.Run() s.Run()
time.Sleep(time.Second * 2) time.Sleep(time.Second * 3)
} }
}() }()

@ -2,14 +2,14 @@ package message
import ( import (
"log" "log"
"sendmsg/config" "sendmsg/common"
"sendmsg/global" "sendmsg/global"
"testing" "testing"
"time" "time"
) )
func TestSend(t *testing.T) { func TestSend(t *testing.T) {
conf, err := config.InitConfig() conf, err := common.InitConfig()
if err != nil { if err != nil {
log.Println("[test] Failed to initialize config: ", err) log.Println("[test] Failed to initialize config: ", err)
} }

@ -1,7 +1,7 @@
package service package service
import ( import (
"sendmsg/config" "sendmsg/common"
"sendmsg/global" "sendmsg/global"
"sendmsg/message" "sendmsg/message"
"testing" "testing"
@ -9,7 +9,7 @@ import (
) )
func TestService(t *testing.T) { func TestService(t *testing.T) {
conf, err := config.InitConfig() conf, err := common.InitConfig()
if err != nil { if err != nil {
t.Fatalf("Failed to initialize config: %v", err) t.Fatalf("Failed to initialize config: %v", err)
} }

Loading…
Cancel
Save