Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for adding multiple transports of the same instance

  • Loading branch information...
commit 5a9d7450764482593894079090ca54b8a7db370b 1 parent 99fd0b0
@3rd-Eden 3rd-Eden authored
View
2  .gitignore
@@ -1,3 +1,3 @@
node_modules
.DS_Store
-stream.log
+*.log
View
41 example/stream.js
@@ -0,0 +1,41 @@
+"use strict";
+
+var Logger = require('../')
+ , stream = require('../transports/stream')
+ , logger = new Logger();
+
+// enable file logging
+logger.use(stream, {
+ stream: require('fs').createWriteStream('logger.log')
+});
+
+function namespacing () {
+ logger.debug('debug message');
+ logger.log('logging an array', []);
+ logger.info('info message with object', {});
+ logger.notice('sending a notice', 1, 2, 3);
+ logger.metric('already send', logger.calls, 'logs');
+ logger.warning('odear, we are going to break something');
+ logger.error('something bad happend');
+ logger.critical('oh FUCK the system is melting down');
+ logger.alert('call the police!');
+}
+
+// used for namespacing the stuff
+setTimeout(function showoff () {
+ namespacing();
+}, 100);
+
+// example for non namespaced stuff
+logger.log('hello world');
+
+logger.log('userdefinednamespace', 'one word without spaces is a user namespace');
+
+// listen for emitted errors
+logger.on('error', function (args, stack) {
+ console.log('There was an error logged at line: ' + (stack
+ ? stack[0].getLineNumber()
+ : 'unknown'
+ ));
+});
+
View
13 lib/logger.js
@@ -286,9 +286,7 @@ Logger.prototype.ignoring = function ignoring () {
Logger.prototype.use = function use (Transport, options) {
// prevent duplicates
- if (this.has(Transport)
- || type(Transport) !== 'function'
- ) return this;
+ if (type(Transport) !== 'function') return this;
this.transports.push(new Transport(this, options || {}));
return this;
@@ -304,12 +302,13 @@ Logger.prototype.use = function use (Transport, options) {
*/
Logger.prototype.has = function has (Transport) {
- var i = this.transports.length;
+ var i = this.transports.length
+ , source = Transport.toString();
while (i--) {
- if (this.transports[i] instanceof Transport
- || this.transports[i] === Transport
- ) return this.transports[i];
+ if (this.transports[i].constructor.toString() === source) {
+ return this.transports[i];
+ }
}
return false;
View
89 tests/streamer.transport.test.js
@@ -1,93 +1,96 @@
+/*globals it:true, describe:true, Transport:true, fixtures:true, Logger:true */
/**!
* dev/null
* @copyright (c) 2011 observe.it (observe.it) <arnout@observe.com>
* mit licensed
*/
-var Stream = require('../transports').stream
+var Stream = require('../transports').stream;
describe('streamer.transport', function () {
+ "use strict";
+
it('should be an transport instance', function () {
- var streamy = new Stream
+ var streamy = new Stream();
- streamy.should.be.an.instanceof(Transport)
- })
+ streamy.should.be.an.instanceof(Transport);
+ });
it('should have streamer as name', function () {
- var streamy = new Stream
+ var streamy = new Stream();
- streamy.name.should.be.a('string')
- streamy.name.should.equal('streamer')
- })
+ streamy.name.should.be.a('string');
+ streamy.name.should.equal('streamer');
+ });
it('should have all required functions', function () {
- var streamy = new Stream
+ var streamy = new Stream();
- streamy.should.respondTo('write')
- streamy.should.respondTo('close')
- })
+ streamy.should.respondTo('write');
+ streamy.should.respondTo('close');
+ });
it('should work with different streams', function () {
var writeStream = require('fs').createWriteStream('stream.log')
, streamy = new Stream(null, {
stream: writeStream
- })
+ });
- streamy.stream.should.equal(writeStream)
- })
+ streamy.stream.should.equal(writeStream);
+ });
it('should default to stdout', function () {
- var streamy = new Stream
+ var streamy = new Stream();
- streamy.stream.should.equal(process.stdout)
- })
+ streamy.stream.should.equal(process.stdout);
+ });
describe("#write", function () {
it('should trigger the write method of a stream', function () {
var stream = fixtures.stream()
, logger = new Logger({ base: false })
- , asserts = 0
+ , asserts = 0;
- logger.use(Stream, { stream: stream.dummy })
+ logger.use(Stream, { stream: stream.dummy });
stream.on('write', function (str) {
- ++asserts
- })
+ ++asserts;
+ });
- logger.log('testing testing')
- asserts.should.equal(1)
- })
+ logger.log('testing testing');
+ asserts.should.equal(1);
+ });
it('should write to writable streams', function () {
var stream = fixtures.stream()
, logger = new Logger({ base: false })
- , asserts = 0
+ , asserts = 0;
- stream.dummy.writable = false
+ stream.dummy.writable = false;
- logger.use(Stream, { stream: stream.dummy })
+ logger.use(Stream, { stream: stream.dummy });
stream.on('write', function (str) {
- ++asserts
- })
+ ++asserts;
+ });
- logger.log('testing testing')
- asserts.should.equal(0)
- })
- })
+ logger.log('testing testing');
+ asserts.should.equal(0);
+ });
+ });
describe('#close', function () {
it('should trigger the end method of a stream', function () {
var stream = fixtures.stream()
, logger = new Logger({ base: false })
- , asserts = 0
+ , asserts = 0;
- logger.use(Stream, { stream: stream.dummy })
+ logger.use(Stream, { stream: stream.dummy });
stream.on('end', function (str) {
- ++asserts
- })
+ ++asserts;
+ });
- logger.remove(Stream)
+ logger.remove(Stream);
- asserts.should.equal(1)
- })
- })
-})
+ asserts.should.equal(1);
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.