Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add .gitignore

  • Loading branch information...
commit b23704b3e7e622c029d37bb2e800353362f331e9 1 parent b597867
@Qard authored
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules
View
5 README.md
@@ -1,8 +1,7 @@
# Lager
-Simple, pretty and clean logging for node.
+[![Build Status](https://travis-ci.org/Qard/lager.png)](https://travis-ci.org/Qard/lager)
-## Requirements
-* Node.js 0.4+
+Simple, pretty and clean logging for node.
## Install
View
19 index.js
@@ -1,6 +1,5 @@
// Load dependencies.
-var _ = require('underscore')
- , reporter = require('./lib/reporter')
+var reporter = require('./lib/reporter')
, utils = require('./lib/utils')
// Add color support.
@@ -18,6 +17,7 @@ function Lager(name) {
return new Lager(name)
}
+ this.width = 10
this.indentation = ' '
this.useTimestamp = false
@@ -25,11 +25,14 @@ function Lager(name) {
if (typeof name === 'string') {
this.name = name
} else {
- _.extend(this, name)
+ for (var key in name) {
+ this[key] = name[key]
+ }
}
- if (typeof this.indentation !== 'string') {
- this.indentation = utils.numOfSpace(this.indentation)
+ // Support space count as alternative to raw string
+ if (typeof this.indentation === 'number') {
+ this.indentation = utils.numOfSpaces(this.indentation)
}
}
@@ -45,7 +48,7 @@ module.exports = Lager
* @param(s) mixed
* Extra info, will be inspected if non-string
*/
-Lager.prototype.log = reporter('log')
+Lager.prototype.log = reporter.type('log')
/**
* Notice level reporter
@@ -56,7 +59,7 @@ Lager.prototype.log = reporter('log')
* @param(s) mixed
* Extra info, will be inspected if non-string
*/
-Lager.prototype.notice = reporter('info')
+Lager.prototype.notice = reporter.type('info')
/**
* Error level reporter
@@ -67,4 +70,4 @@ Lager.prototype.notice = reporter('info')
* @param(s) mixed
* Extra info, will be inspected if non-string
*/
-Lager.prototype.error = reporter('error')
+Lager.prototype.error = reporter.type('error')
View
30 lib/reporter.js
@@ -22,19 +22,24 @@ function modeColor (mode) {
}
}
+exports.log = function log (mode, m) {
+ console[mode](m)
+}
+
// Export the reporter builder
-module.exports = function (mode) {
+exports.type = function (mode) {
// Prepare some mode scope utilities
var color = modeColor(mode)
- function log (m) { console[mode](m) }
+ function done (m) { exports.log(mode, m) }
// Return reporter
- return function(msg) {
+ return function (msg) {
// Collect extras list and indentation string
var extras = Array.prototype.slice.call(arguments, 1) || []
, ind = this.indentation
+ , res = []
// Support Error objects
if (msg instanceof Error) {
@@ -50,17 +55,22 @@ module.exports = function (mode) {
: extra
})
- // Show message line
- log(utils.indent((this.name + ' - ')[color] + utils.trim(msg), ind))
-
- // Show timestamp, if enabled
+ // Add timestamp, if enabled
if (this.useTimestamp) {
- log(ind + '- ' + (new Date).toUTCString().grey + "\n")
+ res.push(((new Date).toUTCString() + ' - ').grey)
}
- // Show extras, if available
+ // Add message name
+ res.push((utils.pad(this.name, this.width) + ' - ')[color])
+
+ // Add message content
+ res.push(utils.trim(msg))
+
+ // Add extras, if available
extras.forEach(function (extra) {
- log(utils.indent(extra.grey, ind + ind) + "\n")
+ res.push("\n" + utils.indent(extra.grey, ind + ind) + "\n")
})
+
+ done(utils.indent(res.join(''), ind))
}
}
View
43 lib/utils.js
@@ -1,27 +1,28 @@
-var _ = require('underscore')
-
-var utils = {
- // Trim whitespace
- trim: function (str) {
- return (str || '').replace(/^\s+|\s+$/g, '')
- }
+// Trim whitespace
+exports.trim = function (str) {
+ return (str || '').replace(/^\s+|\s+$/g, '')
+}
- // Perform a line map trim
- , trimLines: function (str) {
- return str.split("\n").map(utils.trim).join("\n")
- }
+// Trim each line
+exports.trimLines = function (str) {
+ return str.split("\n").map(exports.trim).join("\n")
+}
- // Indent string
- , indent: function (str, ind) {
- return ind + str.replace(/\n/g, "\n" + ind)
- }
+// Indent all lines of a string
+exports.indent = function (str, ind) {
+ return ind + str.replace(/\n/g, "\n" + ind)
+}
- // Generate indentation text from space count
- , numOfSpace: function (n) {
- return _.range(n).reduce(function (m) {
- return m + ' '
- }, '')
+// Generate indentation text from space count
+exports.numOfSpace = function (n) {
+ var res = []
+ for (var i = 0; i < n; i++) {
+ res.push(' ')
}
+ return res.join('')
}
-module.exports = utils
+// Pad all strings to have the same length
+exports.pad = function (str, depth) {
+ return exports.numOfSpace(depth - str.length) + str
+}
View
6 package.json
@@ -10,11 +10,13 @@
"node": ">= 0.4.0"
},
"dependencies": {
- "underscore": ">= 1.2.0",
"colors": ">= 0.5.0"
},
"devDependencies": {
"mocha":">= 0.5.0",
"should":">= 0.3.0"
+ },
+ "scripts": {
+ "test": "mocha -r should"
}
-}
+}
View
40 test.js
@@ -1,40 +0,0 @@
-#!/usr/bin/env mocha -r should -R spec
-var Lager = require('./index')
- , utils = require('./lib/utils')
-
-// Test different behaviours of the interface.
-describe('Lager', function () {
-
- var log;
- before(function () {
- log = new Lager('test')
- })
-
- it('should support name definitions', function () {
- log.name.should.eql('test')
- })
-
- it('should support object definitions', function () {
- var test = new Lager({ name: 'test' })
- test.name.should.eql('test')
- })
-
- it('should indent string', function () {
- utils.indent('test', ' ').should.eql(' test')
- })
-
- it('should generate indentation string', function () {
- utils.numOfSpace(1).should.eql(' ')
- })
-
- it('should trim string', function () {
- utils.trim(' test ').should.eql('test')
- })
-
- it('should expose reporters', function () {
- log.log.should.be.a('function')
- log.notice.should.be.a('function')
- log.error.should.be.a('function')
- })
-
-})
Please sign in to comment.
Something went wrong with that request. Please try again.