将日志文件数据转换为Vue客户端中的JSON对象

huangapple go评论127阅读模式
英文:

Convert log File Data to Json Object in Vue client

问题

我有一个如下所示的日志文件:

  1. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Route.go:74[IN : GetLatestLogs]"}
  2. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Service.go:40[IN : GetRecentServerErrorLogService]"}
  3. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"DAO.go:117[IN : GetRecentServerErrorLogDAO]"}
  4. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"DAO.go:148[OUT : GetRecentServerErrorLogDAO]"}
  5. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Service.go:47[OUT : GetRecentServerErrorLogService]"}
  6. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Route.go:79[OUT : GetLatestLogs]"}
  7. {"L":"DEBUG","T":"2021-11-01T17:40:55.331+0530","M":"Route.go:74[IN : GetLatestLogs]"}

我正在Golang的echo服务器中读取这个文件,代码如下:

  1. file, err := os.Open(logFilePath)
  2. stat, _ := os.Stat(logFilePath)
  3. buf := make([]byte, stat.Size())
  4. _, err = file.Read(buf)
  5. serverLog := string(buf)

然后将生成的字符串返回:

  1. return c.JSON(http.StatusOK, serverLog)

这是我得到的结果:

  1. "{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.167+0530\",\"M\":\"Route.go:74[IN : GetLatestLogs]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.167+0530\",\"M\":\"Service.go:40[IN : GetRecentServerErrorLogService]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.167+0530\",\"M\":\"DAO.go:117[IN : GetRecentServerErrorLogDAO]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.168+0530\",\"M\":\"DAO.go:148[OUT : GetRecentServerErrorLogDAO]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.168+0530\",\"M\":\"Service.go:47[OUT : GetRecentServerErrorLogService]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.168+0530\",\"M\":\"Route.go:79[OUT : GetLatestLogs]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:40:55.331+0530\",\"M\":\"Route.go:74[IN : GetLatestLogs]\"}{\"L\":\"DEBUG\",\"T\":\"2021-11-02T09:48:49.982+0530\",\"M\":\"controlPanelRoute.go:74[IN : GetLatestLogs]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-02T09:48:49.982+0530\",\"M\":\"controlPanelService.go:40[IN : GetRecentServerErrorLogService]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-02T09:48:49.982+0530\",\"M\":\"controlPanelDAO.go:117[IN : GetRecentServerErrorLogDAO]\"}\n"

我想将接收到的响应转换为JSON对象。

这是我期望的输出:

  1. [
  2. {
  3. "L": "DEBUG",
  4. "T": "2021-11-01T17:37:54.167+0530",
  5. "M": "Route.go:74[IN : GetLatestLogs]"
  6. },
  7. {
  8. "L": "DEBUG",
  9. "T": "2021-11-01T17:37:54.167+0530",
  10. "M": "Service.go:40[IN : GetRecentServerErrorLogService]"
  11. },
  12. {
  13. "L": "DEBUG",
  14. "T": "2021-11-01T17:37:54.167+0530",
  15. "M": "DAO.go:117[IN : GetRecentServerErrorLogDAO]"
  16. },
  17. {
  18. "L": "DEBUG",
  19. "T": "2021-11-01T17:37:54.168+0530",
  20. "M": "DAO.go:148[OUT : GetRecentServerErrorLogDAO]"
  21. },
  22. {
  23. "L": "DEBUG",
  24. "T": "2021-11-01T17:37:54.168+0530",
  25. "M": "Service.go:47[OUT : GetRecentServerErrorLogService]"
  26. },
  27. {
  28. "L": "DEBUG",
  29. "T": "2021-11-01T17:37:54.168+0530",
  30. "M": "Route.go:79[OUT : GetLatestLogs]"
  31. },
  32. {
  33. "L": "DEBUG",
  34. "T": "2021-11-01T17:40:55.331+0530",
  35. "M": "Route.go:74[IN : GetLatestLogs]"
  36. }
  37. ]
英文:

I have a log file as follow:

  1. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Route.go:74[IN : GetLatestLogs]"}
  2. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Service.go:40[IN : GetRecentServerErrorLogService]"}
  3. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"DAO.go:117[IN : GetRecentServerErrorLogDAO]"}
  4. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"DAO.go:148[OUT : GetRecentServerErrorLogDAO]"}
  5. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Service.go:47[OUT : GetRecentServerErrorLogService]"}
  6. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Route.go:79[OUT : GetLatestLogs]"}
  7. {"L":"DEBUG","T":"2021-11-01T17:40:55.331+0530","M":"Route.go:74[IN : GetLatestLogs]"}

I am reading this file in the Golang echo server as follow:

  1. file, err := os.Open(logFilePath)
  2. stat, _ := os.Stat(logFilePath)
  3. buf := make([]byte, stat.Size())
  4. _, err = file.Read(buf)
  5. serverLog := string(buf)

and return this string generated back

  1. return c.JSON(http.StatusOK, serverLog)

this is what I get as result

  1. "{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.167+0530\",\"M\":\"Route.go:74[IN : GetLatestLogs]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.167+0530\",\"M\":\"Service.go:40[IN : GetRecentServerErrorLogService]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.167+0530\",\"M\":\"DAO.go:117[IN : GetRecentServerErrorLogDAO]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.168+0530\",\"M\":\"DAO.go:148[OUT : GetRecentServerErrorLogDAO]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.168+0530\",\"M\":\"Service.go:47[OUT : GetRecentServerErrorLogService]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:37:54.168+0530\",\"M\":\"Route.go:79[OUT : GetLatestLogs]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-01T17:40:55.331+0530\",\"M\":\"Route.go:74[IN : GetLatestLogs]\"}{\"L\":\"DEBUG\",\"T\":\"2021-11-02T09:48:49.982+0530\",\"M\":\"controlPanelRoute.go:74[IN : GetLatestLogs]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-02T09:48:49.982+0530\",\"M\":\"controlPanelService.go:40[IN : GetRecentServerErrorLogService]\"}\n{\"L\":\"DEBUG\",\"T\":\"2021-11-02T09:48:49.982+0530\",\"M\":\"controlPanelDAO.go:117[IN : GetRecentServerErrorLogDAO]\"}\n"

I want to convert this received response to a JSON object.

This is my desired output:

  1. [
  2. {
  3. "L": "DEBUG",
  4. "T": "2021-11-01T17:37:54.167+0530",
  5. "M": "Route.go:74[IN : GetLatestLogs]"
  6. },
  7. {
  8. "L": "DEBUG",
  9. "T": "2021-11-01T17:37:54.167+0530",
  10. "M": "Service.go:40[IN : GetRecentServerErrorLogService]"
  11. },
  12. {
  13. "L": "DEBUG",
  14. "T": "2021-11-01T17:37:54.167+0530",
  15. "M": "DAO.go:117[IN : GetRecentServerErrorLogDAO]"
  16. },
  17. {
  18. "L": "DEBUG",
  19. "T": "2021-11-01T17:37:54.168+0530",
  20. "M": "DAO.go:148[OUT : GetRecentServerErrorLogDAO]"
  21. },
  22. {
  23. "L": "DEBUG",
  24. "T": "2021-11-01T17:37:54.168+0530",
  25. "M": "Service.go:47[OUT : GetRecentServerErrorLogService]"
  26. },
  27. {
  28. "L": "DEBUG",
  29. "T": "2021-11-01T17:37:54.168+0530",
  30. "M": "Route.go:79[OUT : GetLatestLogs]"
  31. },
  32. {
  33. "L": "DEBUG",
  34. "T": "2021-11-01T17:40:55.331+0530",
  35. "M": "Route.go:74[IN : GetLatestLogs]"
  36. }
  37. ]

答案1

得分: 1

这看起来像是一个接一个的有效的 JSON 语句。你可以打开文件,使用 json.NewDecoder(filehandle) 创建一个解码器,然后一次读取一个 JSON 语句。这里有一个使用硬编码输入的示例:

  1. package main
  2. import (
  3. "fmt"
  4. "bytes"
  5. "io"
  6. "encoding/json"
  7. )
  8. var input = []byte(`{"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Route.go:74[IN : GetLatestLogs]"}
  9. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Service.go:40[IN : GetRecentServerErrorLogService]"}
  10. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"DAO.go:117[IN : GetRecentServerErrorLogDAO]"}
  11. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"DAO.go:148[OUT : GetRecentServerErrorLogDAO]"}
  12. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Service.go:47[OUT : GetRecentServerErrorLogService]"}
  13. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Route.go:79[OUT : GetLatestLogs]"}
  14. {"L":"DEBUG","T":"2021-11-01T17:40:55.331+0530","M":"Route.go:74[IN : GetLatestLogs]"}`)
  15. func main() {
  16. r := json.NewDecoder(bytes.NewBuffer(input))
  17. var data interface{}
  18. for i := 0;;i++{
  19. if err := r.Decode(&data); err != nil {
  20. if err == io.EOF {
  21. break
  22. }
  23. panic(err)
  24. } else {
  25. fmt.Printf("%d: %+v\n", i, data)
  26. }
  27. }
  28. }

输出应该是:

  1. 0: map[L:DEBUG M:Route.go:74[IN : GetLatestLogs] T:2021-11-01T17:37:54.167+0530]
  2. 1: map[L:DEBUG M:Service.go:40[IN : GetRecentServerErrorLogService] T:2021-11-01T17:37:54.167+0530]
  3. 2: map[L:DEBUG M:DAO.go:117[IN : GetRecentServerErrorLogDAO] T:2021-11-01T17:37:54.167+0530]
  4. 3: map[L:DEBUG M:DAO.go:148[OUT : GetRecentServerErrorLogDAO] T:2021-11-01T17:37:54.168+0530]
  5. 4: map[L:DEBUG M:Service.go:47[OUT : GetRecentServerErrorLogService] T:2021-11-01T17:37:54.168+0530]
  6. 5: map[L:DEBUG M:Route.go:79[OUT : GetLatestLogs] T:2021-11-01T17:37:54.168+0530]
  7. 6: map[L:DEBUG M:Route.go:74[IN : GetLatestLogs] T:2021-11-01T17:40:55.331+0530]

正如你所看到的,Decode() 在一个 JSON 表达式的末尾停止,所以你可以一直读取下去。

英文:

That looks like one valid Json statement after another. You can open the file, create a decoder with json.NewDecoder(filehandle), and read one Json statement out if it after another. Heres' an example with the input hard coded:

  1. package main
  2. import (
  3. "fmt"
  4. "bytes"
  5. "io"
  6. "encoding/json"
  7. )
  8. var input =[]byte( `{"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Route.go:74[IN : GetLatestLogs]"}
  9. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"Service.go:40[IN : GetRecentServerErrorLogService]"}
  10. {"L":"DEBUG","T":"2021-11-01T17:37:54.167+0530","M":"DAO.go:117[IN : GetRecentServerErrorLogDAO]"}
  11. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"DAO.go:148[OUT : GetRecentServerErrorLogDAO]"}
  12. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Service.go:47[OUT : GetRecentServerErrorLogService]"}
  13. {"L":"DEBUG","T":"2021-11-01T17:37:54.168+0530","M":"Route.go:79[OUT : GetLatestLogs]"}
  14. {"L":"DEBUG","T":"2021-11-01T17:40:55.331+0530","M":"Route.go:74[IN : GetLatestLogs]"}`)
  15. func main() {
  16. r := json.NewDecoder(bytes.NewBuffer(input))
  17. var data interface{}
  18. for i := 0;;i++{
  19. if err := r.Decode(&data); err != nil {
  20. if err == io.EOF {
  21. break
  22. }
  23. panic(err)
  24. } else {
  25. fmt.Printf("%d: %+v\n", i, data)
  26. }
  27. }
  28. }

Output should be:

  1. 0: map[L:DEBUG M:Route.go:74[IN : GetLatestLogs] T:2021-11-01T17:37:54.167+0530]
  2. 1: map[L:DEBUG M:Service.go:40[IN : GetRecentServerErrorLogService] T:2021-11-01T17:37:54.167+0530]
  3. 2: map[L:DEBUG M:DAO.go:117[IN : GetRecentServerErrorLogDAO] T:2021-11-01T17:37:54.167+0530]
  4. 3: map[L:DEBUG M:DAO.go:148[OUT : GetRecentServerErrorLogDAO] T:2021-11-01T17:37:54.168+0530]
  5. 4: map[L:DEBUG M:Service.go:47[OUT : GetRecentServerErrorLogService] T:2021-11-01T17:37:54.168+0530]
  6. 5: map[L:DEBUG M:Route.go:79[OUT : GetLatestLogs] T:2021-11-01T17:37:54.168+0530]
  7. 6: map[L:DEBUG M:Route.go:74[IN : GetLatestLogs] T:2021-11-01T17:40:55.331+0530]

As you can see, Decode() stops at the end of a Json expression, so you can just keep reading over and over again.

答案2

得分: 1

  1. file, err := os.Open("/log/file/path")
  2. if err != nil {
  3. panic(err)
  4. }
  5. info, err := file.Stat()
  6. if err != nil {
  7. panic(err)
  8. }
  9. logs := make(json.RawMessage, 1, info.Size()+1) // len=1 for '['
  10. dec := json.NewDecoder(file)
  11. for dec.More() {
  12. var log json.RawMessage
  13. if err := dec.Decode(&log); err != nil {
  14. panic(err)
  15. }
  16. logs = append(logs, log...)
  17. logs = append(logs, ',')
  18. }
  19. if n := len(logs); n > 1 {
  20. logs[0], logs[n-1] = '[', ']'
  21. c.JSON(http.StatusOK, logs)
  22. }
英文:
  1. file, err := os.Open("/log/file/path")
  2. if err != nil {
  3. panic(err)
  4. }
  5. info, err := file.Stat()
  6. if err != nil {
  7. panic(err)
  8. }
  9. logs := make(json.RawMessage, 1, info.Size()+1) // len=1 for '['
  10. dec := json.NewDecoder(file)
  11. for dec.More() {
  12. var log json.RawMessage
  13. if err := dec.Decode(&log); err != nil {
  14. panic(err)
  15. }
  16. logs = append(logs, log...)
  17. logs = append(logs, ',')
  18. }
  19. if n := len(logs); n > 1 {
  20. logs[0], logs[n-1] = '[', ']'
  21. c.JSON(http.StatusOK, logs)
  22. }

答案3

得分: 1

从你的代码中看,似乎你依赖Gin来进行转换:

  1. return c.JSON(http.StatusOK, serverLog)

实际上,你可以自己处理它,但可能需要逐行进行解组,因为日志文本文件不是一个有效的JSON数组。然后,我将有效的结构重新编组为JSON。在下面的示例中,我使用bufio逐行读取文本文件,并将其解组为Log结构:

  1. package main
  2. import (
  3. "os"
  4. "fmt"
  5. "encoding/json"
  6. "bufio"
  7. )
  8. type Log struct {
  9. L string
  10. T string
  11. M string
  12. }
  13. func main() {
  14. f, err := os.Open("log.txt")
  15. defer f.Close()
  16. var logs []Log
  17. var log Log
  18. input := bufio.NewScanner(f)
  19. for input.Scan() {
  20. textByte := []byte(input.Text())
  21. err = json.Unmarshal(textByte, &log)
  22. if err != nil {
  23. fmt.Printf("解组出错:%v\n", err)
  24. os.Exit(1)
  25. }
  26. logs = append(logs, log)
  27. }
  28. data, err := json.MarshalIndent(logs, "", " ")
  29. if err != nil {
  30. fmt.Printf("编组出错:%v\n", err)
  31. os.Exit(1)
  32. }
  33. fmt.Printf("%s\n", data)
  34. }

或者你可以从一个函数中返回字符串:

  1. return fmt.Sprintf("%s\n", data)

