feat: add log

main
ZGGSONG 2 years ago
parent 5cff81ee50
commit 5c4085f29e

@ -3,7 +3,6 @@ package common
import ( import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
"log"
"os" "os"
"sendmsg/global" "sendmsg/global"
"sendmsg/model" "sendmsg/model"
@ -21,7 +20,7 @@ func InitConfig() (model.Config, error) {
viper.AddConfigPath("../config") viper.AddConfigPath("../config")
viper.AddConfigPath(workDir + "/config") viper.AddConfigPath(workDir + "/config")
viper.OnConfigChange(func(e fsnotify.Event) { viper.OnConfigChange(func(e fsnotify.Event) {
log.Printf("[conf] config update: %v\n", e.Name) Log.Printf("[conf] config update: %v\n", e.Name)
global.GLO_CONF_CH <- updateConfig() global.GLO_CONF_CH <- updateConfig()
}) })
viper.WatchConfig() viper.WatchConfig()

@ -1,7 +1,6 @@
package main package main
import ( import (
"log"
"sendmsg/common" "sendmsg/common"
"sendmsg/global" "sendmsg/global"
"sendmsg/message" "sendmsg/message"
@ -18,7 +17,7 @@ func init() {
global.GLO_CONF_CH = make(chan model.Config) global.GLO_CONF_CH = make(chan model.Config)
_conf, err := common.InitConfig() _conf, err := common.InitConfig()
if err != nil { if err != nil {
log.Fatalf("[init] Failed to initialize config: %v", err) common.Log.Fatalf("[init] Failed to initialize config: %v", err)
} }
global.GLO_CONF = _conf global.GLO_CONF = _conf
} }

@ -3,8 +3,8 @@ package message
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"sendmsg/common"
"sendmsg/model" "sendmsg/model"
) )
@ -14,7 +14,7 @@ type Bark struct {
} }
func (b *Bark) Send(body Body) { func (b *Bark) Send(body Body) {
log.Printf("[bark] sending message...") common.Log.Printf("[bark] sending message...")
var reqBody = model.BarkRequest{ var reqBody = model.BarkRequest{
DeviceKey: b.key, DeviceKey: b.key,
Title: body.Title, Title: body.Title,
@ -25,9 +25,9 @@ func (b *Bark) Send(body Body) {
"application/json; charset=utf-8", "application/json; charset=utf-8",
bytes.NewReader(req)) bytes.NewReader(req))
if err != nil { if err != nil {
log.Fatalf("[bark] http post failed: %v\n", err) common.Log.Fatalf("[bark] http post failed: %v\n", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Printf("[bark] Send successful") common.Log.Printf("[bark] Send successful")
} }

@ -3,8 +3,8 @@ package message
import ( import (
"fmt" "fmt"
"github.com/jordan-wright/email" "github.com/jordan-wright/email"
"log"
"net/smtp" "net/smtp"
"sendmsg/common"
) )
type Mail struct { type Mail struct {
@ -18,7 +18,7 @@ type Mail struct {
} }
func (m *Mail) Send(body Body) { func (m *Mail) Send(body Body) {
log.Printf("[mail] sending message...") common.Log.Printf("[mail] sending message...")
e := email.NewEmail() e := email.NewEmail()
e.From = m.FromName e.From = m.FromName
e.To = m.To e.To = m.To
@ -27,7 +27,7 @@ func (m *Mail) Send(body Body) {
addr := fmt.Sprintf("%v:%v", m.Host, m.Port) addr := fmt.Sprintf("%v:%v", m.Host, m.Port)
err := e.Send(addr, smtp.PlainAuth("", m.Username, m.Password, m.Host)) err := e.Send(addr, smtp.PlainAuth("", m.Username, m.Password, m.Host))
if err != nil { if err != nil {
log.Fatalf("[mail] send failed: %v\n", err) common.Log.Fatalf("[mail] send failed: %v\n", err)
} }
log.Printf("[mail] send successful") common.Log.Printf("[mail] send successful")
} }

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

@ -1,7 +1,7 @@
package service package service
import ( import (
"log" "sendmsg/common"
"sendmsg/message" "sendmsg/message"
) )
@ -10,7 +10,7 @@ type Service struct {
} }
func (s Service) Run() { func (s Service) Run() {
log.Printf("[service] start excute...") common.Log.Printf("[service] start excute...")
send(s.Body) send(s.Body)
} }

Loading…
Cancel
Save