writeLog static method
Writes a log entry to a file in JSON Lines format.
The log entry is appended to a file named {type}_logs.jsonl
within the specified
directory path. If type
is not provided in the payload, it defaults to 'simple'.
The payload
parameter must contain a path
key specifying the directory path
and may contain a type
key to determine the log file prefix. Other keys in the
payload represent the log data and are written to the file in JSON format.
This function ensures that the file is opened, the log is written, and the file is properly closed.
Implementation
static Future<void> writeLog(Map<String, dynamic> payload) async {
final path = payload['path'] as String;
final type = payload['type'] ?? 'simple';
final file = File('$path/${type}_logs.jsonl');
final sink = file.openWrite(mode: FileMode.append);
final logCopy = Map.of(payload)
..remove('path')
..remove('type');
sink.writeln(jsonEncode(logCopy));
await sink.flush();
await sink.close();
}