将JSON数据写入JSON文件

时间:2016-06-15 15:30:16

标签: json file laravel logging laravel-5.2

我的应用程序中的Hello我正在尝试以.json格式创建自己的自定义日志文件。原因是我需要一个结构良好且准确的日志文件,该文件易于阅读,并且不依赖于我的应用程序中的某些特殊代码来读取数据。

我已经能够创建一个json文件:activities.json 我已经能够使用File::append($path, $json)

编写并附加到该文件

这是文件内容的示例:

{"controller":"TestController","function":"testFunction()","model":"Test","user":"Kayla","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"}
{"controller":"TestController","function":"testFunction()","model":"Test","user":"Jason","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"}
{"controller":"UserController","function":"userFunction()","model":"User","user":"Jason","description":"Another event occurred","date":"2016-06-15"}

现在我的问题是以上不是有效的JSON。我如何以这种格式获得它:

[
 {"controller":"TestController","function":"testFunction()","model":"Test","user":"Kayla","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"},
    {"controller":"TestController","function":"testFunction()","model":"Test","user":"Jason","description":"Something happened!! Saving some JSON data here!","date":"2016-06-15"},
    {"controller":"UserController","function":"userFunction()","model":"User","user":"Jason","description":"Another event occurred","date":"2016-06-15"}
]

有没有办法在laravel中编写和附加到json文件?我希望尽可能避免在追加之前读取整个文件并进行搜索和替换,因为该文件可能包含数百到数千条记录。

我不会使用默认的Laravel Log功能。

0 个答案:

没有答案