|
1 |
| -glogg |
2 |
| -===== |
| 1 | +# glogg |
| 2 | + |
| 3 | +[](https://travis-ci.org/undertakerjs/glogg) |
3 | 4 |
|
4 | 5 | Global logging utility
|
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +```js |
| 10 | +var getLogger = require('glogg'); |
| 11 | + |
| 12 | +var logger = getLogger('my-namespace'); |
| 13 | + |
| 14 | +// logs strings |
| 15 | +logger.debug('The MOST verbose!'); |
| 16 | +logger.info('Some important info'); |
| 17 | +logger.warn('All the warnings to you'); |
| 18 | +logger.error('OH NO! SOMETHING HAPPENED!'); |
| 19 | + |
| 20 | +// supports util.format! |
| 21 | +logger.info('%s style!', 'printf'); |
| 22 | + |
| 23 | +// log anything |
| 24 | +logger.debug({ my: 'obj' }); |
| 25 | +logger.info([1, 2, 3]); |
| 26 | + |
| 27 | +// somewhere else |
| 28 | +logger.on('info', function(msg){ |
| 29 | + // do something with msg |
| 30 | +}); |
| 31 | + |
| 32 | +// must be handled to avoid crashing process |
| 33 | +logger.on('error', function(msg){ |
| 34 | + // now it won't crash |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +## API |
| 39 | + |
| 40 | +__Note: This module makes no assumptions about the log levels and they will always |
| 41 | +be emitted. If you are looking to filter some out, your listeners will need to have |
| 42 | +extra logic.__ |
| 43 | + |
| 44 | +### getLogger([namespace]) |
| 45 | + |
| 46 | +Create a new logger at the given namespace (or the default if no namespace is provided). |
| 47 | +Returns an augmented [`sparkles`](https://github.com/phated/sparkles) EventEmitter object |
| 48 | +with 4 methods: `debug()`, `info()`, `warn()` and `error()`. When called, these methods emit |
| 49 | +an event with the same name. If the first argument is a string, the arguments |
| 50 | +are passed through node's `util.format()` before being emitted. Other parts |
| 51 | +of a node program can get the logger by namespace and listen for the events to |
| 52 | +be emitted. |
| 53 | + |
| 54 | +#### logger.debug(msg) |
| 55 | + |
| 56 | +Emits a `debug` event with the given `msg`. |
| 57 | + |
| 58 | +If the first argument is a string, all arguments are passed to node's |
| 59 | +`util.format()` before being emitted. |
| 60 | + |
| 61 | +#### logger.info(msg) |
| 62 | + |
| 63 | +Emits a `info` event with the given `msg`. |
| 64 | + |
| 65 | +If the first argument is a string, all arguments are passed to node's |
| 66 | +`util.format()` before being emitted. |
| 67 | + |
| 68 | +#### logger.warn(msg) |
| 69 | + |
| 70 | +Emits a `warn` event with the given `msg`. |
| 71 | + |
| 72 | +If the first argument is a string, all arguments are passed to node's |
| 73 | +`util.format()` before being emitted. |
| 74 | + |
| 75 | +#### logger.error(msg) |
| 76 | + |
| 77 | +Emits a `error` event with the given `msg`. |
| 78 | + |
| 79 | +If the first argument is a string, all arguments are passed to node's |
| 80 | +`util.format()` before being emitted. |
| 81 | + |
| 82 | +__Note: You must handle this event in some way or the node process will crash |
| 83 | +when an `error` event is emitted.__ |
| 84 | + |
| 85 | +#### logger.on(event, fn) |
| 86 | + |
| 87 | +Standard API from node's `EventEmitter`. Use this to listen for events from |
| 88 | +the logger methods. |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +MIT |
0 commit comments