logger.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. var levels = require('./levels')
  3. , util = require('util')
  4. , events = require('events')
  5. , DEFAULT_CATEGORY = '[default]';
  6. var logWritesEnabled = true;
  7. /**
  8. * Models a logging event.
  9. * @constructor
  10. * @param {String} categoryName name of category
  11. * @param {Log4js.Level} level level of message
  12. * @param {Array} data objects to log
  13. * @param {Log4js.Logger} logger the associated logger
  14. * @author Seth Chisamore
  15. */
  16. function LoggingEvent (categoryName, level, data, logger) {
  17. this.startTime = new Date();
  18. this.categoryName = categoryName;
  19. this.data = data;
  20. this.level = level;
  21. this.logger = logger;
  22. }
  23. /**
  24. * Logger to log messages.
  25. * use {@see Log4js#getLogger(String)} to get an instance.
  26. * @constructor
  27. * @param name name of category to log to
  28. * @author Stephan Strittmatter
  29. */
  30. function Logger (name, level) {
  31. this.category = name || DEFAULT_CATEGORY;
  32. if (level) {
  33. this.setLevel(level);
  34. }
  35. }
  36. util.inherits(Logger, events.EventEmitter);
  37. Logger.DEFAULT_CATEGORY = DEFAULT_CATEGORY;
  38. Logger.prototype.level = levels.TRACE;
  39. Logger.prototype.setLevel = function(level) {
  40. this.level = levels.toLevel(level, this.level || levels.TRACE);
  41. };
  42. Logger.prototype.removeLevel = function() {
  43. delete this.level;
  44. };
  45. Logger.prototype.log = function() {
  46. var args = Array.prototype.slice.call(arguments)
  47. , logLevel = levels.toLevel(args.shift(), levels.INFO)
  48. , loggingEvent;
  49. if (this.isLevelEnabled(logLevel)) {
  50. loggingEvent = new LoggingEvent(this.category, logLevel, args, this);
  51. this.emit("log", loggingEvent);
  52. }
  53. };
  54. Logger.prototype.isLevelEnabled = function(otherLevel) {
  55. return this.level.isLessThanOrEqualTo(otherLevel);
  56. };
  57. ['Trace','Debug','Info','Warn','Error','Fatal', 'Mark'].forEach(
  58. function(levelString) {
  59. var level = levels.toLevel(levelString);
  60. Logger.prototype['is'+levelString+'Enabled'] = function() {
  61. return this.isLevelEnabled(level);
  62. };
  63. Logger.prototype[levelString.toLowerCase()] = function () {
  64. if (logWritesEnabled && this.isLevelEnabled(level)) {
  65. var args = Array.prototype.slice.call(arguments);
  66. args.unshift(level);
  67. Logger.prototype.log.apply(this, args);
  68. }
  69. };
  70. }
  71. );
  72. /**
  73. * Disable all log writes.
  74. * @returns {void}
  75. */
  76. function disableAllLogWrites() {
  77. logWritesEnabled = false;
  78. }
  79. /**
  80. * Enable log writes.
  81. * @returns {void}
  82. */
  83. function enableAllLogWrites() {
  84. logWritesEnabled = true;
  85. }
  86. exports.LoggingEvent = LoggingEvent;
  87. exports.Logger = Logger;
  88. exports.disableAllLogWrites = disableAllLogWrites;
  89. exports.enableAllLogWrites = enableAllLogWrites;