Permalink
Browse files

First commit

  • Loading branch information...
1 parent ef9f5b0 commit 4595de8dc4cebb1c5531bcdc77c30e9febca0b3c @vesln committed Dec 8, 2011
Showing with 447 additions and 0 deletions.
  1. +22 −0 .gitignore
  2. +12 −0 Makefile
  3. +46 −0 Readme.md
  4. +24 −0 examples/basic.js
  5. +16 −0 examples/file.js
  6. +15 −0 examples/levels.js
  7. +11 −0 examples/templates.js
  8. +19 −0 examples/tokens.js
  9. +11 −0 index.js
  10. +149 −0 lib/logme.js
  11. +16 −0 package.json
  12. +106 −0 test/logme.test.js
View
22 .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
View
12 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
View
46 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.
View
24 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.');
View
16 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.');
View
15 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!');
View
11 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');
View
19 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');
View
11 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');
View
149 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;
View
16 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" }
+}
View
106 test/logme.test.js
@@ -0,0 +1,106 @@
+/**
+ * Logme - Minimalistic logging.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+var should = require('should');
+var main = require('../');
+var Logme = main.Logme;
+var dateRegExp = /(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\,\s\d{2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{4}\s\d{2}:\d{2}:\d{2}\sGMT/;
+var logme = null;
+var log = null;
+var stream = {
+ write: function(msg) {
+ log = msg;
+ }
+};
+
+describe('logme singleton', function(){
+ it('should be sane', function(){
+ main.constructor.should.equal(main.Logme);
+ });
+});
+
+describe('Logme', function(){
+ beforeEach(function(){
+ logme = new Logme;
+ });
+
+ afterEach(function(){
+ logme = null;
+ });
+
+ it('should be configurable on construction', function(){
+ logme = new Logme({ prefix: 'foo' });
+ logme.options.prefix.should.equal('foo');
+ });
+
+ it('should have defaults', function(){
+ logme.options.should.eql({
+ level: 'debug',
+ stream: process.stdout
+ });
+ });
+
+ it('should have levels', function(){
+ logme.levels.should.eql({
+ debug: 0,
+ info: 1,
+ warning: 2,
+ error: 3,
+ critical: 4
+ });
+ });
+
+ it('should have templates', function(){
+ logme.templates.should.eql({
+ debug: '- DEBUG -'.magenta + ' :message',
+ info: '- INFO -'.cyan + ' :message',
+ warning: '- WARNING -'.yellow + ' :message',
+ error: '- ERROR -'.red + ' :message',
+ critical: '- CRITICAL -'.red.bold + ' :message'
+ });
+ });
+
+ describe('tokens', function(){
+ it('should have date', function(){
+ logme.tokens.date().should.match(dateRegExp);
+ });
+ });
+
+ describe('message', function(){
+ it('should format messages', function(){
+ var msg = 'Test msg';
+ for (var level in logme.levels) {
+ logme.message(level, msg).should.eql(logme.templates[level].replace(':message', msg));
+ }
+ });
+
+ it('should replace tokens', function(){
+ logme.tokens['foo'] = function() {
+ return 'bar';
+ };
+ logme.templates['debug'] = ':message :foo';
+ logme.message('debug', 'Foo').should.eql('Foo bar');
+ });
+ });
+
+ describe('log', function(){
+ afterEach(function(){
+ log = null;
+ });
+
+ it('shouldn\'t log messages if the supplied log level is less than the defined one', function(){
+ var logme = new Logme({level: 'error', stream: stream});
+ should.not.exist(logme.log('debug', 'Foo'));
+ });
+
+ it('should write to the stream', function(){
+ var logme = new Logme({level: 'debug', stream: stream });
+ logme.log('debug', 'Bar')
+ log.should.equal(logme.message('debug', 'Bar') + '\n');
+ });
+ });
+});

0 comments on commit 4595de8

Please sign in to comment.