Skip to content

Instantly share code, notes, and snippets.

@danieldiekmeier
Created October 29, 2018 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldiekmeier/e45542bf8d47942e91b202573ad8deee to your computer and use it in GitHub Desktop.
Save danieldiekmeier/e45542bf8d47942e91b202573ad8deee to your computer and use it in GitHub Desktop.
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