Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ typings/
build
tsconfig.tsbuildinfo
.nx/
packages/logging/test/artifacts/
68 changes: 29 additions & 39 deletions packages/logging/test/logging.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');
const PrettyStream = require('@tryghost/pretty-stream');
const GhostLogger = require('../lib/GhostLogger');
const includes = require('lodash/includes');
Expand All @@ -11,6 +12,20 @@ const ElasticSearch = require('@tryghost/elasticsearch').BunyanStream;
const HttpStream = require('@tryghost/http-stream');
const sandbox = sinon.createSandbox();
const {Worker} = require('worker_threads');
const testArtifactsRoot = path.resolve(__dirname, 'artifacts', 'logs');

function prepareTestLogDir(name) {
const dir = path.join(testArtifactsRoot, name);

fs.rmSync(dir, {recursive: true, force: true});
fs.mkdirSync(dir, {recursive: true});

return dir;
}

function getTestLogDir(name) {
return path.join(testArtifactsRoot, name);
}

describe('Logging config', function () {
it('Reads file called loggingrc.js', function () {
Expand Down Expand Up @@ -589,26 +604,8 @@ describe('Logging', function () {
assert.equal(ghostLogger.filename, '{env}');
});

it('file stream should use custom filename template', function () {
const tempDir = './test-logs/';
const rimraf = function (dir) {
if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach(function (file) {
const curPath = dir + '/' + file;
if (fs.lstatSync(curPath).isDirectory()) {
rimraf(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dir);
}
};

// Create temp directory
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, {recursive: true});
}
it('file stream should use custom filename template', async function () {
const tempDir = prepareTestLogDir('custom-filename');

var ghostLogger = new GhostLogger({
domain: 'test.com',
Expand All @@ -621,20 +618,18 @@ describe('Logging', function () {
ghostLogger.info('Test log message');

// Give it a moment to write
setTimeout(function () {
assert.equal(fs.existsSync(tempDir + 'production.log'), true);
assert.equal(fs.existsSync(tempDir + 'production.error.log'), true);
await new Promise((resolve) => {
setTimeout(resolve, 100);
});

assert.equal(fs.existsSync(path.join(tempDir, 'production.log')), true);
assert.equal(fs.existsSync(path.join(tempDir, 'production.error.log')), true);

// Cleanup
rimraf(tempDir);
}, 100);
fs.rmSync(tempDir, {recursive: true, force: true});
});

it('file stream supports built-in rotating-file transport config', function () {
const tempDir = './test-logs-rotation-built-in/';
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, {recursive: true});
}
const tempDir = prepareTestLogDir('rotation-built-in');

const ghostLogger = new GhostLogger({
domain: 'test.com',
Expand All @@ -654,10 +649,7 @@ describe('Logging', function () {
});

it('file stream supports external rotating file library config', function () {
const tempDir = './test-logs-rotation-library/';
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, {recursive: true});
}
const tempDir = prepareTestLogDir('rotation-library');

const ghostLogger = new GhostLogger({
domain: 'test.com',
Expand All @@ -680,10 +672,7 @@ describe('Logging', function () {
});

it('file stream rotation library handles missing rotateExisting option', function () {
const tempDir = './test-logs-rotation-library-default/';
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, {recursive: true});
}
const tempDir = prepareTestLogDir('rotation-library-default');

const ghostLogger = new GhostLogger({
domain: 'test.com',
Expand All @@ -705,7 +694,8 @@ describe('Logging', function () {
});

it('file stream exits early when target directory does not exist', function () {
const badPath = './test-logs-missing-dir/';
const badPath = getTestLogDir('missing-dir');
fs.rmSync(badPath, {recursive: true, force: true});
const ghostLogger = new GhostLogger({
transports: ['file'],
path: badPath
Expand Down
Loading