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]"
}
]
From your code it seems you depend on Gin to do the conversion for you:
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:
package main
import (
"os"
"fmt"
"encoding/json"
"bufio"
)
type Log struct {
L string
T string
M string
}
func main() {
f, err := os.Open("log.txt")
defer f.Close()
var logs []Log
var log Log
input := bufio.NewScanner(f)
for input.Scan() {
textByte := []byte(input.Text())
err = json.Unmarshal(textByte, &log)
if err != nil {
fmt.Printf("Problems with unmarshalling: %v\n", err)
os.Exit(1)
}
logs = append(logs, log)
}
data, err := json.MarshalIndent(logs, "", " ")
if err != nil {
fmt.Printf("Error in marshalling: %v\n", err)
os.Exit(1)
}
fmt.Printf("%s\n", data)
}
Or you can return the string from a function:
return fmt.Sprintf("%s\n", data)
Here's the 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]"
}
]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments