Logger.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. class Logger {
  2. _prefix: string;
  3. /**
  4. * specify prefix
  5. * @param {string} prefix
  6. */
  7. constructor(prefix: string) {
  8. this._prefix = prefix;
  9. }
  10. _isEmpty(value: any) {
  11. return value === null || value === undefined || value === '';
  12. }
  13. _baseLog(method = 'log', ...args: any[]) {
  14. if (typeof console[method] === 'function') {
  15. const messages = [...args];
  16. if (!this._isEmpty(this._prefix)) {
  17. messages.unshift(this._prefix, ':');
  18. }
  19. console[method](...messages);
  20. }
  21. }
  22. /* istanbul ignore next */
  23. log(...args: any[]) {
  24. this._baseLog('log', ...args);
  25. }
  26. /* istanbul ignore next */
  27. warn(...args: any[]) {
  28. this._baseLog('warn', ...args);
  29. }
  30. /* istanbul ignore next */
  31. error(...args: any[]) {
  32. this._baseLog('error', ...args);
  33. }
  34. /* istanbul ignore next */
  35. info(...args: any[]) {
  36. this._baseLog('info', ...args);
  37. }
  38. }
  39. export default Logger;