flaschenpost
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -53,25 +53,2 @@ 'use strict'; | ||
Configuration.prototype.setModule = function (module) { | ||
if (!module) { | ||
throw new Error('Module is missing.'); | ||
} | ||
if (!module.name) { | ||
throw new Error('Module name is missing.'); | ||
} | ||
if (!module.version) { | ||
throw new Error('Module version is missing.'); | ||
} | ||
if (this.module) { | ||
throw new Error('Module already set.'); | ||
} | ||
this.module = { | ||
name: module.name, | ||
version: module.version | ||
}; | ||
}; | ||
module.exports = Configuration; |
'use strict'; | ||
var _ = require('lodash'); | ||
var path = require('path'); | ||
var _ = require('lodash'), | ||
findRoot = require('find-root'); | ||
var Configuration = require('./Configuration'), | ||
@@ -47,6 +50,12 @@ FormatterHumanReadable = require('./formatters/HumanReadable'), | ||
if (!that.configuration.module) { | ||
throw new Error('Module is missing.'); | ||
if (!source) { | ||
throw new Error('Source is missing.'); | ||
} | ||
try { | ||
that.configuration.module = require(path.join(findRoot(source), 'package.json')); | ||
} catch (e) { | ||
throw new Error('Could not find package.json.'); | ||
} | ||
_.forOwn(that.configuration.levels, function (levelOptions, levelName) { | ||
@@ -53,0 +62,0 @@ if (!levelOptions.enabled) { |
@@ -8,4 +8,5 @@ 'use strict'; | ||
var Middleware = function (level, options) { | ||
var Middleware = function (level, source) { | ||
var flaschenpost = require('../flaschenpost'); | ||
var options; | ||
@@ -16,5 +17,9 @@ if (!level) { | ||
options = options || {}; | ||
if (!source) { | ||
throw new Error('Source is missing.'); | ||
} | ||
options = {}; | ||
options.objectMode = true; | ||
options.source = options.source || 'express'; | ||
options.source = source; | ||
@@ -21,0 +26,0 @@ Writable.call(this, options); |
{ | ||
"name": "flaschenpost", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "flaschenpost is a logger for cloud-based applications.", | ||
@@ -22,2 +22,3 @@ "contributors": [ | ||
"chalk": "0.5.1", | ||
"find-root": "0.1.1", | ||
"lodash": "2.4.1", | ||
@@ -24,0 +25,0 @@ "markup-js": "1.5.21", |
@@ -19,28 +19,7 @@ # flaschenpost | ||
You need to provide the name and version of your application. For that call the `use` function and specify an appropriate object. | ||
```javascript | ||
flaschenpost.use('module', { | ||
name: 'foo', | ||
version: '0.0.1' | ||
}); | ||
``` | ||
Please note that if you specify additional properties, these properties are ignored. This allows you to hand over your `package.json` file. | ||
```javascript | ||
flaschenpost.use('module', require('./package.json')); | ||
``` | ||
### Using a logger | ||
Once you have set up flaschenpost, you can call its `getLogger` function to acquire a logger. | ||
Next, call the `getLogger` function to acquire a logger. You have to provide `__filename` as parameter, as this value will be used to detect the appropriate `package.json` file and extract information about the current module. | ||
```javascript | ||
var logger = flaschenpost.getLogger(); | ||
``` | ||
If you want to use multiple loggers in your application, you may provide a string as parameter for `getLogger` that identifies the source from where you send log messages. This *may* be the name of the current file. | ||
```javascript | ||
var logger = flaschenpost.getLogger(__filename); | ||
@@ -129,7 +108,7 @@ ``` | ||
For that, provide the `stream` property when setting up morgan and call the `Middleware` constructor function with the desired log level. | ||
For that, provide the `stream` property when setting up morgan and call the `Middleware` constructor function with the desired log level and the path to the `package.json` file that shall be used for the module information. | ||
```javascript | ||
app.use(morgan('combined', { | ||
stream: new flaschenpost.Middleware('info') | ||
stream: new flaschenpost.Middleware('info', __filename) | ||
})); | ||
@@ -136,0 +115,0 @@ ``` |
@@ -60,10 +60,12 @@ 'use strict'; | ||
var configuration = new Configuration(), | ||
spy = sinon.spy(configuration, 'setModule'); | ||
spy = sinon.spy(configuration, 'setLevels'); | ||
var expected = { | ||
name: 'foo', | ||
version: '0.0.1' | ||
info: { | ||
color: 'green', | ||
enabled: true | ||
} | ||
}; | ||
configuration.set('module', expected); | ||
configuration.set('levels', expected); | ||
@@ -73,3 +75,3 @@ assert.that(spy.calledOnce, is.true()); | ||
configuration.setModule.restore(); | ||
configuration.setLevels.restore(); | ||
done(); | ||
@@ -180,82 +182,2 @@ }); | ||
}); | ||
suite('setModule', function () { | ||
test('is a function.', function (done) { | ||
var configuration = new Configuration(); | ||
assert.that(configuration.setModule, is.ofType('function')); | ||
done(); | ||
}); | ||
test('throws an error if module is missing.', function (done) { | ||
var configuration = new Configuration(); | ||
assert.that(function () { | ||
configuration.setModule(); | ||
}, is.throwing('Module is missing.')); | ||
done(); | ||
}); | ||
test('throws an error if module name is missing.', function (done) { | ||
var configuration = new Configuration(); | ||
assert.that(function () { | ||
configuration.setModule({ version: '0.0.1' }); | ||
}, is.throwing('Module name is missing.')); | ||
done(); | ||
}); | ||
test('throws an error if module version is missing.', function (done) { | ||
var configuration = new Configuration(); | ||
assert.that(function () { | ||
configuration.setModule({ name: 'foo' }); | ||
}, is.throwing('Module version is missing.')); | ||
done(); | ||
}); | ||
test('sets the given module.', function (done) { | ||
var expected = { | ||
name: 'foo', | ||
version: '0.0.1' | ||
}; | ||
var configuration = new Configuration(); | ||
configuration.setModule(expected); | ||
assert.that(configuration.module, is.equalTo(expected)); | ||
done(); | ||
}); | ||
test('ignores additional values.', function (done) { | ||
var expected = { | ||
name: 'foo', | ||
version: '0.0.1' | ||
}; | ||
var input = { | ||
name: 'foo', | ||
description: 'The ultimate foo module.', | ||
version: '0.0.1' | ||
}; | ||
var configuration = new Configuration(); | ||
configuration.setModule(input); | ||
assert.that(configuration.module, is.equalTo(expected)); | ||
done(); | ||
}); | ||
test('throws an error if the module was already configured.', function (done) { | ||
var configuration = new Configuration(); | ||
configuration.setModule({ | ||
name: 'foo', | ||
version: '0.0.1' | ||
}); | ||
assert.that(function () { | ||
configuration.setModule({ | ||
name: 'foo', | ||
version: '0.0.1' | ||
}); | ||
}, is.throwing('Module already set.')); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -6,4 +6,3 @@ 'use strict'; | ||
var flaschenpost = require('../lib/flaschenpost'), | ||
letter = require('../lib/letter'), | ||
packageJson = require('../package.json'); | ||
letter = require('../lib/letter'); | ||
@@ -47,12 +46,25 @@ suite('flaschenpost', function () { | ||
test('throws an error if no module has been set.', function (done) { | ||
test('throws an error if source is missing.', function (done) { | ||
assert.that(function () { | ||
flaschenpost.getLogger(); | ||
}, is.throwing('Module is missing.')); | ||
}, is.throwing('Source is missing.')); | ||
done(); | ||
}); | ||
test('throws an error if source is not a valid path.', function (done) { | ||
assert.that(function () { | ||
flaschenpost.getLogger('foobar'); | ||
}, is.throwing('Could not find package.json.')); | ||
done(); | ||
}); | ||
test('throws an error if given path does not have a package.json file.', function (done) { | ||
assert.that(function () { | ||
flaschenpost.getLogger('/'); | ||
}, is.throwing('Could not find package.json.')); | ||
done(); | ||
}); | ||
test('returns an object.', function (done) { | ||
flaschenpost.use('module', packageJson); | ||
assert.that(flaschenpost.getLogger(), is.ofType('object')); | ||
assert.that(flaschenpost.getLogger(__filename), is.ofType('object')); | ||
done(); | ||
@@ -62,5 +74,3 @@ }); | ||
test('has the levels as log functions.', function (done) { | ||
var logger; | ||
flaschenpost.use('module', packageJson); | ||
logger = flaschenpost.getLogger(); | ||
var logger = flaschenpost.getLogger(__filename); | ||
assert.that(logger.fatal, is.ofType('function')); | ||
@@ -76,5 +86,3 @@ assert.that(logger.error, is.ofType('function')); | ||
test('throws an error when no message is given.', function (done) { | ||
var logger; | ||
flaschenpost.use('module', packageJson); | ||
logger = flaschenpost.getLogger(); | ||
var logger = flaschenpost.getLogger(__filename); | ||
assert.that(function () { | ||
@@ -87,5 +95,3 @@ logger.info(); | ||
test('throws an error when message is not a string.', function (done) { | ||
var logger; | ||
flaschenpost.use('module', packageJson); | ||
logger = flaschenpost.getLogger(); | ||
var logger = flaschenpost.getLogger(__filename); | ||
assert.that(function () { | ||
@@ -98,5 +104,3 @@ logger.info(42); | ||
test('writes the message to a letter.', function (done) { | ||
var logger; | ||
flaschenpost.use('module', packageJson); | ||
logger = flaschenpost.getLogger(__filename); | ||
var logger = flaschenpost.getLogger(__filename); | ||
@@ -110,4 +114,4 @@ letter.once('data', function (paragraph) { | ||
assert.that(paragraph.module, is.equalTo({ | ||
name: packageJson.name, | ||
version: packageJson.version | ||
name: 'foo', | ||
version: '0.0.1' | ||
})); | ||
@@ -133,9 +137,5 @@ assert.that(paragraph.source, is.equalTo(__filename)); | ||
test('does not write a message if the log level is disabled.', function (done) { | ||
var counter, | ||
logger; | ||
var logger = flaschenpost.getLogger(__filename); | ||
flaschenpost.use('module', packageJson); | ||
logger = flaschenpost.getLogger(); | ||
counter = 0; | ||
var counter = 0; | ||
letter.once('data', function () { | ||
@@ -142,0 +142,0 @@ counter++; |
@@ -16,7 +16,2 @@ 'use strict'; | ||
flaschenpost.initialize(); | ||
flaschenpost.use('module', { | ||
name: 'foo', | ||
version: '0.0.1' | ||
}); | ||
}); | ||
@@ -38,6 +33,15 @@ | ||
test('throws an error if source is missing.', function (done) { | ||
assert.that(function () { | ||
/*eslint-disable no-new*/ | ||
new Middleware('info'); | ||
/*eslint-enable no-new*/ | ||
}, is.throwing('Source is missing.')); | ||
done(); | ||
}); | ||
test('throws an error if the specified level does not exist.', function (done) { | ||
assert.that(function () { | ||
/*eslint-disable no-new*/ | ||
new Middleware('foo'); | ||
new Middleware('foo', __filename); | ||
/*eslint-enable no-new*/ | ||
@@ -49,3 +53,3 @@ }, is.throwing('Level is invalid.')); | ||
test('returns a writable stream.', function (done) { | ||
assert.that(new Middleware('info'), is.instanceOf(Writable)); | ||
assert.that(new Middleware('info', __filename), is.instanceOf(Writable)); | ||
done(); | ||
@@ -55,3 +59,3 @@ }); | ||
test('writes messages using the specified log level.', function (done) { | ||
var middleware = new Middleware('info'); | ||
var middleware = new Middleware('info', __filename); | ||
@@ -70,24 +74,2 @@ letter.once('data', function (data) { | ||
}); | ||
test('uses \'express\' as default source name.', function (done) { | ||
var middleware = new Middleware('info'); | ||
letter.once('data', function (data) { | ||
assert.that(data.source, is.equalTo('express')); | ||
done(); | ||
}); | ||
middleware.write('foobar'); | ||
}); | ||
test('uses the given source name if one is specified.', function (done) { | ||
var middleware = new Middleware('info', { source: 'express /baz' }); | ||
letter.once('data', function (data) { | ||
assert.that(data.source, is.equalTo('express /baz')); | ||
done(); | ||
}); | ||
middleware.write('foobar'); | ||
}); | ||
}); |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
24
36460
6
974
131
1
+ Addedfind-root@0.1.1
+ Addedfind-root@0.1.1(transitive)