index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. var colorspace = require('colorspace')
  3. , enabled = require('enabled')
  4. , kuler = require('kuler')
  5. , util = require('util');
  6. /**
  7. * Check if the terminal we're using allows the use of colors.
  8. *
  9. * @type {Boolean}
  10. * @private
  11. */
  12. var tty = require('tty').isatty(1);
  13. /**
  14. * The default stream instance we should be writing against.
  15. *
  16. * @type {Stream}
  17. * @public
  18. */
  19. var stream = process.stdout;
  20. /**
  21. * A simple environment based logger.
  22. *
  23. * Options:
  24. *
  25. * - colors: Force the use of colors or forcefully disable them. If this option
  26. * is not supplied the colors will be based on your terminal.
  27. * - stream: The Stream instance we should write our logs to, defaults to
  28. * process.stdout but can be anything you like.
  29. *
  30. * @param {String} name The namespace of your log function.
  31. * @param {Object} options Logger configuration.
  32. * @returns {Function} Configured logging method.
  33. * @api public
  34. */
  35. function factory(name, options) {
  36. if (!enabled(name)) return function diagnopes() {};
  37. options = options || {};
  38. options.colors = 'colors' in options ? options.colors : tty;
  39. options.ansi = options.colors ? kuler(name, colorspace(name)) : name;
  40. options.stream = options.stream || stream;
  41. //
  42. // Allow multiple streams, so make sure it's an array which makes iteration
  43. // easier.
  44. //
  45. if (!Array.isArray(options.stream)) {
  46. options.stream = [options.stream];
  47. }
  48. //
  49. // The actual debug function which does the logging magic.
  50. //
  51. return function debug(line) {
  52. //
  53. // Better formatting for error instances.
  54. //
  55. if (line instanceof Error) line = line.stack || line.message || line;
  56. line = [
  57. //
  58. // Add the colorized namespace.
  59. //
  60. options.ansi,
  61. //
  62. // The total time we took to execute the next debug statement.
  63. //
  64. ' ',
  65. line
  66. ].join('');
  67. //
  68. // Use util.format so we can follow the same API as console.log.
  69. //
  70. line = util.format.apply(this, [line].concat(
  71. Array.prototype.slice.call(arguments, 1)
  72. )) + '\n';
  73. options.stream.forEach(function each(stream) {
  74. stream.write(line);
  75. });
  76. };
  77. }
  78. /**
  79. * Override the "default" stream that we write to. This allows you to globally
  80. * configure the steams.
  81. *
  82. * @param {Stream} output
  83. * @returns {function} Factory
  84. * @api private
  85. */
  86. factory.to = function to(output) {
  87. stream = output;
  88. return factory;
  89. };
  90. //
  91. // Expose the module.
  92. //
  93. module.exports = factory;