Build log

This commit is contained in:
mmcwilliams 2019-03-08 22:31:51 -05:00
parent 87f3bcef6f
commit faeddb84ab
1 changed files with 45 additions and 44 deletions

View File

@ -1,45 +1,46 @@
'use strict' 'use strict';
const { createLogger, format, transports } = require('winston');
const winston = require('winston') const { combine, timestamp, label, printf } = format;
const path = require('path') const path = require('path');
const fs = require('fs-extra') const fs = require('fs-extra');
const os = require('os') const os = require('os');
const logTime = 'MM/DD/YY-HH:mm:ss';
const log = {} let transport;
async function logFile() {
log.file = function () { let exists;
let logPath = path.join(os.homedir(), `/.config/mcopy-cli/`) let logPath = path.join(os.homedir(), `/.config/mcopy-cli/`);
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
logPath = path.join(os.homedir(), `/Library/Logs/mcopy-cli/`) logPath = path.join(os.homedir(), `/Library/Logs/mcopy-cli/`);
} else if (process.platform === 'win32') {
logPath = path.join(os.homedir(), `/AppData/Roaming/mcopy-cli/`)
} }
if (!fs.existsSync(logPath)) { else if (process.platform === 'win32') {
fs.mkdirSync(logPath) logPath = path.join(os.homedir(), `/AppData/Roaming/mcopy-cli/`);
} }
return path.join(logPath, 'mcopy-cli.log') exists = await fs.exists(logPath);
if (!exists) {
await fs.mkdir(logPath);
}
return path.join(logPath, 'mcopy-cli.log');
} }
module.exports = async function (arg) {
log.time = 'MM/DD/YY-HH:mm:ss'
module.exports = function (arg) {
if (arg.quiet) { if (arg.quiet) {
log.transport = { transport = {
info : function () {}, info: function () { },
warn : function () {}, warn: function () { },
error : function () {} error: function () { }
};
} }
} else { else {
log.transport = winston.createLogger({ transport = createLogger({
transports: [ transports: [
new (winston.transports.Console)({ new (transports.Console)({
format: winston.format.simple() format: combine(format.colorize(), format.simple())
}), }),
new (winston.transports.File)({ new (transports.File)({
filename: log.file() filename: await logFile()
}) })
] ]
}) });
} }
return log.transport return transport;
} };
//# sourceMappingURL=index.js.map