Skip to content

Commit

Permalink
docs: add example of uncaughtRejections logging (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriHeiko committed Apr 30, 2020
1 parent df25fa2 commit 3d07a80
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions README.md
Expand Up @@ -102,6 +102,8 @@ logger to use throughout your application if you so choose.
* [Exceptions](#exceptions)
* [Handling Uncaught Exceptions with winston](#handling-uncaught-exceptions-with-winston)
* [To Exit or Not to Exit](#to-exit-or-not-to-exit)
* [Rejections](#rejections)
* [Handling Uncaught Promise Rejections with winston](#handling-uncaught-promise-rejections-with-winston)
* [Profiling](#profiling)
* [Streaming Logs](#streaming-logs)
* [Querying Logs](#querying-logs)
Expand Down Expand Up @@ -888,6 +890,61 @@ const logger = winston.createLogger({ exitOnError: ignoreEpipe });
logger.exitOnError = ignoreEpipe;
```

## Rejections

### Handling Uncaught Promise Rejections with winston

With `winston`, it is possible to catch and log `uncaughtRejection` events
from your process. With your own logger instance you can enable this behavior
when it's created or later on in your applications lifecycle:

``` js
const { createLogger, transports } = require('winston');

// Enable rejection handling when you create your logger.
const logger = createLogger({
transports: [
new transports.File({ filename: 'combined.log' })
],
rejectionHandlers: [
new transports.File({ filename: 'rejections.log' })
]
});

// Or enable it later on by adding a transport or using `.rejections.handle`
const logger = createLogger({
transports: [
new transports.File({ filename: 'combined.log' })
]
});

// Call rejections.handle with a transport to handle rejections
logger.rejections.handle(
new transports.File({ filename: 'rejections.log' })
);
```

If you want to use this feature with the default logger, simply call
`.rejections.handle()` with a transport instance.

``` js
//
// You can add a separate rejection logger by passing it to `.rejections.handle`
//
winston.rejections.handle(
new winston.transports.File({ filename: 'path/to/rejections.log' })
);

//
// Alternatively you can set `handleRejections` to true when adding transports
// to winston.
//
winston.add(new winston.transports.File({
filename: 'path/to/combined.log',
handleRejections: true
}));
```

## Profiling

In addition to logging messages and metadata, `winston` also has a simple
Expand Down

0 comments on commit 3d07a80

Please sign in to comment.