index.d.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Originally from Definitely Typed, see:
  2. // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/b4683d7/types/loglevel/index.d.ts
  3. // Original definitions by: Stefan Profanter <https://github.com/Pro>
  4. // Gabor Szmetanko <https://github.com/szmeti>
  5. // Christian Rackerseder <https://github.com/screendriver>
  6. declare const log: log.RootLogger;
  7. export = log;
  8. declare namespace log {
  9. /**
  10. * Log levels
  11. */
  12. interface LogLevel {
  13. TRACE: 0;
  14. DEBUG: 1;
  15. INFO: 2;
  16. WARN: 3;
  17. ERROR: 4;
  18. SILENT: 5;
  19. }
  20. /**
  21. * Possible log level numbers.
  22. */
  23. type LogLevelNumbers = LogLevel[keyof LogLevel];
  24. /**
  25. * Possible log level descriptors, may be string, lower or upper case, or number.
  26. */
  27. type LogLevelDesc = LogLevelNumbers | LogLevelNames | 'silent' | keyof LogLevel;
  28. type LogLevelNames =
  29. | 'trace'
  30. | 'debug'
  31. | 'info'
  32. | 'warn'
  33. | 'error';
  34. type LoggingMethod = (...message: any[]) => void;
  35. type MethodFactory = (methodName: LogLevelNames, level: LogLevelNumbers, loggerName: string | symbol) => LoggingMethod;
  36. interface RootLogger extends Logger {
  37. /**
  38. * If you're using another JavaScript library that exposes a 'log' global, you can run into conflicts with loglevel.
  39. * Similarly to jQuery, you can solve this by putting loglevel into no-conflict mode immediately after it is loaded
  40. * onto the page. This resets to 'log' global to its value before loglevel was loaded (typically undefined), and
  41. * returns the loglevel object, which you can then bind to another name yourself.
  42. */
  43. noConflict(): any;
  44. /**
  45. * This gets you a new logger object that works exactly like the root log object, but can have its level and
  46. * logging methods set independently. All loggers must have a name (which is a non-empty string or a symbol)
  47. * Calling * getLogger() multiple times with the same name will return an identical logger object.
  48. * In large applications, it can be incredibly useful to turn logging on and off for particular modules as you are
  49. * working with them. Using the getLogger() method lets you create a separate logger for each part of your
  50. * application with its own logging level. Likewise, for small, independent modules, using a named logger instead
  51. * of the default root logger allows developers using your module to selectively turn on deep, trace-level logging
  52. * when trying to debug problems, while logging only errors or silencing logging altogether under normal
  53. * circumstances.
  54. * @param name The name of the produced logger
  55. */
  56. getLogger(name: string | symbol): Logger;
  57. /**
  58. * This will return you the dictionary of all loggers created with getLogger, keyed off of their names.
  59. */
  60. getLoggers(): { [name: string]: Logger };
  61. /**
  62. * A .default property for ES6 default import compatibility
  63. */
  64. default: RootLogger;
  65. }
  66. interface Logger {
  67. /**
  68. * Available log levels.
  69. */
  70. readonly levels: LogLevel;
  71. /**
  72. * Plugin API entry point. This will be called for each enabled method each time the level is set
  73. * (including initially), and should return a MethodFactory to be used for the given log method, at the given level,
  74. * for a logger with the given name. If you'd like to retain all the reliability and features of loglevel, it's
  75. * recommended that this wraps the initially provided value of log.methodFactory
  76. */
  77. methodFactory: MethodFactory;
  78. /**
  79. * Output trace message to console.
  80. * This will also include a full stack trace
  81. *
  82. * @param msg any data to log to the console
  83. */
  84. trace(...msg: any[]): void;
  85. /**
  86. * Output debug message to console including appropriate icons
  87. *
  88. * @param msg any data to log to the console
  89. */
  90. debug(...msg: any[]): void;
  91. /**
  92. * Output debug message to console including appropriate icons
  93. *
  94. * @param msg any data to log to the console
  95. */
  96. log(...msg: any[]): void;
  97. /**
  98. * Output info message to console including appropriate icons
  99. *
  100. * @param msg any data to log to the console
  101. */
  102. info(...msg: any[]): void;
  103. /**
  104. * Output warn message to console including appropriate icons
  105. *
  106. * @param msg any data to log to the console
  107. */
  108. warn(...msg: any[]): void;
  109. /**
  110. * Output error message to console including appropriate icons
  111. *
  112. * @param msg any data to log to the console
  113. */
  114. error(...msg: any[]): void;
  115. /**
  116. * This disables all logging below the given level, so that after a log.setLevel("warn") call log.warn("something")
  117. * or log.error("something") will output messages, but log.info("something") will not.
  118. *
  119. * @param level as a string, like 'error' (case-insensitive) or as a number from 0 to 5 (or as log.levels. values)
  120. * @param persist Where possible the log level will be persisted. LocalStorage will be used if available, falling
  121. * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass
  122. * false as the optional 'persist' second argument, persistence will be skipped.
  123. */
  124. setLevel(level: LogLevelDesc, persist?: boolean): void;
  125. /**
  126. * Returns the current logging level, as a value from LogLevel.
  127. * It's very unlikely you'll need to use this for normal application logging; it's provided partly to help plugin
  128. * development, and partly to let you optimize logging code as below, where debug data is only generated if the
  129. * level is set such that it'll actually be logged. This probably doesn't affect you, unless you've run profiling
  130. * on your code and you have hard numbers telling you that your log data generation is a real performance problem.
  131. */
  132. getLevel(): LogLevel[keyof LogLevel];
  133. /**
  134. * This sets the current log level only if one has not been persisted and can’t be loaded. This is useful when
  135. * initializing scripts; if a developer or user has previously called setLevel(), this won’t alter their settings.
  136. * For example, your application might set the log level to error in a production environment, but when debugging
  137. * an issue, you might call setLevel("trace") on the console to see all the logs. If that error setting was set
  138. * using setDefaultLevel(), it will still say as trace on subsequent page loads and refreshes instead of resetting
  139. * to error.
  140. *
  141. * The level argument takes is the same values that you might pass to setLevel(). Levels set using
  142. * setDefaultLevel() never persist to subsequent page loads.
  143. *
  144. * @param level as a string, like 'error' (case-insensitive) or as a number from 0 to 5 (or as log.levels. values)
  145. */
  146. setDefaultLevel(level: LogLevelDesc): void;
  147. /**
  148. * This resets the current log level to the default level (or `warn` if no explicit default was set) and clears
  149. * the persisted level if one was previously persisted.
  150. */
  151. resetLevel(): void;
  152. /**
  153. * This enables all log messages, and is equivalent to log.setLevel("trace").
  154. *
  155. * @param persist Where possible the log level will be persisted. LocalStorage will be used if available, falling
  156. * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass
  157. * false as the optional 'persist' second argument, persistence will be skipped.
  158. */
  159. enableAll(persist?: boolean): void;
  160. /**
  161. * This disables all log messages, and is equivalent to log.setLevel("silent").
  162. *
  163. * @param persist Where possible the log level will be persisted. LocalStorage will be used if available, falling
  164. * back to cookies if not. If neither is available in the current environment (i.e. in Node), or if you pass
  165. * false as the optional 'persist' second argument, persistence will be skipped.
  166. */
  167. disableAll(persist?: boolean): void;
  168. }
  169. }