Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Logger fixes
Browse files Browse the repository at this point in the history
Custom loggers were not being used, and only one log level string can be used.

Cleaning up stormpath.init() tests while I’m at it.
  • Loading branch information
Robert committed Mar 21, 2016
1 parent 42739f9 commit 1e50890
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
6 changes: 3 additions & 3 deletions docs/configuration.rst
Expand Up @@ -248,13 +248,13 @@ Logging
By default, this library will create a `Winston`_ logger and use this for
logging error messages to standard output.

While actively developing your application, you may want to include the ``info``
level for debugging purposes:
While actively developing your application, you may want to increase to the
``info`` level for debugging purposes:

.. code-block:: javascript
app.use(stormpath.init(app, {
debug: 'info, error'
debug: 'info'
}));
If you want to supply your own Winston logger, you can do that as well:
Expand Down
7 changes: 4 additions & 3 deletions lib/stormpath.js
Expand Up @@ -27,20 +27,21 @@ var userAgent = 'stormpath-express/' + version + ' ' + 'express/' + expressVersi
function initClient(app, opts) {
opts.userAgent = userAgent;

var logger = app.get('stormpathLogger');
var logger = opts.logger;

if (!logger) {
logger = new winston.Logger({
transports: [
new winston.transports.Console({
colorize: true,
level: opts.debug ? 'info' : 'error'
level: opts.debug || 'error'
})
]
});
app.set('stormpathLogger', logger);
}

app.set('stormpathLogger', logger);

var client = require('./client')(opts);

client.on('error', function (err) {
Expand Down
10 changes: 10 additions & 0 deletions test/helpers.js
Expand Up @@ -192,3 +192,13 @@ module.exports.destroyApplication = function (application, callback) {
});
});
};

/**
* A logger implementation that does not do anything, useful for tests where you
* want to silence the default logger.
* @type {Object}
*/
module.exports.noOpLogger = {
info: function () {},
error: function () {}
};
76 changes: 47 additions & 29 deletions test/test-stormpath.js
Expand Up @@ -3,61 +3,79 @@
var assert = require('assert');
var express = require('express');
var helpers = require('./helpers');
var stormpath = require('../index');

describe('#init()', function () {
it('should export stormpath.init when express-stormpath is required', function () {
assert.doesNotThrow(function () {
require('../index').init;
}, Error);
});
var applicationConfigurationErrorRegExp = new RegExp(/Please specify your Stormpath Application in your configuration/);

describe('stormpath.init()', function () {
var stormpathApplication;

it('should should emit a stormpath.ready event when ready', function (done) {

before(function (done) {
helpers.createApplication(helpers.createClient(), function (err, application) {
if (err) {
return done(err);
}
stormpathApplication = application;
done();
});
});


var app = helpers.createStormpathExpressApp({ application: { href: application.href } });
after(function (done) {
helpers.destroyApplication(stormpathApplication, done);
});

app.on('stormpath.error', done);

app.on('stormpath.ready', function () {
helpers.destroyApplication(application, done);
});
it('should export stormpath.init when express-stormpath is required', function () {

assert.doesNotThrow(function () {
require('../index').init;
}, Error);

});


it('should should emit a stormpath.ready event when ready', function (done) {

var app = helpers.createStormpathExpressApp({ application: stormpathApplication });

app.on('stormpath.ready', function () {
done();
});

});

// Can't figure out how to properly test an async throw...
it.skip('should throw an error when an invalid configuration is supplied', function (done) {
var stormpath = require('../index');

it('should emit an error if an application is not defined', function (done) {

var app = express();

app.on('stormpath.error', function (err) {
assert.equal(err !== null);
assert(err && err.message.match(applicationConfigurationErrorRegExp));
done();
});

app.on('stormpath.ready', done);
app.use(stormpath.init(app, {
logger: helpers.noOpLogger
}));

app.use(stormpath.init(app, { application: {}, client: {} }));
});

it('should not throw an error if a valid configuration is supplied', function (done) {

helpers.createApplication(helpers.createClient(), function (err, application) {
if (err) {
return done(err);
}
it('should allow me to define a custom logger', function (done) {

var app = helpers.createStormpathExpressApp({ application: { href: application.href } });
var app = express();

app.on('stormpath.error', done);
app.use(stormpath.init(app, {
logger: {
error: function (err) {
assert(err && err.message.match(applicationConfigurationErrorRegExp));
done();
}
}
}));

app.on('stormpath.ready', function () {
helpers.destroyApplication(application, done);
});
});
});

});

0 comments on commit 1e50890

Please sign in to comment.