perf: update log level

main
zggsong 2 years ago
parent 25750ee61b
commit a964630a70

@ -26,16 +26,16 @@ func InitialConfig() model.Config {
viper.AddConfigPath("../config") viper.AddConfigPath("../config")
viper.AddConfigPath(filePath) viper.AddConfigPath(filePath)
viper.OnConfigChange(func(e fsnotify.Event) { viper.OnConfigChange(func(e fsnotify.Event) {
log.Printf("[INFO] 配置文件更新: %v", e.Name) log.Tracef("配置文件更新: %v", e.Name)
global.GLO_CONFIG_CHAN <- getConfig() global.GLO_CONFIG_CHAN <- getConfig()
}) })
viper.WatchConfig() viper.WatchConfig()
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {
log.Println("[ERROR] 未找到配置,请先添加配置") log.Errorf("未找到配置,请先添加配置")
os.Exit(1) os.Exit(1)
} }
log.Printf("[INFO] Loaded Config Success...") log.Infof("Loaded Config Success...")
return getConfig() return getConfig()
} }

@ -27,7 +27,7 @@ func init() {
log.SetFormatter(&log.TextFormatter{ log.SetFormatter(&log.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05", TimestampFormat: "2006-01-02 15:04:05",
}) })
log.Printf("[INFO] ======") log.Infof("======")
// 初始化配置文件 // 初始化配置文件
global.GLO_CONFIG = config.InitialConfig() global.GLO_CONFIG = config.InitialConfig()
@ -39,7 +39,7 @@ func init() {
log.Fatal("[ERROR] Could not create upload directory...") log.Fatal("[ERROR] Could not create upload directory...")
return return
} }
log.Printf("[INFO] Created Upload Directory") log.Infof("Created Upload Directory")
} }
} }
@ -55,7 +55,7 @@ func main() {
// 监听每日申报 // 监听每日申报
go chanHandler() go chanHandler()
log.Printf("[INFO] Service Listener Port At 7201...") log.Infof("Service Listener Port At 7201...")
err := http.ListenAndServe(":7201", mux) err := http.ListenAndServe(":7201", mux)
if err != nil { if err != nil {
log.Fatalf("[FATAL] Start Server Error %s", err) log.Fatalf("[FATAL] Start Server Error %s", err)

@ -23,7 +23,7 @@ func initBark() {
} }
func (m Bark) Send(message Body) { func (m Bark) Send(message Body) {
log.Println("[bark] Sending by bark...") log.Infof("[bark] Sending by bark...")
var reqBody = Request{ var reqBody = Request{
DeviceKey: b.key, DeviceKey: b.key,
Title: message.Title, Title: message.Title,
@ -37,7 +37,7 @@ func (m Bark) Send(message Body) {
log.Fatalf("[bark] http post failed: %v\n", err) log.Fatalf("[bark] http post failed: %v\n", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Printf("[bark] Send successful") log.Infof("[bark] Send successful")
} }
type Request struct { type Request struct {

@ -33,7 +33,7 @@ func initMail() {
} }
func (m Mail) Send(message Body) { func (m Mail) Send(message Body) {
log.Println("[mail] Sending by mail...") log.Infof("[mail] Sending by mail...")
e := email.NewEmail() e := email.NewEmail()
e.From = m.FromName e.From = m.FromName
e.To = m.To e.To = m.To
@ -44,5 +44,5 @@ func (m Mail) Send(message Body) {
if err != nil { if err != nil {
log.Fatalf("[mail] Send failed: %v\n", err) log.Fatalf("[mail] Send failed: %v\n", err)
} }
log.Printf("[mail] Send successful") log.Infof("[mail] Send successful")
} }

@ -54,14 +54,14 @@ https://github.com/zggsong`))
// 根据字段名获取表单文件 // 根据字段名获取表单文件
formFile, _, err := r.FormFile("file") formFile, _, err := r.FormFile("file")
if err != nil { if err != nil {
log.Printf("[ERROR] (HandlerSingleFile) Get form file failed: %s", err) log.Errorf("(HandlerSingleFile) Get form file failed: %s", err)
return return
} }
defer formFile.Close() defer formFile.Close()
// 创建保存文件 // 创建保存文件
destFile, err := os.Create("./upload/" + fn) destFile, err := os.Create("./upload/" + fn)
if err != nil { if err != nil {
log.Printf("[ERROR] (HandlerSingleFile) Create Save failed: %s", err) log.Errorf("(HandlerSingleFile) Create Save failed: %s", err)
return return
} }
defer destFile.Close() defer destFile.Close()
@ -69,10 +69,10 @@ https://github.com/zggsong`))
// 读取表单文件,写入保存文件 // 读取表单文件,写入保存文件
_, err = io.Copy(destFile, formFile) _, err = io.Copy(destFile, formFile)
if err != nil { if err != nil {
log.Printf("[ERROR] (HandlerSingleFile) Write file failed: %s", err) log.Errorf("(HandlerSingleFile) Write file failed: %s", err)
return return
} }
log.Printf("[INFO] Have Receive File: %v", fn) //输出上传的文件名 log.Infof("Have Receive File: %v", fn) //输出上传的文件名
str, _ := os.Getwd() str, _ := os.Getwd()
dic := fmt.Sprintf("%v/upload/", str) dic := fmt.Sprintf("%v/upload/", str)
if fn == "xcm.jpeg" { if fn == "xcm.jpeg" {
@ -95,7 +95,7 @@ func DeclarationService(files map[string]string) {
//登陆获取auth //登陆获取auth
ticket, userStr, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord) ticket, userStr, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord)
if ticket == "" || err != nil { if ticket == "" || err != nil {
log.Printf("ticket: %v, err:%v", ticket, err.Error()) log.Errorf("Ticket: %v, err:%v", ticket, err.Error())
util.SendMessageError(err) util.SendMessageError(err)
return return
} }
@ -111,13 +111,13 @@ func DeclarationService(files map[string]string) {
imagesLinks, err := core.Upload2Azure(ticket, userStr, m) imagesLinks, err := core.Upload2Azure(ticket, userStr, m)
//log.Printf("[DEBUG] get images links: %s", imagesLinks) //log.Printf("[DEBUG] get images links: %s", imagesLinks)
if err != nil { if err != nil {
log.Printf(err.Error()) log.Errorf(err.Error())
util.SendMessageError(err) util.SendMessageError(err)
return return
} }
if imagesLinks == nil { if imagesLinks == nil {
err = errors.New("[ERROR] Get no images links") err = errors.New("[ERROR] Get no images links")
log.Printf(err.Error()) log.Errorf(err.Error())
util.SendMessageError(err) util.SendMessageError(err)
return return
} }
@ -126,7 +126,7 @@ func DeclarationService(files map[string]string) {
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
err = core.EpidemicRegistration(ticket, userStr, imagesLinks) err = core.EpidemicRegistration(ticket, userStr, imagesLinks)
if err != nil && i > 1 { if err != nil && i > 1 {
log.Printf("重试3次失败%v", err.Error()) log.Errorf("重试3次失败%v", err.Error())
util.SendMessageError(err) util.SendMessageError(err)
return return
} else { } else {
@ -134,20 +134,20 @@ func DeclarationService(files map[string]string) {
} }
} }
log.Printf("[INFO] 每日申报成功") log.Infof("[INFO] 每日申报成功")
//刷新门禁 //刷新门禁
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
err = core.RefreshDoor(ticket, userStr) err = core.RefreshDoor(ticket, userStr)
if err != nil && i > 1 { if err != nil && i > 1 {
log.Printf("重试3次失败%v", err.Error()) log.Errorf("重试3次失败%v", err.Error())
util.SendMessageError(err) util.SendMessageError(err)
return return
} else { } else {
break break
} }
} }
log.Printf("[INFO] 刷新门禁成功") log.Infof("[INFO] 刷新门禁成功")
util.SendSuccess("【成功】每日申报、刷新门禁") util.SendSuccess("【成功】每日申报、刷新门禁")
} }
@ -160,34 +160,34 @@ func DeclarationService(files map[string]string) {
func CompressImageResource(imagePath string) { func CompressImageResource(imagePath string) {
file, err := os.Open(imagePath) file, err := os.Open(imagePath)
if err != nil { if err != nil {
log.Printf("[ERROR] (compressImageResource) Open File failed err: %v", err) log.Errorf("(compressImageResource) Open File failed err: %v", err)
} }
defer file.Close() defer file.Close()
img, _, err := image.Decode(file) img, _, err := image.Decode(file)
if err != nil { if err != nil {
log.Printf("[ERROR] (compressImageResource) Decode error: %v", err) log.Errorf("(compressImageResource) Decode error: %v", err)
} }
buf := bytes.Buffer{} buf := bytes.Buffer{}
quality, err := strconv.Atoi(global.GLO_CONFIG.Quality) quality, err := strconv.Atoi(global.GLO_CONFIG.Quality)
if err != nil { if err != nil {
log.Printf("[ERROR] (compressImageResource) Quality convert from config error: %v", err) log.Errorf("(compressImageResource) Quality convert from config error: %v", err)
} }
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality}) err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
if err != nil { if err != nil {
log.Printf("[ERROR] (compressImageResource) Encode error: %v", err) log.Errorf("(compressImageResource) Encode error: %v", err)
} }
//保存到新文件中 //保存到新文件中
newFile, err := os.Create(imagePath) newFile, err := os.Create(imagePath)
if err != nil { if err != nil {
log.Printf("[ERROR] (compressImageResource) Create Compress File failed err: %v", err) log.Errorf("(compressImageResource) Create Compress File failed err: %v", err)
} }
defer newFile.Close() defer newFile.Close()
_, err = newFile.Write(buf.Bytes()) _, err = newFile.Write(buf.Bytes())
if err != nil { if err != nil {
log.Printf("[ERROR] (compressImageResource) Write Compress File failed err: %v", err) log.Errorf("(compressImageResource) Write Compress File failed err: %v", err)
} else { } else {
log.Printf("[INFO] Compress %v success", imagePath) log.Infof("Compress %v success", imagePath)
} }
} }
@ -221,7 +221,7 @@ func HandlerMultiFiles(w http.ResponseWriter, r *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
io.Copy(destFile, srcFile) io.Copy(destFile, srcFile)
log.Printf("接收文件: %v", files[i].Filename) //输出上传的文件名 log.Infof("接收文件: %v", files[i].Filename) //输出上传的文件名
} }
w.Write([]byte("successfully")) //这个写入到w的是输出到客户端的 w.Write([]byte("successfully")) //这个写入到w的是输出到客户端的
} }

Loading…
Cancel
Save