package main import ( "bufio" "encoding/json" "fmt" "github.com/tealeg/xlsx" "log" "os" "strings" ) // LogEntry 表示每行日志记录的结构 type DataD struct { File string `json:"file"` Func string `json:"func"` Level string `json:"level"` Msg string `json:"msg"` Time string `json:"time"` } // LogEntry 表示日志记录的结构 type LogEntry struct { Content string `json:"__CONTENT__"` } type RealData struct { Uid string Ip string Lang string Timezone string Country string CountryCode string Imei string Device string Version string Carrier string Vpn string Time string } func main() { // 打开文件 file, err := os.Open("log.json") if err != nil { log.Fatalf("无法打开文件: %v", err) } defer file.Close() var res []RealData scanner := bufio.NewScanner(file) for scanner.Scan() { // 读取每一行 line := scanner.Text() // 解析JSON var entry LogEntry err := json.Unmarshal([]byte(line), &entry) if err != nil { log.Printf("解析JSON失败: %v", err) continue } var data DataD json.Unmarshal([]byte(strings.Replace(entry.Content, "\\", "", -1)), &data) // 打印解析后的结构 realDatas := strings.Split(data.Msg, "--") uid := strings.Split(realDatas[1], ":") ip := strings.Split(realDatas[2], ":") lang := strings.Split(realDatas[3], ":") timezone := strings.Split(realDatas[4], ":") country := strings.Split(realDatas[5], ":") countryCode := strings.Split(realDatas[6], ":") imei := strings.Split(realDatas[7], ":") device := strings.Split(realDatas[8], ":") version := strings.Split(realDatas[9], ":") carrier := strings.Split(realDatas[10], ":") vpn := strings.Split(realDatas[11], ":") fmt.Printf("uid:%v,ip:%v,lang:%v,timezone:%v,country:%v,countryCode:%v,imei:%v,device:%v,version:%v,carrier:%v,vpn:%v,time:%v\n", uid[1], ip[1], lang[1], timezone[1], country[1], countryCode[1], imei[1], device[1], version[1], carrier[1], vpn[1], data.Time) res = append(res, RealData{ Uid: uid[1], Ip: ip[1], Lang: lang[1], Timezone: timezone[1], Country: country[1], CountryCode: countryCode[1], Imei: imei[1], Device: device[1], Version: version[1], Carrier: carrier[1], Vpn: vpn[1], Time: data.Time, }) } if err := scanner.Err(); err != nil { log.Fatalf("读取文件时出错: %v", err) } excelFileName := fmt.Sprintf("./用户地区分析.xlsx") xlFile := xlsx.NewFile() sheet, err := xlFile.AddSheet("wealth") if err != nil { panic(err) } row := sheet.AddRow() c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value, c11.Value, c12.Value = "uid", "ip", "lang", "timezone", "country", "countryCode", "imei", "device", "version", "carrier", "vpn", "time" for _, d := range res { row := sheet.AddRow() c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell() c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value, c11.Value, c12.Value = d.Uid, d.Ip, d.Lang, d.Timezone, d.Country, d.CountryCode, d.Imei, d.Device, d.Version, d.Carrier, d.Vpn, d.Time } _ = xlFile.Save(excelFileName) }