Skip to content

Commit

Permalink
Fix: Allow destructuring the individual log methods (fixes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Dec 10, 2018
1 parent a3b4ef2 commit ac6bfd1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -15,19 +15,19 @@ function getLogger(namespace) {
var logger = sparkles(namespace);

levels.forEach(function(level){
logger[level] = makeLogLevel(level);
logger[level] = makeLogLevel(logger, level);
});

return logger;
}

function makeLogLevel(level) {
function makeLogLevel(self, level) {
return function(msg){
if (typeof msg === 'string') {
msg = format.apply(null, arguments);
}

this.emit(level, msg);
self.emit(level, msg);
};
}

Expand Down
29 changes: 20 additions & 9 deletions test/index.js
Expand Up @@ -4,21 +4,21 @@ var expect = require('expect');

var glogg = require('../');

describe('glogg', function(){
describe('glogg', function() {

var logger;

beforeEach(function(done){
beforeEach(function(done) {
logger = glogg('glogg-test');
done();
});

afterEach(function(done){
afterEach(function(done) {
logger.remove();
done();
});

it('emits a debug event when debug method is called', function(done){
it('emits a debug event when debug method is called', function(done) {
logger.on('debug', function(msg){
expect(msg).toEqual('test');
done();
Expand All @@ -27,7 +27,7 @@ describe('glogg', function(){
logger.debug('test');
});

it('emits a info event when info method is called', function(done){
it('emits a info event when info method is called', function(done) {
logger.on('info', function(msg){
expect(msg).toEqual('test');
done();
Expand All @@ -36,7 +36,7 @@ describe('glogg', function(){
logger.info('test');
});

it('emits a warn event when warn method is called', function(done){
it('emits a warn event when warn method is called', function(done) {
logger.on('warn', function(msg){
expect(msg).toEqual('test');
done();
Expand All @@ -45,7 +45,7 @@ describe('glogg', function(){
logger.warn('test');
});

it('emits a error event when error method is called', function(done){
it('emits a error event when error method is called', function(done) {
logger.on('error', function(msg){
expect(msg).toEqual('test');
done();
Expand All @@ -54,7 +54,7 @@ describe('glogg', function(){
logger.error('test');
});

it('formats a string message with util.format syntax', function(done){
it('formats a string message with util.format syntax', function(done) {
logger.on('debug', function(msg){
expect(msg).toEqual('test something');
done();
Expand All @@ -63,7 +63,7 @@ describe('glogg', function(){
logger.debug('test %s', 'something');
});

it('does not format a non-string message', function(done){
it('does not format a non-string message', function(done) {
var expected = { test: 'something' };

logger.on('debug', function(msg){
Expand All @@ -73,4 +73,15 @@ describe('glogg', function(){

logger.debug(expected);
});

it('allows you to "destructure" the individual log-level functions', function(done) {
var debug = logger.debug;

logger.on('debug', function(msg){
expect(msg).toEqual('test');
done();
});

debug('test');
});
});

0 comments on commit ac6bfd1

Please sign in to comment.