# Code coverage report for lib/utils/log.js

## Statements: 73.33% (22 / 30) Branches: 30% (6 / 20) Functions: 83.33% (5 / 6) Lines: 73.33% (22 / 30)

All files » lib/utils/ » log.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    621
    1

    1

    1
    23

    23
    23

    1
    4
    2

    2
    2

    1
    6

    1
    13
    9

    1
    2

    1
    1

    1'use strict';
    var util = require('util'),
        colour = require('./colour'),
        bus = require('./bus'),
        required = false;

    var coding = {
      log: 'black',
      info: 'yellow',
      status: 'green',
      detail: 'yellow',
      fail: 'red',
      error: 'red'
    };

    function log(type, text) {
      var msg = colour(coding[type], '[nodemon] ' + (text||''));

      Eif (required) {
        bus.emit('log', { type: type, message: text, colour: msg });
      } else if (type === 'error') {
        util.error(msg);
      } else {
        util.log(msg);
      }
    }

    var Logger = function (r) {
      if (!(this instanceof Logger)) {
        return new Logger(r);
      }
      this.required(r);
      return this;
    };

    Object.keys(coding).forEach(function (type) {
      Logger.prototype[type] = log.bind(null, type);
    });

    // detail is for messages that are turned on during debug
    Logger.prototype.detail = function (msg) {
      if (this.debug) {
        log('detail', msg);
      }
    };

    Logger.prototype.required = function (val) {
      required = val;
    };

    Logger.prototype.debug = false;
    Logger.prototype._log = function (type, msg) {
      if (required) {
        bus.emit('log', { type: type, message: msg || '', colour: msg || '' });
      } else if (type === 'error') {
        util.error(msg);
      } else {
        console.log(msg || '');
      }
    };

    module.exports = Logger;