以下是输出结果:

  1. [
  2. {
  3. "L": "DEBUG",
  4. "T": "2021-11-01T17:37:54.167+0530",
  5. "M": "Route.go:74[IN : GetLatestLogs]"
  6. },
  7. {
  8. "L": "DEBUG",
  9. "T": "2021-11-01T17:37:54.167+0530",
  10. "M": "Service.go:40[IN : GetRecentServerErrorLogService]"
  11. },
  12. {
  13. "L": "DEBUG",
  14. "T": "2021-11-01T17:37:54.167+0530",
  15. "M": "DAO.go:117[IN : GetRecentServerErrorLogDAO]"
  16. },
  17. {
  18. "L": "DEBUG",
  19. "T": "2021-11-01T17:37:54.168+0530",
  20. "M": "DAO.go:148[OUT : GetRecentServerErrorLogDAO]"
  21. },
  22. {
  23. "L": "DEBUG",
  24. "T": "2021-11-01T17:37:54.168+0530",
  25. "M": "Service.go:47[OUT : GetRecentServerErrorLogService]"
  26. },
  27. {
  28. "L": "DEBUG",
  29. "T": "2021-11-01T17:37:54.168+0530",
  30. "M": "Route.go:79[OUT : GetLatestLogs]"
  31. },
  32. {
  33. "L": "DEBUG",
  34. "T": "2021-11-01T17:40:55.331+0530",
  35. "M": "Route.go:74[IN : GetLatestLogs]"
  36. }
  37. ]
英文:

From your code it seems you depend on Gin to do the conversion for you:

  1. return c.JSON(http.StatusOK, serverLog)

You can actually handle it yourself, but it may require Unmarshalling line by line, since the log text file isn't a valid JSON array. Then I marshall the valid structure back into JSON. In the example below I used bufio to read the text file by line, and unmarshall to a Log struct:

  1. package main
  2. import (
  3. "os"
  4. "fmt"
  5. "encoding/json"
  6. "bufio"
  7. )
  8. type Log struct {
  9. L string
  10. T string
  11. M string
  12. }
  13. func main() {
  14. f, err := os.Open("log.txt")
  15. defer f.Close()
  16. var logs []Log
  17. var log Log
  18. input := bufio.NewScanner(f)
  19. for input.Scan() {
  20. textByte := []byte(input.Text())
  21. err = json.Unmarshal(textByte, &log)
  22. if err != nil {
  23. fmt.Printf("Problems with unmarshalling: %v\n", err)
  24. os.Exit(1)
  25. }
  26. logs = append(logs, log)
  27. }
  28. data, err := json.MarshalIndent(logs, "", " ")
  29. if err != nil {
  30. fmt.Printf("Error in marshalling: %v\n", err)
  31. os.Exit(1)
  32. }
  33. fmt.Printf("%s\n", data)
  34. }

Or you can return the string from a function:

  1. return fmt.Sprintf("%s\n", data)

Here's the output:

  1. [
  2. {
  3. "L": "DEBUG",
  4. "T": "2021-11-01T17:37:54.167+0530",
  5. "M": "Route.go:74[IN : GetLatestLogs]"
  6. },
  7. {
  8. "L": "DEBUG",
  9. "T": "2021-11-01T17:37:54.167+0530",
  10. "M": "Service.go:40[IN : GetRecentServerErrorLogService]"
  11. },
  12. {
  13. "L": "DEBUG",
  14. "T": "2021-11-01T17:37:54.167+0530",
  15. "M": "DAO.go:117[IN : GetRecentServerErrorLogDAO]"
  16. },
  17. {
  18. "L": "DEBUG",
  19. "T": "2021-11-01T17:37:54.168+0530",
  20. "M": "DAO.go:148[OUT : GetRecentServerErrorLogDAO]"
  21. },
  22. {
  23. "L": "DEBUG",
  24. "T": "2021-11-01T17:37:54.168+0530",
  25. "M": "Service.go:47[OUT : GetRecentServerErrorLogService]"
  26. },
  27. {
  28. "L": "DEBUG",
  29. "T": "2021-11-01T17:37:54.168+0530",
  30. "M": "Route.go:79[OUT : GetLatestLogs]"
  31. },
  32. {
  33. "L": "DEBUG",
  34. "T": "2021-11-01T17:40:55.331+0530",
  35. "M": "Route.go:74[IN : GetLatestLogs]"
  36. }
  37. ]

huangapple
  • 本文由 发表于 2021年11月2日 12:42:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/69805480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定