<commit_before>var path = require('path');
var os = require('os');
var fs = require('fs');
var uuid = require('uuid');
var test = require('tape');
var dateFormat = require('date-format');
var rimraf = require('rimraf');

var captureStdio = require('./lib/capture-stdio.js');
var Logger = require('../logger.js');
var DiskBackend = require('../backends/disk.js');
var ConsoleBackend = require('../backends/console.js');

test('file logging', function (assert) {
    var loc = path.join(os.tmpDir(), uuid());

    var logger = Logger({
        meta: {
            team: 'rt',
            project: 'foobar'
        },
        backends: {
            disk: DiskBackend({
                folder: loc
            })
        }
    });

    assert.ok(logger);
    assert.equal(typeof logger.info, 'function');

    logger.info('some message', {
        some: 'object'
    }, function (err) {
        assert.ifError(err);

        var fileUri = 'rt-foobar.log-' +
            dateFormat('yyyyMMdd');

        fs.readdir(loc, function (err, files) {
            assert.ifError(err);

            assert.deepEqual(files, [fileUri]);

            fs.readFile(path.join(loc, fileUri), function (err, buf) {
                assert.ifError(err);

                buf = String(buf);
                assert.ok(buf.indexOf('some message') !== -1);
                assert.ok(buf.indexOf('some=object') !== -1);

                rimraf(loc, assert.end);
            });
        });
    });
});


test('works with multiple backends', function (assert) {
    var loc = path.join(os.tmpDir(), uuid());

    var logger = Logger({
        meta: {
            team: 'rt',
            project: 'foobar'
        },
        backends: {
            disk: DiskBackend({
                folder: loc
            }),
            console: ConsoleBackend()
        }
    });

    assert.ok(captureStdio('info: hello foo=bar', function () {
        logger.info('hello', { foo: 'bar' }, onlog);
    }));

    function onlog(err) {
        assert.ifError(err);

        var fileUri = path.join(loc, 'rt-foobar.log-' +
            dateFormat('yyyyMMdd'));

        fs.readFile(fileUri, function (err, buf) {
            assert.ifError(err);

            buf = String(buf);

            assert.ok(buf.indexOf('hello') !== -1);
            assert.ok(buf.indexOf('foo=bar') !== -1);

            rimraf(loc, assert.end);
        });
    }
});
<commit_msg>Add a test for FileBackend<commit_after>var path = require('path');
var os = require('os');
var fs = require('fs');
var uuid = require('uuid');
var test = require('tape');
var dateFormat = require('date-format');
var rimraf = require('rimraf');

var captureStdio = require('./lib/capture-stdio.js');
var Logger = require('../logger.js');
var DiskBackend = require('../backends/disk.js');
var FileBackend = require('../backends/file.js');
var ConsoleBackend = require('../backends/console.js');

test('disk logging', function (assert) {
    var loc = path.join(os.tmpDir(), uuid());

    var logger = Logger({
        meta: {
            team: 'rt',
            project: 'foobar'
        },
        backends: {
            disk: DiskBackend({
                folder: loc
            })
        }
    });

    assert.ok(logger);
    assert.equal(typeof logger.info, 'function');

    logger.info('some message', {
        some: 'object'
    }, function (err) {
        assert.ifError(err);

        var fileUri = 'rt-foobar.log-' +
            dateFormat('yyyyMMdd');

        fs.readdir(loc, function (err, files) {
            assert.ifError(err);

            assert.deepEqual(files, [fileUri]);

            fs.readFile(path.join(loc, fileUri), function (err, buf) {
                assert.ifError(err);

                buf = String(buf);
                assert.ok(buf.indexOf('some message') !== -1);
                assert.ok(buf.indexOf('some=object') !== -1);

                rimraf(loc, assert.end);
            });
        });
    });
});

test('file logging', function (assert) {
    var loc = path.join(os.tmpDir(), uuid());

    var logger = Logger({
        meta: {
            team: 'rt',
            project: 'foobar'
        },
        backends: {
            file: FileBackend({
                fileName: path.join(loc, 'rt-foobar.log')
            })
        }
    });

    assert.ok(logger);
    assert.equal(typeof logger.info, 'function');

    logger.info('some message', {
        some: 'object'
    }, function (err) {
        assert.ifError(err);

        var fileUri = 'rt-foobar.log';

        fs.readdir(loc, function (err, files) {
            assert.ifError(err);

            assert.deepEqual(files, [fileUri]);

            fs.readFile(path.join(loc, fileUri), function (err, buf) {
                assert.ifError(err);

                var entry = JSON.parse(buf);
                assert.equal(entry.message, 'some message');
                assert.equal(entry.some, 'object');

                rimraf(loc, assert.end);
            });
        });
    });
});


test('works with multiple backends', function (assert) {
    var loc = path.join(os.tmpDir(), uuid());

    var logger = Logger({
        meta: {
            team: 'rt',
            project: 'foobar'
        },
        backends: {
            disk: DiskBackend({
                folder: loc
            }),
            console: ConsoleBackend()
        }
    });

    assert.ok(captureStdio('info: hello foo=bar', function () {
        logger.info('hello', { foo: 'bar' }, onlog);
    }));

    function onlog(err) {
        assert.ifError(err);

        var fileUri = path.join(loc, 'rt-foobar.log-' +
            dateFormat('yyyyMMdd'));

        fs.readFile(fileUri, function (err, buf) {
            assert.ifError(err);

            buf = String(buf);

            assert.ok(buf.indexOf('hello') !== -1);
            assert.ok(buf.indexOf('foo=bar') !== -1);

            rimraf(loc, assert.end);
        });
    }
});

{'fraction_non_alphanumeric': 0.1665542818610924, 'fraction_numerical': 0.0013486176668914363, 'mean_word_length': 1.8185273159144892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7005091', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1739, 'n_words': 356}