Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[codestyle] Small code clean up

  • Loading branch information...
commit c13ab09c7e9cdbb1c86c837ff4a8ccd07db16b2e 1 parent a49b64c
@3rd-Eden 3rd-Eden authored
View
5 index.js
@@ -1,11 +1,12 @@
+'use strict';
+
/**!
* dev/null
- * @copyright (c) 2011 Observer (observer.no.de) <info@3rd-Eden.com>
+ * @copyright (c) 2012 Observer (observer.no.de) <info@3rd-Eden.com>
* MIT Licensed
*/
/**
* Require the core module.
*/
-
module.exports = require('./lib/logger');
View
96 lib/logger.js
@@ -1,15 +1,14 @@
-"use strict";
+'use strict';
/**!
* dev/null
- * @copyright (c) 2011 Observe.it (observe.it) <arnout@observe.com>
+ * @copyright (c) 2012 Observe.it (observe.it) <arnout@observe.com>
* MIT Licensed
*/
-
var tty = require('tty')
, colors = require('colors')
, util = require('util')
- , EventEmitter = process.EventEmitter;
+ , EventEmitter = require('events').EventEmitter;
/**
* Strict type checking.
@@ -18,7 +17,6 @@ var tty = require('tty')
* @returns {String}
* @api private
*/
-
function type (prop) {
var rs = Object.prototype.toString.call(prop);
return rs.slice(8, rs.length - 1).toLowerCase();
@@ -30,7 +28,6 @@ function type (prop) {
* @type {Boolean}
* @api private
*/
-
var atty = tty.isatty(process.stdout.fd);
/**
@@ -39,7 +36,6 @@ var atty = tty.isatty(process.stdout.fd);
* @type {Array}
* @api private
*/
-
var levels = {
'alert': 0
, 'critical': 1
@@ -60,7 +56,6 @@ var levels = {
* @type {Object}
* @api private
*/
-
var methods = {
development: {
alert: 'alert: '.red
@@ -102,8 +97,7 @@ var methods = {
* @param {Object} options options
* @api public
*/
-
-var Logger = module.exports = function devnull (options) {
+var Logger = module.exports = function devnull(options) {
options = options || {};
// default options
@@ -151,8 +145,7 @@ Logger.prototype.__proto__ = EventEmitter.prototype;
* @param {Function} fn callback
* @api public
*/
-
-Logger.prototype.configure = function configure (env, fn) {
+Logger.prototype.configure = function configure(env, fn) {
var envs = 'all'
, args = [].slice.call(arguments);
@@ -171,8 +164,7 @@ Logger.prototype.configure = function configure (env, fn) {
* @param {Mixed} value the new value
* @api public
*/
-
-Logger.prototype.set = function set (key, value) {
+Logger.prototype.set = function set(key, value) {
if (key in this) {
if (value !== this[key]) this.emit('settings:' + key, value);
@@ -189,8 +181,7 @@ Logger.prototype.set = function set (key, value) {
* @returns {Mixed}
* @api public
*/
-
-Logger.prototype.get = function get (key) {
+Logger.prototype.get = function get(key) {
return this[key];
};
@@ -200,8 +191,7 @@ Logger.prototype.get = function get (key) {
* @param {String} key
* @returns {Boolean}
*/
-
-Logger.prototype.enabled = function enabled (key) {
+Logger.prototype.enabled = function enabled(key) {
return !!this.get(key);
};
@@ -212,8 +202,7 @@ Logger.prototype.enabled = function enabled (key) {
* @returns {Boolean}
* @api public
*/
-
-Logger.prototype.disabled = function disabled (key) {
+Logger.prototype.disabled = function disabled(key) {
return !this.get(key);
};
@@ -223,8 +212,7 @@ Logger.prototype.disabled = function disabled (key) {
* @param {String} key
* @api public
*/
-
-Logger.prototype.enable = function enable (key) {
+Logger.prototype.enable = function enable(key) {
return this.set(key, true);
};
@@ -234,8 +222,7 @@ Logger.prototype.enable = function enable (key) {
* @param {String} key
* @api public
*/
-
-Logger.prototype.disabled = function disabled (key) {
+Logger.prototype.disable = function disable(key) {
return this.set(key, true);
};
@@ -245,8 +232,7 @@ Logger.prototype.disabled = function disabled (key) {
* @param {String} file
* @api public
*/
-
-Logger.prototype.ignore = function ignore (file) {
+Logger.prototype.ignore = function ignore(file) {
this.silence[file] = 1;
return this;
@@ -257,8 +243,7 @@ Logger.prototype.ignore = function ignore (file) {
*
* @api public
*/
-
-Logger.prototype.unignore = function unignore (file) {
+Logger.prototype.unignore = function unignore(file) {
if (file in this.silence) delete this.silence[file];
return this;
@@ -270,8 +255,7 @@ Logger.prototype.unignore = function unignore (file) {
* @returns {Array}
* @api public
*/
-
-Logger.prototype.ignoring = function ignoring () {
+Logger.prototype.ignoring = function ignoring() {
return Object.keys(this.silence);
};
@@ -283,8 +267,7 @@ Logger.prototype.ignoring = function ignoring () {
* @returns {Logger}
* @api public
*/
-
-Logger.prototype.use = function use (Transport, options) {
+Logger.prototype.use = function use(Transport, options) {
// prevent duplicates
if (type(Transport) !== 'function') return this;
@@ -300,8 +283,7 @@ Logger.prototype.use = function use (Transport, options) {
* @return {Boolean}
* @api private
*/
-
-Logger.prototype.has = function has (Transport) {
+Logger.prototype.has = function has(Transport) {
if (!Transport) return false;
var i = this.transports.length
@@ -323,8 +305,7 @@ Logger.prototype.has = function has (Transport) {
* @returns {Logger}
* @api public
*/
-
-Logger.prototype.remove = function remove (Transport) {
+Logger.prototype.remove = function remove(Transport) {
var transport = this.has(Transport)
, i = this.transports.length;
@@ -352,8 +333,7 @@ Logger.prototype.remove = function remove (Transport) {
* @returns {Logger}
* @api public
*/
-
-Logger.prototype.write = function write (type, level, stack) {
+Logger.prototype.write = function write(type, level, stack) {
var args = Array.prototype.slice.call(arguments).slice(3)
, length = this.transports.length
, i = 0
@@ -366,7 +346,6 @@ Logger.prototype.write = function write (type, level, stack) {
// silence, silence! ssht!
if (this.silence[filename]) return this;
-
}
for (; i < length; i++) {
@@ -392,20 +371,22 @@ Logger.prototype.write = function write (type, level, stack) {
* @returns {String}
* @api public
*/
-
-Logger.prototype.stamp = function stamp (date) {
+Logger.prototype.stamp = function stamp(date) {
if (!this.timestamp) return '';
- var now = date || new Date;
- return this.pattern.replace(/\{(.+?)(?::(.*?))?\}/g, function (res, method, padding) {
- for (res = now[method in now ? method : 'get' + method]() // exec the getter
- + (/h/.test(method) || '') // increment month by 1
- + ''; // cast to string
- res.length < padding; // while we need padding
- res = 0 + res // padd with zeros
- );
+ var now = date || new Date();
+
+ return this.pattern.replace(
+ /\{(.+?)(?::(.*?))?\}/g
+ , function replace(res, method, padding) {
+ for (res = now[method in now ? method : 'get' + method]() // exec the getter
+ + (/h/.test(method) || '') // increment month by 1
+ + ''; // cast to string
+ res.length < padding; // while we need padding
+ res = 0 + res // padd with zeros
+ );
- return res;
+ return res;
});
};
@@ -422,9 +403,8 @@ Logger.prototype.stamp = function stamp (date) {
* @returns {String}
* @api private
*/
-
var formatRegExp = /%[sdj%]/g;
-Logger.prototype.format = function format (f) {
+Logger.prototype.format = function format(f) {
var args = arguments
, len = args.length
, str = []
@@ -434,11 +414,12 @@ Logger.prototype.format = function format (f) {
for (; i < len; i++) {
str.push(util.inspect(args[i]));
}
+
return str.join(' ');
}
i = 1;
- str = f.replace(formatRegExp, function (x) {
+ str = f.replace(formatRegExp, function formatting(x) {
if (i >= len) return x;
switch (x) {
@@ -475,8 +456,7 @@ Logger.prototype.format = function format (f) {
* @returns {String} namespace
* @api private
*/
-
-Logger.prototype.namespace = function namespace (trace, args, filename) {
+Logger.prototype.namespace = function namespace(trace, args, filename) {
if (!trace || !trace.length) return [];
var arg = args[0]
@@ -517,8 +497,7 @@ Logger.prototype.namespace = function namespace (trace, args, filename) {
*
* @api private
*/
-
-Object.keys(levels).forEach(function levelr (level) {
+Object.keys(levels).forEach(function levelr(level) {
var index = levels[level]
, capture = [
'var stack, err, original;'
@@ -552,7 +531,6 @@ Object.keys(levels).forEach(function levelr (level) {
* @type {String}
* @api public
*/
-
Logger.version = '0.0.7';
/**
@@ -561,7 +539,6 @@ Logger.version = '0.0.7';
* @type {Object}
* @api public
*/
-
Logger.methods = methods;
/**
@@ -570,5 +547,4 @@ Logger.methods = methods;
* @type {Object}
* @api public
*/
-
Logger.levels = levels;
View
2  package.json
@@ -18,7 +18,7 @@
, "long-stack-traces": "0.1.2"
, "mongodb": "0.9.9-8"
}
- , "main": "index"
+ , "main": "lib/logger"
, "scripts": {
"test": "make test"
}
View
40 transports/index.js
@@ -1,38 +1,20 @@
+'use strict';
+
/**!
* dev/null
- * @copyright (c) 2011 Observe.it (observe.it) <arnout@observe.com>
+ * @copyright (c) 2012 Observe.it (observe.it) <arnout@observe.com>
* MIT Licensed
*/
/**
- * Lazy require the `stream` transport.
- */
-
-var stream;
-Object.defineProperty(exports, 'stream', {
- get: function () {
- return stream || (stream = require('./stream'));
- }
-});
-
-/**
- * Lazy require the `mongodb` transport.
- */
-
-var mongodb;
-Object.defineProperty(exports, 'mongodb', {
- get: function () {
- return mongodb || (mongodb = require('./mongodb'));
- }
-});
-
-/**
- * Lazy require the transport base.
+ * Lazy require the different transports
*/
+['stream', 'mongodb', 'transport'].forEach(function generate(transport) {
+ var cached;
-var transport;
-Object.defineProperty(exports, 'transport', {
- get: function () {
- return transport || (transport = require('./transport'));
- }
+ Object.defineProperty(exports, transport, {
+ get: function get {
+ return cached || (cached = require('./'+ transport));
+ }
+ });
});
View
41 transports/mongodb.js
@@ -1,16 +1,22 @@
-"use strict";
+'use strict';
/**!
* dev/null
- * @copyright (c) 2011 observe.it (observe.it) <arnout@observe.com>
+ * @copyright (c) 2012 observe.it (observe.it) <arnout@observe.com>
* mit licensed
*/
-
var Transport = require('./transport')
, mongodb = require('mongodb')
, os = require('os');
-var MongoDB = module.exports = function mongo (logger, options) {
+/**
+ * The MongoDB transport.
+ *
+ * @constructor
+ * @param {Logger} logger logger base instance
+ * @param {Object} options configurations
+ */
+var MongoDB = module.exports = function mongo(logger, options) {
// properties that could be overriden
this.collection = 'log';
this.save = false;
@@ -32,7 +38,6 @@ var MongoDB = module.exports = function mongo (logger, options) {
/**
* Inherit from `Transport`.
*/
-
require('util').inherits(MongoDB, Transport);
/**
@@ -44,11 +49,10 @@ require('util').inherits(MongoDB, Transport);
* @param {Function} fn callback
* @api private
*/
-
MongoDB.prototype.collect = function collect(err, db, collection, fn) {
if (err) return fn.call(db, err, null);
- db.collection(collection, function collection (err, col) {
+ db.collection(collection, function collection(err, col) {
fn.call(col, err, db);
});
};
@@ -60,8 +64,7 @@ MongoDB.prototype.collect = function collect(err, db, collection, fn) {
* @param {MongoDB} mongo Mongodb connection stream
* @api private
*/
-
-MongoDB.prototype.open = function open (err, connection) {
+MongoDB.prototype.open = function open(err, connection) {
var self = this;
if (!err) {
@@ -69,7 +72,7 @@ MongoDB.prototype.open = function open (err, connection) {
// handle uncaught errors, close the connection so we can automatically
// setup a new one again
- connection.on('error', function uncaughtError (err) {
+ connection.on('error', function uncaughtError(err) {
self.stream.close();
self.stream = null;
self.connecting = false;
@@ -82,7 +85,7 @@ MongoDB.prototype.open = function open (err, connection) {
this.connecting = false;
// start the whole collect thingy
- this.queue.splice(0).forEach(function queue (request) {
+ this.queue.splice(0).forEach(function queue(request) {
self.collect.call(self.stream
, err
, self.stream
@@ -99,11 +102,11 @@ MongoDB.prototype.open = function open (err, connection) {
* @param {Function} fn callback
* @api private
*/
-
-MongoDB.prototype.allocate = function allocate (collection, fn) {
+MongoDB.prototype.allocate = function allocate(collection, fn) {
// fast case
- if (this.stream)
+ if (this.stream) {
return this.collect.call(this.stream, null, this.stream, collection, fn);
+ }
// no stream so we will queue up requests if needed
var request = {
@@ -132,11 +135,10 @@ MongoDB.prototype.allocate = function allocate (collection, fn) {
* @param {String} namespace
* @api public
*/
-
-MongoDB.prototype.write = function write (type, namespace, args) {
+MongoDB.prototype.write = function write(type, namespace, args) {
var self = this;
- this.allocate(this.collection, function allocate (err, db) {
+ this.allocate(this.collection, function allocate(err, db) {
var log = {
type: type
, machine: self.machine
@@ -154,7 +156,7 @@ MongoDB.prototype.write = function write (type, namespace, args) {
if (err) return self.logger.emit('transport:error', err, log);
// attempt to add the log to the db
- this.insert(log, { safe: self.safe }, function insert (err) {
+ this.insert(log, { safe: self.safe }, function insert(err) {
if (err) return self.logger.emit('transport:error', err, log);
self.logger.emit('transport:write', log);
@@ -167,8 +169,7 @@ MongoDB.prototype.write = function write (type, namespace, args) {
*
* @api private
*/
-
-MongoDB.prototype.close = function close () {
+MongoDB.prototype.close = function close() {
if (!this.stream) return this;
this.stream.close();
View
18 transports/stream.js
@@ -1,11 +1,10 @@
-"use strict";
+'use strict';
/**!
* dev/null
- * @copyright (c) 2011 observe.it (observe.it) <arnout@observe.com>
+ * @copyright (c) 2012 observe.it (observe.it) <arnout@observe.com>
* mit licensed
*/
-
var Transport = require('./transport');
/**
@@ -16,12 +15,11 @@ var Transport = require('./transport');
*
* - `stream` A node.js compatible stream, defaults to process.stdout
*
- * @param {Stream} stream
+ * @param {Logger} logger
* @param {Object} options
* @api public
*/
-
-var Streamer = module.exports = function streamer (logger, options) {
+var Streamer = module.exports = function streamer(logger, options) {
// properties that could be overriden
this.stream = process.stdout;
@@ -34,7 +32,6 @@ var Streamer = module.exports = function streamer (logger, options) {
/**
* Inherit from `Transport`.
*/
-
require('util').inherits(Streamer, Transport);
/**
@@ -42,10 +39,10 @@ require('util').inherits(Streamer, Transport);
*
* @param {String} type
* @param {String} namespace
+ * @param {Array} args arguments
* @api public
*/
-
-Streamer.prototype.write = function write (type, namespace, args) {
+Streamer.prototype.write = function write(type, namespace, args) {
var log = this.logger.stamp()
+ ' '
+ this.logger.prefix[type]
@@ -68,8 +65,7 @@ Streamer.prototype.write = function write (type, namespace, args) {
*
* @api private
*/
-
-Streamer.prototype.close = function close () {
+Streamer.prototype.close = function close() {
// don't close the stdout
if (this.stream === process.stdout) return this;
View
22 transports/transport.js
@@ -1,5 +1,10 @@
-"use strict";
+'use strict';
+/**!
+ * dev/null
+ * @copyright (c) 2012 observe.it (observe.it) <arnout@observe.com>
+ * mit licensed
+ */
var EventEmitter = process.EventEmitter;
/**
@@ -9,8 +14,7 @@ var EventEmitter = process.EventEmitter;
* @returns {String}
* @api private
*/
-
-function type (prop) {
+function type(prop) {
var rs = Object.prototype.toString.call(prop);
return rs.slice(8, rs.length - 1).toLowerCase();
}
@@ -19,11 +23,11 @@ function type (prop) {
* The base setup.
*
* @constructor
+ * @param {Logger} logger
* @param {Object} options
* @api private
*/
-
-var Transport = module.exports = function transport (logger, options) {
+var Transport = module.exports = function transport(logger, options) {
var self = this
, key;
@@ -46,7 +50,7 @@ var Transport = module.exports = function transport (logger, options) {
// lazy initialize
if (this.initialize) {
- process.nextTick(function next () {
+ process.nextTick(function next() {
self.initialize.call(self, options);
});
}
@@ -63,13 +67,11 @@ Transport.prototype.__proto__ = EventEmitter.prototype;
* @param {Array} args arguments
* @api public
*/
-
-Transport.prototype.write = function write (type, namespace, args) {};
+Transport.prototype.write = function write(type, namespace, args) {};
/**
* The transport needs to be removed
*
* @api public
*/
-
-Transport.prototype.close = function close () {};
+Transport.prototype.close = function close() {};
Please sign in to comment.
Something went wrong with that request. Please try again.