Skip to content

Commit

Permalink
feat: write logs to a user directory
Browse files Browse the repository at this point in the history
Write the log file to a user-specific directory, depending on the OS:
- `~/Library/Logs/DAISY Ace/ace.log` on macOS
- `~\AppData\Local\DAISY Ace\Log\ace.log`  on Windows
- `~/.local/state/DAISY Ace/ace.log` on Linux

Also augment Winston with a `logAndExit` method to propertly flush
the logs to files before exitting (see winstonjs/winston#228).

In the CLI, move the main processing to an `async` function and
wait for a timeout when logging and exitting.

Closes #50.
  • Loading branch information
rdeltour committed Oct 9, 2017
1 parent b41e77b commit 97d2dc9
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 66 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
],
"dependencies": {
"axe-core": "^2.3.1",
"env-paths": "^1.0.0",
"express": "^4.15.5",
"express-easy-zip": "^1.1.4",
"extract-zip": "^1.6.5",
Expand All @@ -64,7 +65,7 @@
"tmp": "0.0.31",
"unzip": "^0.1.11",
"uuidv4": "^0.5.0",
"winston": "^2.3.1",
"winston": "^2.4.0",
"xmldom": "^0.1.27",
"xpath": "^0.0.24"
},
Expand Down
85 changes: 49 additions & 36 deletions src/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,63 @@ const cli = meow(`
string: ['outdir', 'tempdir'],
});

logger.initLogger({verbose: cli.flags.verbose, silent: cli.flags.silent});

// Check that an EPUB path is specified
if (cli.input.length === 0) {
winston.error('Input required');
cli.showHelp(1);
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

// Check that output directories can be overridden
let outdir = cli.flags.outdir;
if (outdir) {
if (cli.flags.subdir) {
outdir = path.join(outdir, path.parse(cli.input[0]).name);
(async function processArgs() {
logger.initLogger({ verbose: cli.flags.verbose, silent: cli.flags.silent });

// Check that an EPUB path is specified
if (cli.input.length === 0) {
winston.logAndExit('error', 'Input required', () => {
console.log(cli.help);
process.exit(1);
});
await sleep(5000);
process.exit(1);
}
if (!cli.flags.force) {
const overrides = ['ace.json', 'report.html', 'data', 'js']
.map(file => path.join(outdir, file))
.filter(fs.existsSync);
if (overrides.length > 0) {
winston.warn(`\

// Check that output directories can be overridden
let outdir = cli.flags.outdir;
if (outdir) {
if (cli.flags.subdir) {
outdir = path.join(outdir, path.parse(cli.input[0]).name);
}
if (!cli.flags.force) {
const overrides = ['ace.json', 'report.html', 'data', 'js']
.map(file => path.join(outdir, file))
.filter(fs.existsSync);
if (overrides.length > 0) {
winston.logAndExit('warn', `\
Output directory is not empty.
Running Ace would override the following files or directories:
Running Ace would override the following files or directories:
${overrides.map(file => ` - ${file}`).join('\n')}
Use option --force to override.
`);
process.exit(1);
Use option --force to override.
`, 1);
await sleep(5000);
process.exit(1);
}
}
}
}

// finally, invoke Ace
ace(cli.input[0], {
cwd: cli.flags.cwd || process.cwd(),
outdir,
tmpdir: cli.flags.tempdir,
verbose: cli.flags.verbose,
silent: cli.flags.silent,
jobId: '',
})
.catch((err) => {
if (err) winston.error(err.message);
console.log(`Re-run Ace using the --verbose option to enable full debug logging.`)
process.exit(1);
});
// finally, invoke Ace
ace(cli.input[0], {
cwd: cli.flags.cwd || process.cwd(),
outdir,
tmpdir: cli.flags.tempdir,
verbose: cli.flags.verbose,
silent: cli.flags.silent,
jobId: '',
})
.catch((err) => {
if (err && err.message) winston.error(err.message);
winston.logAndExit('info', 'Closing logs.', () => {
console.log('Re-run Ace using the --verbose option to enable full debug logging.');
process.exit(1);
});
});
}());
88 changes: 66 additions & 22 deletions src/core/logger.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@
'use strict';

const winston = require('winston');
const envPaths = require('env-paths');
const fs = require('fs-extra');
const LOGFILE = __dirname + "/../ace.log";
const path = require('path');
const winston = require('winston');

module.exports = {
initLogger: function(options) {
// clear old log file
fs.removeSync(LOGFILE);
const acePaths = envPaths('DAISY Ace', { suffix: null });

// set up logger
var level = 'info';
if (options.verbose) {
level = 'verbose';
}
winston.configure({
level: level,
transports: [
new (winston.transports.File)({name: "file", filename: LOGFILE}),
new (winston.transports.Console)({name: "console"})
]
module.exports.initLogger = function initLogger(options = {}) {
// Check logging directoy exists
if (!fs.existsSync(acePaths.log)) {
fs.mkdirSync(acePaths.log);
}

// OS-dependant path to log file
const logfile = path.join(acePaths.log, 'ace.log');

// clear old log file
if (fs.existsSync(logfile)) {
fs.removeSync(logfile);
}

// set up logger
const level = (options.verbose) ? 'verbose' : 'info';
winston.configure({
level,
transports: [
new winston.transports.File({ name: 'file', filename: logfile }),
new winston.transports.Console({ name: 'console' }),
],
});
if (options.silent) {
winston.remove('console');
}
winston.cli();
};

/* eslint-disable no-underscore-dangle */
// Properly wait that loggers write to file before exitting
// See https://github.com/winstonjs/winston/issues/228
winston.logAndExit = function logAndExit(level, msg, codeOrCallback) {
const self = this;
this.log(level, msg, () => {
let numFlushes = 0;
let numFlushed = 0;
Object.keys(self.default.transports).forEach((k) => {
if (self.default.transports[k]._stream) {
numFlushes += 1;
self.default.transports[k]._stream.once('finish', () => {
numFlushed += 1;
if (numFlushes === numFlushed) {
if (typeof codeOrCallback === 'function') {
codeOrCallback();
} else {
process.exit(codeOrCallback);
}
}
});
self.default.transports[k]._stream.end();
}
});
if (options.silent) {
winston.remove("console");
if (numFlushes === 0) {
if (typeof codeOrCallback === 'function') {
codeOrCallback();
} else {
process.exit(codeOrCallback);
}
}
winston.cli();
}
}
});
};
/* eslint-enable no-underscore-dangle */

11 changes: 7 additions & 4 deletions tests/__tests__/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
exports[`test existing output 1`] = `
"warn: Output directory is not empty.
Running Ace would override the following files or directories:
Running Ace would override the following files or directories:
- report/ace.json
- report/report.html
- report/js
Use option --force to override.
Use option --force to override.
"
`;

Expand Down Expand Up @@ -64,7 +64,10 @@ exports[`test no input 2`] = `
"
`;
exports[`test unexisting input 1`] = `"Re-run Ace using the --verbose option to enable full debug logging."`;
exports[`test unexisting input 1`] = `
"info: Closing logs.
Re-run Ace using the --verbose option to enable full debug logging."
`;
exports[`test unexisting input 2`] = `
"error: Couldn’t find EPUB file 'unexisting.epub'
Expand Down
10 changes: 7 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,10 @@ enqueue@^1.0.2:
dependencies:
sliced "0.0.5"

env-paths@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-1.0.0.tgz#4168133b42bb05c38a35b1ae4397c8298ab369e0"

err-code@^1.0.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/err-code/-/err-code-1.1.2.tgz#06e0116d3028f6aef4806849eb0ea6a748ae6960"
Expand Down Expand Up @@ -4565,9 +4569,9 @@ window-size@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"

winston@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/winston/-/winston-2.3.1.tgz#0b48420d978c01804cf0230b648861598225a119"
winston@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/winston/-/winston-2.4.0.tgz#808050b93d52661ed9fb6c26b3f0c826708b0aee"
dependencies:
async "~1.0.0"
colors "1.0.x"
Expand Down

0 comments on commit 97d2dc9

Please sign in to comment.