diff --git a/README.md b/README.md index 584d0e897..9af9f5759 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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