-
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -4565,9 +4569,9 @@ [email protected]: | |
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" | ||
|