mcopy/app/lib/log/index.js

79 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-03-09 03:31:29 +00:00
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.Log = void 0;
const winston_1 = require("winston");
const path_1 = require("path");
const fs_extra_1 = require("fs-extra");
const os_1 = require("os");
2019-03-09 03:31:29 +00:00
const logTime = 'MM/DD/YY-HH:mm:ss';
/**
* Determine the location of the log file based on the operating system
* and return as an absolute string from os.homedir()
*
* @returns {string} Path to log file
**/
2019-03-09 03:31:29 +00:00
async function logFile() {
2024-04-06 23:09:50 +00:00
const homeDir = (0, os_1.homedir)();
const linuxDir = `/.mcopy/`;
const macDir = `/Library/Logs/mcopy/`;
const winDir = `/AppData/Roaming/mcopy/`;
2024-04-06 23:09:50 +00:00
let logPath = (0, path_1.normalize)((0, path_1.join)(homeDir, linuxDir));
let dirExists;
2019-03-09 03:31:29 +00:00
if (process.platform === 'darwin') {
2024-04-06 23:09:50 +00:00
logPath = (0, path_1.normalize)((0, path_1.join)(homeDir, macDir));
2019-03-09 03:31:29 +00:00
}
else if (process.platform === 'win32') {
2024-04-06 23:09:50 +00:00
logPath = (0, path_1.normalize)((0, path_1.join)(homeDir, winDir));
2019-03-09 03:31:29 +00:00
}
try {
2024-04-06 23:09:50 +00:00
dirExists = await (0, fs_extra_1.exists)(logPath);
}
catch (err) {
console.error(err);
}
if (!dirExists) {
try {
2024-04-06 23:09:50 +00:00
await (0, fs_extra_1.mkdir)(logPath);
}
catch (err) {
console.error(`Error creating directory for mcopy log file, ${logPath}`);
console.error(err);
}
2019-03-09 03:31:29 +00:00
}
2024-04-06 23:09:50 +00:00
return (0, path_1.join)(logPath, 'mcopy.log');
2019-03-09 03:31:29 +00:00
}
/**
* Create and return the logger transport based on settings determined in
* arguments object
*
* @param {object} arg Arguments from process
*
* @returns {object} Logger transport
**/
async function Log(arg) {
let transport;
let consoleFormat = {
colorize: true
};
let fileFormat = {
filename: await logFile(),
json: true
};
if (arg && arg.label) {
consoleFormat.label = arg.label;
fileFormat.label = arg.label;
2019-03-09 03:31:29 +00:00
}
transport = (0, winston_1.createLogger)({
format: winston_1.format.combine(winston_1.format.label({ label: arg.label || 'mcopy' }), winston_1.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}), winston_1.format.printf((info) => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}` + (info.splat !== undefined ? `${info.splat}` : " "))),
transports: [
new (winston_1.transports.Console)(consoleFormat),
new (winston_1.transports.File)(fileFormat)
]
});
2019-03-09 03:31:29 +00:00
return transport;
}
exports.Log = Log;
module.exports = { Log };
2019-03-09 03:31:29 +00:00
//# sourceMappingURL=index.js.map