Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JSONLog
import fsExtra from 'fs-extra'
export default function jsonLog (filepath) {
return async function (data) {
await fsExtra.ensureFile(filepath)
await fsExtra.appendFile(filepath, JSON.stringify(data) + '\n')
}
}
import test from 'ava'
import path from 'path'
import os from 'os'
import fs from 'fs'
import jsonLog from '../json-log.js'
test('writes json to a file', async t => {
const filepath = path.join(os.tmpdir(), 'test-' + Math.random() + '.txt')
const logger = jsonLog(filepath)
await logger({ fields: 'example', problems: [1, 2, 3] })
await logger({ fields: 'example2', problems: [1, 2, 3] })
const file = fs.readFileSync(filepath, 'utf-8')
t.is(file, `{"fields":"example","problems":[1,2,3]}\n{"fields":"example2","problems":[1,2,3]}\n`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment