Skip to content

Commit 7aa3733

Browse files
committed
Merge pull request #118 from holidayextras/debug-override
Allow overriding debug function.
2 parents 511f5a4 + 619fb4c commit 7aa3733

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- 2016-05-05 - v1.5.0
2+
- 2016-05-05 - Allow overriding debug output functions
3+
- 2016-04-27 - Static code analysis with Flow and JSCPD
14
- 2016-04-22 - v1.4.0
25
- 2016-04-22 - Return all validation errors
36
- 2016-02-16 - v1.3.3

documentation/debugging.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ $ DEBUG=jsonApi:handler:find npm test
2525
```
2626
$ DEBUG=jsonApi:handler:* npm test
2727
```
28+
29+
### Integration with application logging
30+
31+
If you wish to integrate `jsonapi-server` debug output with your application's logging solution, you can override the functions that are invoked for outputting the messages for the supported namespaces by invoking the package's `debugging.__overrideDebugOutput(outputFnFactory)` function where the `outputFnFactory` parameter is a function that expects a namespace string parameter and returns an output function for the namespace.
32+
33+
A simple example of this functionality in action would be:
34+
35+
```javascript
36+
var debugging = require("jsonapi-server/lib/debugging");
37+
38+
var outputFnfactory = function(namespace) {
39+
return function(message) {
40+
console.log(namespace + ">>>", message);
41+
}
42+
}
43+
44+
debugging.__overrideDebugOutput(outputFnfactory);
45+
```

lib/debugging.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
/* @flow weak */
22
"use strict";
3-
module.exports = {
3+
4+
var debug = require("debug");
5+
6+
function overrideDebugOutputHelper(debugFns, outputFnFactory) {
7+
Object.keys(debugFns).filter(function(key) {
8+
return (key.substr(0, 2) !== "__");
9+
}).forEach(function(key) {
10+
if (debugFns[key] instanceof Function) {
11+
debugFns[key] = outputFnFactory(debugFns[key].namespace);
12+
return null;
13+
}
14+
return overrideDebugOutputHelper(debugFns[key], outputFnFactory);
15+
});
16+
}
17+
18+
var debugging = module.exports = {
419
handler: {
5-
search: require("debug")("jsonApi:handler:search"),
6-
find: require("debug")("jsonApi:handler:find"),
7-
create: require("debug")("jsonApi:handler:create"),
8-
update: require("debug")("jsonApi:handler:update"),
9-
delete: require("debug")("jsonApi:handler:delete")
20+
search: debug("jsonApi:handler:search"),
21+
find: debug("jsonApi:handler:find"),
22+
create: debug("jsonApi:handler:create"),
23+
update: debug("jsonApi:handler:update"),
24+
delete: debug("jsonApi:handler:delete")
1025
},
11-
include: require("debug")("jsonApi:include"),
12-
filter: require("debug")("jsonApi:filter"),
13-
validationInput: require("debug")("jsonApi:validation:input"),
14-
validationOutput: require("debug")("jsonApi:validation:output"),
15-
validationError: require("debug")("jsonApi:validation:error"),
16-
errors: require("debug")("jsonApi:errors"),
17-
requestCounter: require("debug")("jsonApi:requestCounter")
26+
include: debug("jsonApi:include"),
27+
filter: debug("jsonApi:filter"),
28+
validationInput: debug("jsonApi:validation:input"),
29+
validationOutput: debug("jsonApi:validation:output"),
30+
validationError: debug("jsonApi:validation:error"),
31+
errors: debug("jsonApi:errors"),
32+
requestCounter: debug("jsonApi:requestCounter"),
33+
34+
__overrideDebugOutput: function() {}
1835
};
36+
37+
debugging.__overrideDebugOutput = overrideDebugOutputHelper.bind(null, debugging);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapi-server",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "A config driven NodeJS framework implementing json:api",
55
"keywords": [
66
"jsonapi",

0 commit comments

Comments
 (0)