UNPKG

3.69 kBMarkdownView Raw
1# winston
2
3A MongoDB transport for [winston][0].
4
5Current version supports only mongodb driver version 3.x and winston 3.x. If you want to use
6winston-mongodb with mongodb version 1.4.x use winston-mongodb <1.x. For mongodb 2.x use
7winston-mongodb <3.x.
8
9## Motivation
10`tldr;?`: To break the [winston][0] codebase into small modules that work
11together.
12
13The [winston][0] codebase has been growing significantly with contributions and
14other logging transports. This is **awesome**. However, taking a ton of
15additional dependencies just to do something simple like logging to the Console
16and a File is overkill.
17
18## Usage
19``` js
20 var winston = require('winston');
21
22 /**
23 * Requiring `winston-mongodb` will expose
24 * `winston.transports.MongoDB`
25 */
26 require('winston-mongodb');
27
28 winston.add(new winston.transports.MongoDB(options));
29```
30
31The MongoDB transport takes the following options. 'db' is required:
32
33* __level:__ Level of messages that this transport should log, defaults to
34'info'.
35* __silent:__ Boolean flag indicating whether to suppress output, defaults to
36false.
37* __db:__ MongoDB connection uri, pre-connected `MongoClient` object or promise
38which resolves to a pre-connected `MongoClient` object.
39* __options:__ MongoDB connection parameters (optional, defaults to
40`{poolSize: 2, autoReconnect: true, useNewUrlParser: true}`).
41* __collection__: The name of the collection you want to store log messages in,
42defaults to 'log'.
43* __storeHost:__ Boolean indicating if you want to store machine hostname in
44logs entry, if set to true it populates MongoDB entry with 'hostname' field,
45which stores os.hostname() value.
46* __label:__ Label stored with entry object if defined.
47* __name:__ Transport instance identifier. Useful if you need to create multiple
48MongoDB transports.
49* __capped:__ In case this property is true, winston-mongodb will try to create
50new log collection as capped, defaults to false.
51* __cappedSize:__ Size of logs capped collection in bytes, defaults to 10000000.
52* __cappedMax:__ Size of logs capped collection in number of documents.
53* __tryReconnect:__ Will try to reconnect to the database in case of fail during
54initialization. Works only if __db__ is a string. Defaults to false.
55* __decolorize:__ Will remove color attributes from the log entry message,
56defaults to false.
57* __leaveConnectionOpen:__ Will leave MongoClient connected after transport shut down.
58* __metaKey:__ Configure which key is used to store metadata in the logged info object.
59Defaults to `'metadata'` to remain compatible with the [metadata format](https://github.com/winstonjs/logform/blob/master/examples/metadata.js)
60* __expireAfterSeconds:__ Seconds before the entry is removed. Works only if __capped__ is not set.
61
62*Metadata:* Logged as a native JSON object in 'meta' property.
63
64*Logging unhandled exceptions:* For logging unhandled exceptions specify
65winston-mongodb as `handleExceptions` logger according to winston documentation.
66
67## Querying and streaming logs
68
69Besides supporting the main options from winston, this transport supports the
70following extra options:
71
72* __includeIds:__ Whether the returned logs should include the `_id` attribute
73settled by mongodb, defaults to `false`.
74
75## Installation
76
77``` bash
78 $ npm install winston
79 $ npm install winston-mongodb
80```
81
82## [Changelog](https://github.com/winstonjs/winston-mongodb/releases)
83
84#### Author: [Charlie Robbins](http://blog.nodejitsu.com)
85#### Contributors: [Yurij Mikhalevich](https://github.com/yurijmikhalevich), [Kendrick Taylor](https://github.com/sktaylor), [Yosef Dinerstein](https://github.com/yosefd), [Steve Dalby](https://github.com/stevedalby)
86
87[0]: https://github.com/winstonjs/winston
88
\No newline at end of file