| [81bc7da] | 1 | var colour = require('./colour');
|
|---|
| 2 | var bus = require('./bus');
|
|---|
| 3 | var required = false;
|
|---|
| 4 | var useColours = true;
|
|---|
| 5 |
|
|---|
| 6 | var coding = {
|
|---|
| 7 | log: 'black',
|
|---|
| 8 | info: 'yellow',
|
|---|
| 9 | status: 'green',
|
|---|
| 10 | detail: 'yellow',
|
|---|
| 11 | fail: 'red',
|
|---|
| 12 | error: 'red',
|
|---|
| 13 | };
|
|---|
| 14 |
|
|---|
| 15 | function log(type, text) {
|
|---|
| 16 | var msg = '[nodemon] ' + (text || '');
|
|---|
| 17 |
|
|---|
| 18 | if (useColours) {
|
|---|
| 19 | msg = colour(coding[type], msg);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | // always push the message through our bus, using nextTick
|
|---|
| 23 | // to help testing and get _out of_ promises.
|
|---|
| 24 | process.nextTick(() => {
|
|---|
| 25 | bus.emit('log', { type: type, message: text, colour: msg });
|
|---|
| 26 | });
|
|---|
| 27 |
|
|---|
| 28 | // but if we're running on the command line, also echo out
|
|---|
| 29 | // question: should we actually just consume our own events?
|
|---|
| 30 | if (!required) {
|
|---|
| 31 | if (type === 'error') {
|
|---|
| 32 | console.error(msg);
|
|---|
| 33 | } else {
|
|---|
| 34 | console.log(msg || '');
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | var Logger = function (r) {
|
|---|
| 40 | if (!(this instanceof Logger)) {
|
|---|
| 41 | return new Logger(r);
|
|---|
| 42 | }
|
|---|
| 43 | this.required(r);
|
|---|
| 44 | return this;
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | Object.keys(coding).forEach(function (type) {
|
|---|
| 48 | Logger.prototype[type] = log.bind(null, type);
|
|---|
| 49 | });
|
|---|
| 50 |
|
|---|
| 51 | // detail is for messages that are turned on during debug
|
|---|
| 52 | Logger.prototype.detail = function (msg) {
|
|---|
| 53 | if (this.debug) {
|
|---|
| 54 | log('detail', msg);
|
|---|
| 55 | }
|
|---|
| 56 | };
|
|---|
| 57 |
|
|---|
| 58 | Logger.prototype.required = function (val) {
|
|---|
| 59 | required = val;
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | Logger.prototype.debug = false;
|
|---|
| 63 | Logger.prototype._log = function (type, msg) {
|
|---|
| 64 | if (required) {
|
|---|
| 65 | bus.emit('log', { type: type, message: msg || '', colour: msg || '' });
|
|---|
| 66 | } else if (type === 'error') {
|
|---|
| 67 | console.error(msg);
|
|---|
| 68 | } else {
|
|---|
| 69 | console.log(msg || '');
|
|---|
| 70 | }
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | Object.defineProperty(Logger.prototype, 'useColours', {
|
|---|
| 74 | set: function (val) {
|
|---|
| 75 | useColours = val;
|
|---|
| 76 | },
|
|---|
| 77 | get: function () {
|
|---|
| 78 | return useColours;
|
|---|
| 79 | },
|
|---|
| 80 | });
|
|---|
| 81 |
|
|---|
| 82 | module.exports = Logger;
|
|---|