Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Readme and formatter tweaks
  • Loading branch information
mixu committed Jun 9, 2012
1 parent 7eb1d0a commit a12cea1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
44 changes: 39 additions & 5 deletions backends/console.js
Expand Up @@ -27,23 +27,31 @@ module.exports = {
end: function() {},
// formatting
formatClean: function(name, level, args) {
return '['+new Date().toUTCString()+'] '
var d = new Date();
function pad(s) { return (s.toString().length == 1? '0'+s : s); }
return '['
+d.getFullYear()+'-'
+pad(d.getMonth()+1) +'-'
+pad(d.getDay())+' '
+d.toLocaleTimeString()
+'] '
+ (name ? name + ' ' : '')
+ (level ? level + ' ' : '')
+ args.join(' ');
},
formatColor: function(name, level, args) {
var colors = { debug: 'magenta', info: 'cyan', warn: 'yellow', error: 'red' };
function pad(s) { return (s.toString().length == 4? ' '+s : s); }
return (name ? name + ' ' : '')
+ (level ? style('- ' + level.toUpperCase() + ' -', colors[level]) + ' ' : '')
+ (level ? style('- ' + pad(level.toUpperCase()) + ' -', colors[level]) + ' ' : '')
+ args.join(' ');
},
formatNpm: function(name, level, args) {
var out = {
debug: '\033[34;40m' + 'debug' + '\033[39m ',
info: '\033[32m' + 'info' + '\033[39m ',
warn: '\033[30;41m' + 'WARN' + '\033[0m ',
error: '\033[31;40m' + 'ERR!' + '\033[0m '
info: '\033[32m' + 'info' + '\033[39m ',
warn: '\033[30;41m' + 'WARN' + '\033[0m ',
error: '\033[31;40m' + 'ERR!' + '\033[0m '
};
return (name ? '\033[37;40m'+ name +'\033[0m ' : '')
+ (level && out[level]? out[level] : '')
Expand All @@ -59,5 +67,31 @@ module.exports = {
return (name ? name +' ' : '')
+ (level && out[level]? out[level] : '')
+ args.join(' ');
},
formatWithStack: function(name, level, args) {
var colors = { debug: 'magenta', info: 'cyan', warn: 'yellow', error: 'red' };
function pad(s) { return (s.toString().length == 4? ' '+s : s); }
function getStack() {
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function (err, stack) {
return stack;
};
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}

var frame = getStack()[4];

return (name ? name + ' ' : '')
+ (level ? style(pad(level), colors[level]) + ' ' : '')
+ style(
frame.getFileName().replace(new RegExp('^.*/(.+)$'), '$1')
+ ":" + frame.getLineNumber()
, 'grey')
+ ' '
+ args.join(' ');
}
};
11 changes: 11 additions & 0 deletions backends/jquery.js
@@ -0,0 +1,11 @@
function jqBackend(options) {
this.url = options.url || 'http://localhost';
}

jqBackend.prototype.write = function(str) {

};

jqBackend.prototype.end = function() {};

module.exports = jqBackend;
21 changes: 17 additions & 4 deletions readme.md
Expand Up @@ -15,14 +15,21 @@ Backends:

## Pipes everywhere

To log to the console:
You always pipe to a writable stream. To log to the console:

require('minilog').pipe(process.stdout);

To log into a file:

require('minilog').pipe(fs.createWriteStream('./temp.log'));

To log every 30s over HTTP via jQuery.ajax:

var jQueryBackend = require('minilog').backends.jquery;
require('minilog').pipe(new jQueryBackend({ url: 'http://localhost/'}));

You can pipe to more than one pipe if you want.

## Basic usage and namespaces

Basic usage:
Expand Down Expand Up @@ -55,7 +62,7 @@ Output:

## Formatting / templating

Formatting can be applied to pipes:
Each pipe returns a chainable config object. Formatting can be applied to pipes:

MiniLog
.pipe(process.stdout)
Expand All @@ -66,8 +73,7 @@ Formatting can be applied to pipes:
});

The console logger comes with format functions inspired by [logme](https://github.com/vesln/logme).

### Theme: clean
The withStack formatter can print the module name and current line number by examining the stack trace.



Expand Down Expand Up @@ -99,3 +105,10 @@ TODO not done
## Using it in the browser

TODO - use Glue or onejs.

## Disabling logging completely

If your build system supports this (e.g. onejs --tie minilog="..."), use this replacement to disable logging in production builds:

function minilog() { return minilog; };

5 changes: 5 additions & 0 deletions test/example/example.js
Expand Up @@ -24,3 +24,8 @@ out();
console.log('\n== Theme: learnboost\n');
config.format(ConsoleBackend.formatLearnboost);
out();

console.log('\n== Theme: withStack\n');
config.format(ConsoleBackend.formatWithStack);
out();

0 comments on commit a12cea1

Please sign in to comment.