1
0
mirror of https://github.com/balkian/simple-colourful-logger.git synced 2024-11-24 05:02:30 +00:00

First commit

This commit is contained in:
J.Fernando Sánchez 2013-06-03 23:47:51 +02:00
parent a8f0dbd25b
commit 0b579cc4c4
4 changed files with 100 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

20
README.md Normal file
View File

@ -0,0 +1,20 @@
![GSI Logo](http://gsi.dit.upm.es/templates/jgsi/images/logo.png)
[Simple Colourful Logger](http://gsi.dit.upm.es)
==================================
A simple logger originally designed for the Maia platform. It accepts arbitrary arguments and formats the input if it's not a string. As a plus, it keeps the padding, so the extra information printed doesn't break the indentation of the default console.log.
You can specify the name of the logger and the levels that should be displayed. By default, only the "log" level is enabled. Use 'all' to add all the logging levels.
var Logger = require('simple-colourful-logger').Logger;
logger = new Logger('my-name', ['debug','warn']);
logger.info('This is an error');
logger.debug('This is a debug', {foo: "bar", bar: "foo"});
logger.warn('This is a warning');
The result is the following:
01:52:41 [DEBUG][my-name] { foo: "bar",
bar: "foo }
01:52:42 [WARN] [my-name] This is a warning

View File

@ -0,0 +1,78 @@
var clc = require('cli-color'),
util = require('util');
var Logger = function(name, levels, colours){
this.name = name;
this.colours = colours;
this.logsactivated = { log: true };
for(var level in levels){
this.logsactivated[levels[level]] = true;
}
this.log('Initializing logger for:',name, ' for levels:', levels);
}
Logger.prototype.applyColours = function(c,colours){
for(var colour in colours){
if(c[colours[colour]]){
c=c[colours[colour]];
}
}
return c;
}
Logger.prototype.logmessage = function(level, colours){
var c;
if(typeof colours === 'string'){
c = clc[colours];
}else{
var c = this.applyColours(clc,colours);
}
return function(){
if(this.logsactivated[level] || this.logsactivated['all']){
var args = Array.prototype.slice.call(arguments);
var time = new Date();
var msg = time.toLocaleTimeString()+' ';
msg += '['+c(level)+']';
var padding = 5 - level.length;
while(padding>0){
msg+=' ';
padding-=1;
}
padding = 9+7+1;
if(this.name){
var c1 = clc;
msg += '['+c1(this.name)+'] ';
padding += 3+this.name.length;
}
var paddingstr = '\n';
while(padding>0){
paddingstr+=' ';
padding-=1;
}
for(var i in args){
if(typeof args[i] !== 'string'){
msg+=paddingstr;
args[i] = util.inspect(args[i]);
}
msg+=args[i].replace(/(\r\n|\n|\r)/gm, paddingstr);
}
console.log(msg);
}
}
}
Logger.prototype.removeLevel = function(level){
this.logsactivated[level] = false;
}
Logger.prototype.setLevel = function(level){
this.logsactivated[level] = true;
}
Logger.prototype.log = Logger.prototype.logmessage('LOG');
Logger.prototype.warn = Logger.prototype.logmessage('WARN','yellow');
Logger.prototype.info = Logger.prototype.logmessage('INFO', ['green']);
Logger.prototype.error = Logger.prototype.logmessage('ERROR', ['red','bgWhite']);
Logger.prototype.debug = Logger.prototype.logmessage('DEBUG',['bgWhite','blue']);
exports.Logger = Logger;

View File

@ -4,7 +4,7 @@
"description": "Simple colourful logger (http://gsi.dit.upm.es).",
"version": "0.1.0",
"repository": {
"url": "git://github.com/gsi-upm/Maia"
"url": "git://github.com/balkian/simple-colourful-logger"
},
"main": "./lib/simple-colourful-logger",
"dependencies": {