Logger.ts 917 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. log(...args: any[]) {
  23. this._baseLog('log', ...args);
  24. }
  25. warn(...args: any[]) {
  26. this._baseLog('warn', ...args);
  27. }
  28. error(...args: any[]) {
  29. this._baseLog('error', ...args);
  30. }
  31. info(...args: any[]) {
  32. this._baseLog('info', ...args);
  33. }
  34. }
  35. export default Logger;