logger.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Logger = exports.LoggerLevel = void 0;
  4. const util_1 = require("util");
  5. const error_1 = require("./error");
  6. const utils_1 = require("./utils");
  7. // Filters for classes
  8. const classFilters = {};
  9. let filteredClasses = {};
  10. let level;
  11. // Save the process id
  12. const pid = process.pid;
  13. // current logger
  14. // eslint-disable-next-line no-console
  15. let currentLogger = console.warn;
  16. /** @public */
  17. exports.LoggerLevel = Object.freeze({
  18. ERROR: 'error',
  19. WARN: 'warn',
  20. INFO: 'info',
  21. DEBUG: 'debug',
  22. error: 'error',
  23. warn: 'warn',
  24. info: 'info',
  25. debug: 'debug'
  26. });
  27. /**
  28. * @public
  29. */
  30. class Logger {
  31. /**
  32. * Creates a new Logger instance
  33. *
  34. * @param className - The Class name associated with the logging instance
  35. * @param options - Optional logging settings
  36. */
  37. constructor(className, options) {
  38. options = options !== null && options !== void 0 ? options : {};
  39. // Current reference
  40. this.className = className;
  41. // Current logger
  42. if (!(options.logger instanceof Logger) && typeof options.logger === 'function') {
  43. currentLogger = options.logger;
  44. }
  45. // Set level of logging, default is error
  46. if (options.loggerLevel) {
  47. level = options.loggerLevel || exports.LoggerLevel.ERROR;
  48. }
  49. // Add all class names
  50. if (filteredClasses[this.className] == null) {
  51. classFilters[this.className] = true;
  52. }
  53. }
  54. /**
  55. * Log a message at the debug level
  56. *
  57. * @param message - The message to log
  58. * @param object - Additional meta data to log
  59. */
  60. debug(message, object) {
  61. if (this.isDebug() &&
  62. ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
  63. (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
  64. const dateTime = new Date().getTime();
  65. const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'DEBUG', this.className, pid, dateTime, message);
  66. const state = {
  67. type: exports.LoggerLevel.DEBUG,
  68. message,
  69. className: this.className,
  70. pid,
  71. date: dateTime
  72. };
  73. if (object)
  74. state.meta = object;
  75. currentLogger(msg, state);
  76. }
  77. }
  78. /**
  79. * Log a message at the warn level
  80. *
  81. * @param message - The message to log
  82. * @param object - Additional meta data to log
  83. */
  84. warn(message, object) {
  85. if (this.isWarn() &&
  86. ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
  87. (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
  88. const dateTime = new Date().getTime();
  89. const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'WARN', this.className, pid, dateTime, message);
  90. const state = {
  91. type: exports.LoggerLevel.WARN,
  92. message,
  93. className: this.className,
  94. pid,
  95. date: dateTime
  96. };
  97. if (object)
  98. state.meta = object;
  99. currentLogger(msg, state);
  100. }
  101. }
  102. /**
  103. * Log a message at the info level
  104. *
  105. * @param message - The message to log
  106. * @param object - Additional meta data to log
  107. */
  108. info(message, object) {
  109. if (this.isInfo() &&
  110. ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
  111. (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
  112. const dateTime = new Date().getTime();
  113. const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'INFO', this.className, pid, dateTime, message);
  114. const state = {
  115. type: exports.LoggerLevel.INFO,
  116. message,
  117. className: this.className,
  118. pid,
  119. date: dateTime
  120. };
  121. if (object)
  122. state.meta = object;
  123. currentLogger(msg, state);
  124. }
  125. }
  126. /**
  127. * Log a message at the error level
  128. *
  129. * @param message - The message to log
  130. * @param object - Additional meta data to log
  131. */
  132. error(message, object) {
  133. if (this.isError() &&
  134. ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
  135. (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))) {
  136. const dateTime = new Date().getTime();
  137. const msg = (0, util_1.format)('[%s-%s:%s] %s %s', 'ERROR', this.className, pid, dateTime, message);
  138. const state = {
  139. type: exports.LoggerLevel.ERROR,
  140. message,
  141. className: this.className,
  142. pid,
  143. date: dateTime
  144. };
  145. if (object)
  146. state.meta = object;
  147. currentLogger(msg, state);
  148. }
  149. }
  150. /** Is the logger set at info level */
  151. isInfo() {
  152. return level === exports.LoggerLevel.INFO || level === exports.LoggerLevel.DEBUG;
  153. }
  154. /** Is the logger set at error level */
  155. isError() {
  156. return level === exports.LoggerLevel.ERROR || level === exports.LoggerLevel.INFO || level === exports.LoggerLevel.DEBUG;
  157. }
  158. /** Is the logger set at error level */
  159. isWarn() {
  160. return (level === exports.LoggerLevel.ERROR ||
  161. level === exports.LoggerLevel.WARN ||
  162. level === exports.LoggerLevel.INFO ||
  163. level === exports.LoggerLevel.DEBUG);
  164. }
  165. /** Is the logger set at debug level */
  166. isDebug() {
  167. return level === exports.LoggerLevel.DEBUG;
  168. }
  169. /** Resets the logger to default settings, error and no filtered classes */
  170. static reset() {
  171. level = exports.LoggerLevel.ERROR;
  172. filteredClasses = {};
  173. }
  174. /** Get the current logger function */
  175. static currentLogger() {
  176. return currentLogger;
  177. }
  178. /**
  179. * Set the current logger function
  180. *
  181. * @param logger - Custom logging function
  182. */
  183. static setCurrentLogger(logger) {
  184. if (typeof logger !== 'function') {
  185. throw new error_1.MongoInvalidArgumentError('Current logger must be a function');
  186. }
  187. currentLogger = logger;
  188. }
  189. /**
  190. * Filter log messages for a particular class
  191. *
  192. * @param type - The type of filter (currently only class)
  193. * @param values - The filters to apply
  194. */
  195. static filter(type, values) {
  196. if (type === 'class' && Array.isArray(values)) {
  197. filteredClasses = {};
  198. values.forEach(x => (filteredClasses[x] = true));
  199. }
  200. }
  201. /**
  202. * Set the current log level
  203. *
  204. * @param newLevel - Set current log level (debug, warn, info, error)
  205. */
  206. static setLevel(newLevel) {
  207. if (newLevel !== exports.LoggerLevel.INFO &&
  208. newLevel !== exports.LoggerLevel.ERROR &&
  209. newLevel !== exports.LoggerLevel.DEBUG &&
  210. newLevel !== exports.LoggerLevel.WARN) {
  211. throw new error_1.MongoInvalidArgumentError(`Argument "newLevel" should be one of ${(0, utils_1.enumToString)(exports.LoggerLevel)}`);
  212. }
  213. level = newLevel;
  214. }
  215. }
  216. exports.Logger = Logger;
  217. //# sourceMappingURL=logger.js.map