-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: write logs to a user directory
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
Showing
5 changed files
with
131 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters