hardlounge/src/log.js
Jérémie Astori b90c224a99
Define a raw logger to avoid using console.log, use it in extra help for environment variables, and add a test for this
This has multiple benefits:

- Respects the "Do not mock what you do not own" principle, instead we mock `log.raw` when necessary
- Lets us not re-assign `console.log`, which breaks as Mocha uses `console.log` as well
- Save and restore initial `log.raw` in test hooks (before/after), otherwise this would break Mocha/Chai
2017-12-11 23:48:51 -05:00

46 lines
1.1 KiB
JavaScript

"use strict";
var colors = require("colors/safe");
var moment = require("moment");
const read = require("read");
var Helper = require("./helper");
function timestamp(type, messageArgs) {
var format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss";
var tz = Helper.config.logs.timezone || "UTC+00:00";
var time = moment().utcOffset(tz).format(format);
Array.prototype.unshift.call(messageArgs, colors.dim(time), type);
return messageArgs;
}
/* eslint-disable no-console */
exports.error = function() {
console.error.apply(console, timestamp(colors.red("[ERROR]"), arguments));
};
exports.warn = function() {
console.error.apply(console, timestamp(colors.yellow("[WARN]"), arguments));
};
exports.info = function() {
console.log.apply(console, timestamp(colors.blue("[INFO]"), arguments));
};
exports.debug = function() {
console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments));
};
exports.raw = function() {
console.log.apply(console, arguments);
};
/* eslint-enable no-console */
exports.prompt = (options, callback) => {
options.prompt = timestamp(colors.cyan("[PROMPT]"), [options.text]).join(" ");
read(options, callback);
};