All files / src/utils Logger.js

100% Statements 20/20
93.75% Branches 15/16
100% Functions 3/3
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                22x                                                                         46x         46x         46x 46x 24x 24x             22x 132x 132x                   132x     4x 4x 2x 2x 2x   2x       2x 1x   2x          
import chalk from 'chalk';
 
// - Trace: The unimportant detail about how the process run. You may hardly use it.
// - Debug: A debug message for processing information to help during troubleshooting.
// - Info: Generally useful information to log (service start/stop, configuration assumptions, etc).
// - Warn: Anything that can potentially cause application oddities.
// - Error: Error has happend, but the user can still use the system after fixing the error.
// - Fatal: The system is unusable.
const levels = [
  {
    level: 'Trace',
    color: chalk.gray
  },
  {
    level: 'Debug',
    color: chalk.hex('#D3D3D3')
  },
  {
    level: 'Info',
    color: chalk.hex('#3753d3')
  },
  {
    level: 'Warn',
    color: chalk.yellow
  },
  {
    level: 'Error',
    color: chalk.hex('#cf342f')
  },
  {
    level: 'Fatal',
    color: chalk.hex('#cf0014')
  }
];
 
class Logger {
  /**
   * Constructs a new Logger instance.
   * @param {Object.<string, any>} props - Logger properties.
   */
  constructor(props) {
    /**
     * Symbol for the logger.
     * @type {string}
     */
    this.symbol = '';
    /**
     * Name of the logger.
     * @type {string}
     */
    this.name = '';
    /**
     * Determines whether to log messages.
     * @type {boolean}
     */
    this.log = props.log !== undefined ? props.log : true; // determin whether console.log or not
    if (!props.onlyWords) {
      this.symbol = '';
      this.name = props.name;
    }
  }
}
 
// The Logger's prototype's method 'info' 'warn' etc. are compatible with console.log
// So you can use it as console.log
levels.forEach(item => {
  const { level, color } = item;
  const fnName = level.toLocaleLowerCase();
 
  /**
   * Logs an error message.
   * @function
   * @memberof Logger.prototype
   * @param {string} firstParam - The first parameter to log.
   * @param {...any} rest - Additional parameters to log.
   * @returns {string} - The formatted log message.
   */
  Logger.prototype[fnName] = function fn(firstParam, ...rest) {
    // if (typeof params === 'obejct') params = JSON.stringify(params);
 
    let prefix = `${this.symbol ? this.symbol + ' ' : ''}${this.name ? this.name + ' ' : ''}[${level}]: `;
    if (typeof firstParam === 'object' && firstParam !== null) {
      prefix += '\n';
      Eif (this.log) {
        console.log(color(prefix), firstParam, ...rest);
      }
      return chalk(color(prefix), firstParam, ...rest);
    }
    // To compatible with the situation below, We need to surround the rest with method color
    // logger.error('Your Node.js version is needed to >= %s', '10.1');
    if (this.log) {
      console.log(color(prefix + firstParam), color(...rest));
    }
    return chalk(color(prefix + firstParam), color(...rest));
  };
});
 
export default Logger;