zhangzengfei
2024-03-12 ccb6685b1b28dcd7d376887504cd98b5cd410b44
添加日志打印
3个文件已修改
64 ■■■■ 已修改文件
config/config.go 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/server.go 17 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
config/config.go
@@ -7,12 +7,22 @@
)
type common struct {
    EsUrl     string `mapstructure: "esUrl"`
    ServerUrl string `mapstructure: "serverUrl"`
    OrgName   string `mapstructure: "orgName"`
    EsUrl     string `mapstructure:"esUrl"`
    ServerUrl string `mapstructure:"serverUrl"`
    OrgName   string `mapstructure:"orgName"`
    Interval  int    `mapstructure:"pushInterval"`
}
type logConfig struct {
    Path       string `mapstructure:"path"`       //日志存储路径
    Level      int    `mapstructure:"level"`      //日志等级
    MaxSize    int    `mapstructure:"maxSize"`    //日志文件大小上限
    MaxBackups int    `mapstructure:"maxBackups"` //日志压缩包个数
    MaxAge     int    `mapstructure:"maxAge"`     //保留压缩包天数
}
var Options = &common{}
var LogConf = &logConfig{}
func Init() {
    var err error
@@ -27,6 +37,8 @@
    }
    viper.UnmarshalKey("common", Options)
    viper.UnmarshalKey("log", LogConf)
    viper.WatchConfig()
    viper.OnConfigChange(func(in fsnotify.Event) {
        viper.UnmarshalKey("common", Options)
main.go
@@ -6,6 +6,8 @@
    "fmt"
    "time"
    "basic.com/valib/logger.git"
)
func init() {
@@ -14,22 +16,25 @@
func main() {
    done := make(chan bool)
    // 日志初始化
    var logFile = config.LogConf.Path + "esSyncClient.log"
    logger.InitLogger(logFile, config.LogConf.Level, config.LogConf.MaxSize, config.LogConf.MaxBackups, config.LogConf.MaxAge)
    go runEvery(done)
    <-done
    fmt.Println("程序已退出")
    logger.Debug("程序已退出")
}
func runEvery(done chan<- bool) {
    ticker := time.NewTicker(10 * time.Second)
    ticker := time.NewTicker(time.Duration(config.Options.Interval) * time.Second)
    defer ticker.Stop()
    for {
        select {
        case <-ticker.C:
            fmt.Println("开始执行函数。。。")
            logger.Debug("开始执行推送任务")
            doServer()
            fmt.Println("本次执行完毕, 当前所有数据已发送!!!")
            logger.Debug("本次执行完毕, 当前所有数据已发送!!!")
            time.Sleep(10 * time.Second)
        }
    }
@@ -40,32 +45,36 @@
    for {
        t, err := service.GetTotal()
        if err != nil {
            fmt.Println(err)
            logger.Error(err.Error())
            return
        }
        total = t
        fmt.Println("未发送的数据总量:", total)
        logger.Debug("未发送的数据总量:", total)
        if total == 0 {
            break
        } else {
            for {
                connectStatus := service.ConnectControl()
                fmt.Println("connectStatus: ", connectStatus)
                logger.Debug("服务端连接状态connectStatus: ", connectStatus)
                if connectStatus == true {
                    url := config.Options.ServerUrl
                    result, err1 := service.GetData()
                    if err1 != nil {
                        fmt.Println(err1)
                        logger.Warn(err1.Error())
                        break
                    }
                    id, errs2 := service.SendData(result, url)
                    fmt.Println("data id is: ", id)
                    logger.Debug("推送的数据 id: ", id)
                    if errs2 == nil {
                        //delStatus := service.DeleteData(id)
                        markStatus := service.MarkData(id)
                        fmt.Println(markStatus)
                        logger.Debug("记录推送状态 markStatus: ", markStatus)
                    }
                    break
                } else {
                    fmt.Println("5秒后尝试重新连接。。。")
                    logger.Debug("服务端连接失败,5秒后尝试重新连接。。。")
                    time.Sleep(5 * time.Second)
                }
            }
service/server.go
@@ -2,11 +2,13 @@
import (
    "basic.com/pubsub/esutil.git"
    "basic.com/valib/logger.git"
    "bytes"
    "data_msg_push_server/config"
    "data_msg_push_server/model"
    "data_msg_push_server/util"
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
@@ -107,14 +109,19 @@
            for _, picMaxUrl := range source[0]["picMaxUrl"].([]interface{}) {
                picMaxImageData, err := GetImageData("http://" + picMaxUrl.(string))
                if err != nil {
                    fmt.Println("获取大图数据失败:", err)
                    logger.Warn("获取大图数据失败:", err.Error())
                    continue
                }
                picMaxImages = append(picMaxImages, picMaxImageData)
            }
            fmt.Println("图片数据数组大小:", len(picMaxImages))
        }
    }
    if len(picMaxImages) == 0 {
        return nil, errors.New("获取大图失败, id:" + source[0]["id"].(string))
    }
    //fmt.Println("图片数据数组大小:", len(picMaxImages))
    picSmImages := make([][]byte, 0) // 存储图片数据的数组
    if source[0]["targetInfo"].(interface{}) != nil {
@@ -126,13 +133,17 @@
            }
            picSmImageData, err := GetImageData("http://" + picSmUrl)
            if err != nil {
                fmt.Println("获取图片数据失败:", err)
                fmt.Println("获取小图数据失败:", err)
                continue
            }
            picSmImages = append(picSmImages, picSmImageData)
        }
    }
    if len(picSmImages) == 0 {
        return nil, errors.New("获取小图数据失败, id:" + source[0]["id"].(string))
    }
    // 上报机构名称
    source[0]["orgName"] = config.Options.OrgName