Skip to content

Instantly share code, notes, and snippets.

@wildhart
Created February 11, 2020 01:20
Show Gist options
  • Save wildhart/bf876b888fb6cb232078a055065af325 to your computer and use it in GitHub Desktop.
Save wildhart/bf876b888fb6cb232078a055065af325 to your computer and use it in GitHub Desktop.
Colorful console.logs in Meteor
console.col = {
colours: {
red: "31", // prefix with 1; for light/bold colours
green: "32",
yellow: "33",
blue: "34",
magenta: "35",
cyan: "36",
default: "0",
white: "0",
},
addTime: Meteor.isProduction,
log: console.log,
warn: console.warn,
names: {},
name: function(name, col, options) {
if (!console.col.colours[col]) return console.error('console.defineColour, name:', name, 'invalid colour:', col);
console.col.names[name] = {
col: col,
mute: options.mute || false,
prefix: options.prefix || false,
addTime: (typeof options.addTime == 'undefined') ? 'default' : options.addTime,
};
},
};
console.col.name('sms', 'yellow', {prefix: true, mute: true});
console.col.name('email', 'magenta', {prefix: true, mute: true});
console.col.name('checkAccounts', 'red', {prefix: true, mute: true});
console.col.name('push', 'cyan', {prefix: true, mute: true});
console.col.name('Jobs', 'blue', {prefix: true, mute: true});
console.col.name('api', 'green', {prefix: true, mute: false});
console.log = function(...args) {
try {
var warning = args[0]=='warn' ? ' WARNING:': '';
if (warning) args.shift();
var col=args[0];
// ignore the LISTENING message which tells Meteor that the server is running
if (col=="LISTENING") return console.col.log.apply(this, args);
var addTime = console.col.addTime;
const timestamp = new Date().format("DDTHH:mm:ss");
let name = console.col.names[col];
if (!name && typeof col=='string' && !console.col.colours[col] && args.length>1 && col.length<16 && col.indexOf(":")==-1) {
// automatically add a new definition
name = {col: 'white', prefix: true, mute: false, addTime: 'default'};
console.col.names[col] = name;
}
if (name) {
if (name.mute && !warning) return;
args.shift(); // remove first item so we don't output it in the console.
if (name.prefix) args.unshift(col+":");
if (name.addTime!='default') addTime = name.addTime;
col = name.col;
} else if (!console.col.colours[col]) {
// add timestamp to start
if (addTime) args.unshift(timestamp);
return console.col[warning?'warn':'log'].apply(this, args);
} else {
args.shift(); // remove first item so we don't output it in the console.
}
try {
for (var i=args.length; i--;) {
if (typeof args[i]=='object') args[i]=JSON.stringify(args[i]);
}
} catch(e) {
console.col.log.apply(this, args);
}
// concatenate the timestamp with the colour control codes so don't get an extra space.
col=console.col.colours[col];
if (warning) col+=";4";
col+=';1'; // make all text lighter/bold
args[0]=(addTime?timestamp+' ':'')+'\x1b['+col+'m'+args[0]+warning;
args[args.length-1]+='\x1b['+console.col.colours.default+'m';
return console.col.log.apply(this, args);
} catch(e) {
console.error("ERROR in consoleColours", e);
return console.col.log.apply(this, args);
}
}
console.warn = function(...args) {
args.unshift('warn'); // add 'warn' as first argument
return console.log.apply(this, args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment