Colors in JavaScript console

前端 未结 27 2486
说谎
说谎 2020-11-22 11:42

Can Chrome\'s built-in JavaScript console display colors?

I want errors in red, warnings in orange and console.log\'s in green. Is that possible?

27条回答
  •  一生所求
    2020-11-22 12:04

    const coloring = fn => ({ background, color = 'white' }) => (...text) => fn(`%c${text.join('')}`, `color:${color};background:${background}`);
    const colors = {
      primary: '#007bff',
      success: '#28a745',
      warning: '#ffc107',
      danger: '#dc3545',
      info: '#17a2b8',
    };
    const dir = (key = '', value = {}) => {
      logs.primary(`++++++++++++start:${key}++++++++++++++`);
      console.dir(value);
      logs.primary(`++++++++++++end:${key}++++++++++++++`);
    };
    const logs = Object.keys(colors)
      .reduce((prev, curr) => ({ ...prev, [curr]: coloring(console.log)({ background: colors[curr] }) }), { dir });
      
      logs.success('hello succes');
      logs.warning('hello fail');

提交回复
热议问题