r/PHPhelp • u/silentheaven83 • Dec 11 '25
Create with Monolog JSON_Formatter a valid JSON file
Hello everybody,
I need your help. I'm trying to use the JsonFormatter of Monolog with this code:
$formatter = new \Monolog\Formatter\JsonFormatter();
$formatter->includeStacktraces();
$streamer = new StreamHandler("json_log.log");
$streamer->setFormatter($formatter);
$logger = new \Monolog\Logger('Channel');
$logger->pushHandler($streamer);I thought that it would create a valid JSON file to parse, but it writes a JSON object that is valid "per line".
Is that a way to make the log file a valid JSON file, for eg.
[
{log_object},
{log_object}
]Thank you!
Edit: Just wanted to thank everybody for your replies, thank you!
2
Upvotes
6
u/Own-Perspective4821 Dec 11 '25
You misunderstood log formatting.
It is not about the log file content format itself, it is about the format that each log entry is formatted in.
Also: If you write consecutive lines in a file without knowing the end, JSON is NOT the format you want your file in.
5
u/allen_jb Dec 11 '25
Newline-delimited JSON (NDJSON, AKA LDJSON or JSONL) is not uncommon in use cases such as logs or streaming data.
Using your desired format could be problematic because the log file would no longer be append-only. You would have to keep inserting new data before the end of the file (or erasing then readding the closing
])It would also mean that, at the time of reading the file (which in some cases may happen part-way through a write operation), if any line is incomplete or otherwise invalid, the entire log file is invalid. With NDJSON, you can simply discard the invalid line and continue reading further lines.
4
u/CyberJack77 Dec 11 '25
Well, that is on purpose. Log files in JSON format (which have a single JSON object per log line) are usually read by other applications that parse each line and do something with the data. It would be highly inefficient if these applications had to re-parse the entire log file over and over again, just to find any additions.
Also, log rotation would become hard (if not impossible) if the entire log file had to be one valid JSON object. Log rotation might be done by Monolog, but also by external systems.
1
u/CommunicationNo2197 Dec 13 '25
Hey! I've run into this exact issue before. The problem is that Mongoose's
_idfield is actually a special BSON ObjectId type, not a plain string. When you try to serialize it to JSON, it doesn't automatically convert to a string format.The easiest fix is to convert the
_idto a string before sending the response:Or if you're dealing with arrays of documents, you can map through them:
Another approach is to use
.lean()on your query which gives you plain JavaScript objects instead of Mongoose documents, but you'll still need to convert the_id:The
.toObject()method is generally the cleanest approach since it handles all the Mongoose-specific stuff and gives you a plain object that's ready for JSON serialization.If you need to validate your JSON output after making these changes, I built a JSON formatter tool that's helpful for checking the structure.
0
u/[deleted] Dec 11 '25
[deleted] — view deleted comment