Logger.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // let _level: string | undefined = undefined;
  2. import { isNode } from "../platform";
  3. export enum LogLevel {
  4. NONE = "none",
  5. ERROR = "error",
  6. WARN = "warn",
  7. INFO = "info",
  8. DEBUG = "debug",
  9. }
  10. export class Logger {
  11. private levels = ["error", "warn", "info", "debug"];
  12. private readonly isBrowser: boolean;
  13. private colors: {
  14. warn: string;
  15. debug: string;
  16. start: string;
  17. end: string;
  18. error: string;
  19. info: string;
  20. };
  21. public messageFormat: string;
  22. private _logLevel: LogLevel;
  23. public tzOffset: number;
  24. constructor(level?: LogLevel) {
  25. // if (!_level) {
  26. // _level = level || "info"; // defaults to info
  27. // }
  28. this._logLevel = level || LogLevel.INFO;
  29. this.isBrowser = !isNode;
  30. if (!this.isBrowser) {
  31. this.colors = {
  32. start: "\x1b[2m",
  33. warn: "\x1b[35m",
  34. info: "\x1b[33m",
  35. debug: "\x1b[36m",
  36. error: "\x1b[31m",
  37. end: "\x1b[0m",
  38. };
  39. } else {
  40. this.colors = {
  41. start: "%c",
  42. warn: "color : #ff00ff",
  43. info: "color : #ffff00",
  44. debug: "color : #00ffff",
  45. error: "color : #ff0000",
  46. end: "",
  47. };
  48. }
  49. this.messageFormat = "[%t] [%l] - [%m]";
  50. this.tzOffset = new Date().getTimezoneOffset() * 60000;
  51. }
  52. /**
  53. *
  54. * @param level {string}
  55. * @returns {boolean}
  56. */
  57. canSend(level: LogLevel) {
  58. return this._logLevel
  59. ? this.levels.indexOf(this._logLevel) >= this.levels.indexOf(level)
  60. : false;
  61. }
  62. /**
  63. * @param message {string}
  64. */
  65. warn(message: string) {
  66. this._log(LogLevel.WARN, message, this.colors.warn);
  67. }
  68. /**
  69. * @param message {string}
  70. */
  71. info(message: string) {
  72. this._log(LogLevel.INFO, message, this.colors.info);
  73. }
  74. /**
  75. * @param message {string}
  76. */
  77. debug(message: string) {
  78. this._log(LogLevel.DEBUG, message, this.colors.debug);
  79. }
  80. /**
  81. * @param message {string}
  82. */
  83. error(message: string) {
  84. this._log(LogLevel.ERROR, message, this.colors.error);
  85. }
  86. format(message: string, level: string) {
  87. return this.messageFormat
  88. .replace("%t", this.getDateTime())
  89. .replace("%l", level.toUpperCase())
  90. .replace("%m", message);
  91. }
  92. get logLevel() {
  93. return this._logLevel;
  94. }
  95. setLevel(level: LogLevel) {
  96. this._logLevel = level;
  97. }
  98. static setLevel(level: string) {
  99. console.log(
  100. "Logger.setLevel is deprecated, it will has no effect. Please, use client.setLogLevel instead."
  101. );
  102. }
  103. /**
  104. * @param level {string}
  105. * @param message {string}
  106. * @param color {string}
  107. */
  108. _log(level: LogLevel, message: string, color: string) {
  109. if (this.canSend(level)) {
  110. this.log(level, message, color);
  111. } else {
  112. return;
  113. }
  114. }
  115. /**
  116. * Override this function for custom Logger. <br />
  117. *
  118. * @remarks use `this.isBrowser` to check and handle for different environment.
  119. * @param level {string}
  120. * @param message {string}
  121. * @param color {string}
  122. */
  123. log(level: LogLevel, message: string, color: string) {
  124. if (!this.isBrowser) {
  125. console.log(color + this.format(message, level) + this.colors.end);
  126. } else {
  127. console.log(this.colors.start + this.format(message, level), color);
  128. }
  129. }
  130. getDateTime() {
  131. return new Date(Date.now() - this.tzOffset).toISOString().slice(0, -1);
  132. }
  133. }