Writing JSON data to a JSON file

2019-09-12 00:38发布

Hello in my application I am currently trying to create my own custom log files in .json format. Reason for this is because I want a well structured and accurate log file which can be easily read and would not depend on some special code in my application to read the data.

I have been able to create a json file: activities.json I have been able to write and append to that file using File::append($path, $json)

This is a sample of the file contents:

{"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"}

Now my issue is the above is not a valid JSON. How do I get it in this format:

[
 {"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"}
]

Is there a way of writing and appending to a json file in laravel? As much as possible I want to avoid reading the entire file before the append and doing a search and replace since the file may contain hundreds to thousands of records.

I will not use the default Laravel Log function.

0条回答
登录 后发表回答