diff --git a/.gitignore b/.gitignore index 317f937..cc5695d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .idea .DS_Store - +log/ *.log *.yml \ No newline at end of file diff --git a/config/config.go b/common/config.go similarity index 98% rename from config/config.go rename to common/config.go index 2a4b0aa..663d23c 100644 --- a/config/config.go +++ b/common/config.go @@ -1,4 +1,4 @@ -package config +package common import ( "github.com/fsnotify/fsnotify" diff --git a/config/config_test.go b/common/config_test.go similarity index 96% rename from config/config_test.go rename to common/config_test.go index 08695a6..78696b7 100644 --- a/config/config_test.go +++ b/common/config_test.go @@ -1,4 +1,4 @@ -package config +package common import ( "sendmsg/global" diff --git a/common/logger.go b/common/logger.go new file mode 100644 index 0000000..df261ad --- /dev/null +++ b/common/logger.go @@ -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) + } + } +} diff --git a/go.mod b/go.mod index a096e79..598d4ad 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/fsnotify/fsnotify v1.6.0 github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible github.com/spf13/viper v1.15.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( diff --git a/go.sum b/go.sum index 261cb2a..191b1df 100644 --- a/go.sum +++ b/go.sum @@ -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/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= 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.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index c1296fc..243ba47 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "log" - "sendmsg/config" + "sendmsg/common" "sendmsg/global" "sendmsg/message" "sendmsg/model" @@ -11,8 +11,12 @@ import ( ) func init() { + /*日志初始化*/ + common.InitLog() + + /*配置初始化*/ global.GLO_CONF_CH = make(chan model.Config) - _conf, err := config.InitConfig() + _conf, err := common.InitConfig() if err != nil { log.Fatalf("[init] Failed to initialize config: %v", err) } @@ -23,7 +27,7 @@ func main() { go func() { var count int for { - if count > 5 { + if count > 1 { return } count++ @@ -32,7 +36,7 @@ func main() { Content: "this is content, time is " + time.Now().Format("2006-01-02 15:04:05"), }} s.Run() - time.Sleep(time.Second * 2) + time.Sleep(time.Second * 3) } }() diff --git a/message/message_test.go b/message/message_test.go index e2b3ee2..393a5c5 100644 --- a/message/message_test.go +++ b/message/message_test.go @@ -2,14 +2,14 @@ package message import ( "log" - "sendmsg/config" + "sendmsg/common" "sendmsg/global" "testing" "time" ) func TestSend(t *testing.T) { - conf, err := config.InitConfig() + conf, err := common.InitConfig() if err != nil { log.Println("[test] Failed to initialize config: ", err) } diff --git a/service/service_test.go b/service/service_test.go index c96b8ee..e273743 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -1,7 +1,7 @@ package service import ( - "sendmsg/config" + "sendmsg/common" "sendmsg/global" "sendmsg/message" "testing" @@ -9,7 +9,7 @@ import ( ) func TestService(t *testing.T) { - conf, err := config.InitConfig() + conf, err := common.InitConfig() if err != nil { t.Fatalf("Failed to initialize config: %v", err) }