Skip to content

Commit 7cd1f17

Browse files
authored
Merge pull request #15 from KurtPattyn/develop
Add enrich option to karl configuration
2 parents feefb95 + 65feabe commit 7cd1f17

File tree

5 files changed

+1735
-9
lines changed

5 files changed

+1735
-9
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ To disable this redirection, set the `redirectConsole` option to `false`.
148148
karl.setOptions({ redirectConsole: false });
149149
```
150150

151+
## Log Enrichtment
152+
Applications can add extra information to a message at the moment it is logged.
153+
One example usage is adding request information to the log messages in an express application.
154+
155+
```javascript
156+
karl.setOption({
157+
enrich: addRequestInformation,
158+
json: true
159+
});
160+
161+
function addRequestInformation(msg) {
162+
//fetch request information from somewhere, e.g. by using continuation local storage
163+
const request = context.get("request");
164+
msg.headers = request.headers;
165+
}
166+
167+
app.use((req, res, next) => {
168+
console.log("Entering application");
169+
});
170+
```
171+
172+
```sh
173+
{"timestamp":"2015-08-02T18:02:39.456Z","level":"DEBUG","hostName":"<hidden>","process":{"name":"karltest","pid":26693},"message":"Entering application","fileName":"karltest.js","lineNumber":41,"functionName":"<anonymous>", "headers": { "host": "localhost", ... }}
174+
```
175+
151176
## UncaughtException
152177
Karl catches any uncaught exception (see [Event 'uncaughtException'](https://nodejs.org/api/process.html#process_event_uncaughtexception)).
153178
When such an exception occurs, Karl logs a fatal log message (including stack trace) and then gracefully shuts down the process by emitting a `SIGINT` signal.

lib/karl.js

+5
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ LOG_LEVELS.forEach(function(level) {
370370
lineNumber: caller.getLineNumber(),
371371
functionName: caller.getFunctionName() || "<anonymous>"
372372
};
373+
374+
if (logOptions.enrich) {
375+
logOptions.enrich(message);
376+
}
373377
let msg = format(message) + "\n";
374378

375379
if (logOptions.colorize) {
@@ -430,6 +434,7 @@ if (defaultLogOptions.redirectConsole) {
430434
* @param {Boolean} [options.colorize=false] When true, karl outputs error and fatal messages in red, warning errors in yellow and other messages in the current text color.
431435
* @param {Boolean} [options.redirectConsole=true] When true, console messages are redirected through karl (optionally including location information and adding color).
432436
* @param {Boolean} [options.json=true] When true, messages are printed as json messages, otherwise plain text is printed.
437+
* @param {Function} [options.enrich] An optional callback method that can be used to add additional information to a log message. The enrich method takes a message JSON struct as parameter. The enrich method should directly change the message object and does not have to return anything.
433438
*
434439
* @public
435440
*/

0 commit comments

Comments
 (0)