I have a log file as follow:
{"L":"DEBUG","T":"2021-11-01T17:37:54.167 0530","M":"Route.go:74[IN : GetLatestLogs]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.167 0530","M":"Service.go:40[IN : GetRecentServerErrorLogService]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.167 0530","M":"DAO.go:117[IN : GetRecentServerErrorLogDAO]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.168 0530","M":"DAO.go:148[OUT : GetRecentServerErrorLogDAO]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.168 0530","M":"Service.go:47[OUT : GetRecentServerErrorLogService]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.168 0530","M":"Route.go:79[OUT : GetLatestLogs]"}
{"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:
file, err := os.Open(logFilePath)
stat, _ := os.Stat(logFilePath)
buf := make([]byte, stat.Size())
_, err = file.Read(buf)
serverLog := string(buf)
and return this string generated back
return c.JSON(http.StatusOK, serverLog)
this is what I get as result
"{\"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:
[
{
"L": "DEBUG",
"T": "2021-11-01T17:37:54.167 0530",
"M": "Route.go:74[IN : GetLatestLogs]"
},
{
"L": "DEBUG",
"T": "2021-11-01T17:37:54.167 0530",
"M": "Service.go:40[IN : GetRecentServerErrorLogService]"
},
{
"L": "DEBUG",
"T": "2021-11-01T17:37:54.167 0530",
"M": "DAO.go:117[IN : GetRecentServerErrorLogDAO]"
},
{
"L": "DEBUG",
"T": "2021-11-01T17:37:54.168 0530",
"M": "DAO.go:148[OUT : GetRecentServerErrorLogDAO]"
},
{
"L": "DEBUG",
"T": "2021-11-01T17:37:54.168 0530",
"M": "Service.go:47[OUT : GetRecentServerErrorLogService]"
},
{
"L": "DEBUG",
"T": "2021-11-01T17:37:54.168 0530",
"M": "Route.go:79[OUT : GetLatestLogs]"
},
{
"L": "DEBUG",
"T": "2021-11-01T17:40:55.331 0530",
"M": "Route.go:74[IN : GetLatestLogs]"
}
]
CodePudding user response:
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:
package main
import (
"fmt"
"bytes"
"io"
"encoding/json"
)
var input =[]byte( `{"L":"DEBUG","T":"2021-11-01T17:37:54.167 0530","M":"Route.go:74[IN : GetLatestLogs]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.167 0530","M":"Service.go:40[IN : GetRecentServerErrorLogService]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.167 0530","M":"DAO.go:117[IN : GetRecentServerErrorLogDAO]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.168 0530","M":"DAO.go:148[OUT : GetRecentServerErrorLogDAO]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.168 0530","M":"Service.go:47[OUT : GetRecentServerErrorLogService]"}
{"L":"DEBUG","T":"2021-11-01T17:37:54.168 0530","M":"Route.go:79[OUT : GetLatestLogs]"}
{"L":"DEBUG","T":"2021-11-01T17:40:55.331 0530","M":"Route.go:74[IN : GetLatestLogs]"}`)
func main() {
r := json.NewDecoder(bytes.NewBuffer(input))
var data interface{}
for i := 0;;i {
if err := r.Decode(&data); err != nil {
if err == io.EOF {
break
}
panic(err)
} else {
fmt.Printf("%d: % v\n", i, data)
}
}
}
Output should be:
0: map[L:DEBUG M:Route.go:74[IN : GetLatestLogs] T:2021-11-01T17:37:54.167 0530]
1: map[L:DEBUG M:Service.go:40[IN : GetRecentServerErrorLogService] T:2021-11-01T17:37:54.167 0530]
2: map[L:DEBUG M:DAO.go:117[IN : GetRecentServerErrorLogDAO] T:2021-11-01T17:37:54.167 0530]
3: map[L:DEBUG M:DAO.go:148[OUT : GetRecentServerErrorLogDAO] T:2021-11-01T17:37:54.168 0530]
4: map[L:DEBUG M:Service.go:47[OUT : GetRecentServerErrorLogService] T:2021-11-01T17:37:54.168 0530]
5: map[L:DEBUG M:Route.go:79[OUT : GetLatestLogs] T:2021-11-01T17:37:54.168 0530]
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.
CodePudding user response:
file, err := os.Open("/log/file/path")
if err != nil {
panic(err)
}
info, err := file.Stat()
if err != nil {
panic(err)
}
logs := make(json.RawMessage, 1, info.Size() 1) // len=1 for '['
dec := json.NewDecoder(file)
for dec.More() {
var log json.RawMessage
if err := dec.Decode(&log); err != nil {
panic(err)
}
logs = append(logs, log...)
logs = append(logs, ',')
}
if n := len(logs); n > 1 {
logs[0], logs[n-1] = '[', ']'
c.JSON(http.StatusOK, logs)
}