Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Dec 8, 2011
1 parent ef9f5b0 commit 4595de8
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
@@ -0,0 +1,22 @@
tmp
node_modules
*._
*.tmp
.monitor
*.diff
*.err
*.orig
*.log
*.rej
*.swo
*.swp
*.vi
*~
.DS_Store
Thumbs.db
.cache
.project
.settings
.tmproj
*.esproj
nbproject
12 changes: 12 additions & 0 deletions Makefile
@@ -0,0 +1,12 @@
TESTS = test/*.test.js

test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require should \
--reporter spec \
$(TESTS)

clean:
rm -f examples/tmp/*

.PHONY: test clean
46 changes: 46 additions & 0 deletions Readme.md
@@ -0,0 +1,46 @@
# Logme

Minimalistic logger for Node.js.

![screenshot](http://img17.imageshack.us/img17/3241/screenshot20111209at120.png)

## Features.

- Error levels
- Tokens
- Custom templates
- Custom everything

## How to install it?

$ npm install logme

## Neat. Now what?

var logme = require('logme');
logme.critical('The base is under atack');

## Whaaaat?

For more complete examples see "examples/".

## Tests.

Sure.

$ npm install
$ make test

## Contributing to this library.

Anytime.

## Inspiration.

- [log.js](https://github.com/visionmedia/log.js)
- [express](https://github.com/visionmedia/express)
- [mongoose](https://github.com/LearnBoost/mongoose)

## License.

MIT License.
24 changes: 24 additions & 0 deletions examples/basic.js
@@ -0,0 +1,24 @@
/**
* Logme - Minimalistic logging.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/

var logme = require('../');

/**
* Different levels.
*/
logme.debug('Lol, coffescript.');
logme.info('Logme is sexy.');
logme.warning('Danger!');
logme.error('Ooops, something went wrong.');
logme.critical('The base is under atack.');

// or...
logme.log('debug', 'Lol, coffescript.');
logme.log('info', 'Logme is sexy.');
logme.log('warning', 'Danger!');
logme.log('error', 'Ooops, something went wrong.');
logme.log('critical', 'The base is under atack.');
16 changes: 16 additions & 0 deletions examples/file.js
@@ -0,0 +1,16 @@
/**
* Logme - Minimalistic logging.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/

var colors = require('colors');
colors.mode = 'none';

var Logme = require('../').Logme;
var fs = require('fs');
var stream = fs.createWriteStream(__dirname + '/tmp/file.log', { flags: 'a' })
var logme = new Logme({ stream: stream });

logme.error('Pro Tip: It logs to files.');
15 changes: 15 additions & 0 deletions examples/levels.js
@@ -0,0 +1,15 @@
/**
* Logme - Minimalistic logging.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/

var Logme = require('../').Logme;
var logme = new Logme({ level: 'error' });

logme.debug('This will not be logged.');
logme.info('This will not be logged.');

logme.error('Win!');
logme.critical('Double win!');
11 changes: 11 additions & 0 deletions examples/templates.js
@@ -0,0 +1,11 @@
/**
* Logme - Minimalistic logging.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/

var logme = require('../');

logme.templates['info'] = 'logme -NFO- :message';
logme.info('Custom templates');
19 changes: 19 additions & 0 deletions examples/tokens.js
@@ -0,0 +1,19 @@
/**
* Logme - Minimalistic logging.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/

var logme = require('../');

logme.templates['info'] = ':date - INFO - :message';
logme.info('Custom templates');


logme.tokens['random'] = function() {
return Math.random();
};

logme.templates['info'] = ':date - INFO - :message (:random)';
logme.info('Custom templates');
11 changes: 11 additions & 0 deletions index.js
@@ -0,0 +1,11 @@
/**
* Logme - Minimalistic logging.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/

/**
* Exports the lib.
*/
module.exports = require('./lib/logme');
149 changes: 149 additions & 0 deletions lib/logme.js
@@ -0,0 +1,149 @@
/**
* Logme - Minimalistic logging.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/

/**
* Dependencies.
*/
var colors = require('colors');

/**
* Logme constructor.
*
* @class
*/
function Logme(config) {
this.options = {
stream: process.stdout,
level: 'debug'
};

if (config === Object(config)) {
for (var key in config) {
this.options[key] = config[key];
}
}
};

/**
* Tokens map, used by the message method.
*
* @type {Object}
*/
Logme.prototype.tokens = {
date: function() {
return new Date().toUTCString();
}
};

/**
* Log levels.
*
* @type {Object}
*/
Logme.prototype.levels = {
debug: 0,
info: 1,
warning: 2,
error: 3,
critical: 4
};

/**
* Templates.
*
* @type {Object}
*/
Logme.prototype.templates = {
debug: '- DEBUG -'.magenta + ' :message',
info: '- INFO -'.cyan + ' :message',
warning: '- WARNING -'.yellow + ' :message',
error: '- ERROR -'.red + ' :message',
critical: '- CRITICAL -'.red.bold + ' :message'
};

/**
* Formats a log message.
*
* @param {String} level
* @param {String} str
* @returns {String}
*/
Logme.prototype.message = function(level, str) {
var message = this.templates[level];
for (var token in this.tokens) {
message = message.replace(':' + token, this.tokens[token]());
}
return message.replace(':message', str);
};

/**
* Writes a message to the stream.
*
* @param {String} level
* @param {String} str
*/
Logme.prototype.log = function(level, str) {
if (this.levels[level] < this.levels[this.options.level]) return;
var msg = this.message(level, str);
this.options.stream.write(msg + "\n");
};


/**
* Logs debug message.
*
* @param {String} str
*/
Logme.prototype.debug = function(str) {
return this.log('debug', str)
};

/**
* Logs info message.
*
* @param {String} str
*/
Logme.prototype.info = function(str) {
return this.log('info', str)
};

/**
* Logs warning message.
*
* @param {String} str
*/
Logme.prototype.warning = function(str) {
return this.log('warning', str)
};

/**
* Logs error message.
*
* @param {String} str
*/
Logme.prototype.error = function(str) {
return this.log('error', str)
};

/**
* Logs critical message.
*
* @param {String} str
*/
Logme.prototype.critical = function(str) {
return this.log('critical', str)
};

/**
* Exposing the lib.
*/
module.exports = exports = new Logme;

/**
* Exports Logme constructor.
*/
exports.Logme = Logme;
16 changes: 16 additions & 0 deletions package.json
@@ -0,0 +1,16 @@
{
"name": "logme"
, "version": "0.0.1"
, "description": "Minimalistic stream logger"
, "keywords": ["logger", "logging", "log"]
, "author": "Veselin Todorov <hi@vesln.com>"
, "dependencies": {
"colors": "0.5.1"
}
, "devDependencies": {
"mocha": "0.3.3"
, "should": "0.3.2"
}
, "main": "index"
, "engines": { "node": ">= 0.6.0 < 0.7.0" }
}

0 comments on commit 4595de8

Please sign in to comment.