Files
alexis/lib/logger.js
2023-08-01 12:47:58 +02:00

48 lines
1.3 KiB
JavaScript

'use strict'
var chalk = require('chalk')
// colorize keywords
var keywordMap = {
SETUP: chalk.black.bgGreen.bold,
PHANTOMJS: chalk.black.bgMagenta.bold,
ROUTES: chalk.white.bgBlue.bold,
WEBSERVER: chalk.black.bgCyan.bold,
ERROR: chalk.black.bgRed.bold,
ECHO: chalk.black.bgYellowBright.bold,
}
// output formatted datetime before each log entry
var formatConsoleDate = () => {
var date = new Date()
var hour = date.getHours()
var minutes = date.getMinutes()
var seconds = date.getSeconds()
var milliseconds = date.getMilliseconds()
return ((hour < 10) ? '0' + hour: hour) + ':' +
((minutes < 10) ? '0' + minutes: minutes) + ':' +
((seconds < 10) ? '0' + seconds: seconds) + '.' +
('00' + milliseconds).slice(-3)
}
// log with colour
var logWithColour = function(item, text) {
console.log(
chalk.black.bgWhite(formatConsoleDate()),
keywordMap[item]('[' + item + ']'),
text)
}
// log without colour
var logWithoutColour = function(item, text) {
console.log(formatConsoleDate(), '[' + item + ']', text)
}
// export the log function
module.exports = {
setOption: function (use_colour, quiet) {
if (use_colour === false) this.log = logWithoutColour
if (quiet === true) this.log = function () {}
},
log: logWithColour
}