Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,9 @@ console.log(info);

### Errors

The `errors` format allows you to pass in an instance of a JavaScript `Error` directly to the logger.
It allows you to specify whether not to include the stack-trace.
The `errors` format allows you to pass in an instance of a JavaScript `Error`
directly to the logger. It allows you to specify whether not to include the
stack-trace.

```js
const { format } = require('logform');
Expand All @@ -304,6 +305,32 @@ console.log(info);
// at REPLServer.Interface._line (readline.js:631:8)
```

It will also handle `{ message }` properties as `Error` instances:

```js
const { format } = require('logform');
const { errors } = format;

const errorsFormat = errors({ stack: true })

const info = errorsFormat.transform({
message: new Error('Oh no!')
});

console.log(info);
// Error: Oh no!
// at repl:1:13
// at ContextifyScript.Script.runInThisContext (vm.js:50:33)
// at REPLServer.defaultEval (repl.js:240:29)
// at bound (domain.js:301:14)
// at REPLServer.runBound [as eval] (domain.js:314:12)
// at REPLServer.onLine (repl.js:468:10)
// at emitOne (events.js:121:20)
// at REPLServer.emit (events.js:211:7)
// at REPLServer.Interface._onLine (readline.js:282:10)
// at REPLServer.Interface._line (readline.js:631:8)
```

### JSON

The `json` format uses `fast-safe-stringify` to finalize the message.
Expand Down
30 changes: 22 additions & 8 deletions errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const format = require('./format');
const { MESSAGE } = require('triple-beam');
const { LEVEL, MESSAGE } = require('triple-beam');

/*
* function errors (info)
Expand All @@ -11,15 +11,29 @@ const { MESSAGE } = require('triple-beam');
*
* Optionally, the Error's `stack` property can also be appended to the `info` object.
*/
module.exports = format((info, opts) => {
if (!(info.message instanceof Error)) return info;
module.exports = format((einfo, { stack }) => {
if (einfo instanceof Error) {
const info = Object.assign({}, einfo, {
level: einfo.level,
[LEVEL]: einfo[LEVEL] || einfo.level,
message: einfo.message,
[MESSAGE]: einfo[MESSAGE] || einfo.message
});

const err = info.message;
if (stack) info.stack = einfo.stack;
return info;
}

info.message = err.message;
info[MESSAGE] = err.message;
if (!(einfo.message instanceof Error)) return einfo;

if (opts.stack) info.stack = err.stack;
// Assign all enumerable properties and the
// message property from the error provided.
Object.assign(einfo, einfo.message);
const err = einfo.message;
einfo.message = err.message;
einfo[MESSAGE] = err.message;

return info;
// Assign the stack if requested.
if (stack) einfo.stack = err.stack;
return einfo;
});
74 changes: 37 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading