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
| import log from 'loglevel';
function setColor(message, methodName, loggerName) { const mapping = { trace: '#389e0d', debug: '#f70', info: '#1890FF' }; if (mapping[methodName] && typeof window !== 'undefined') { return [ `%c${methodName}-${loggerName || 'Global'}`, `background: ${mapping[methodName]};color: #fff;padding: 3px 5px;margin-right: 5px`, ].concat(message); } else { return message; } }
function sendLogMsg(message, methodName, loggerName) { setTimeout(() => { console.log('已发送', message, methodName, loggerName); }, 3000); }
function initLogger() { const originalFactory = log.methodFactory; log.methodFactory = (methodName, logLevel, loggerName) => { const rawMethod = originalFactory(methodName, logLevel, loggerName); const that = this; return function () { let logMsg = Array.prototype.slice.call(arguments); that.sendLogMsg(logMsg, methodName, loggerName); rawMethod.apply(this, that.setColor(logMsg, methodName, loggerName)); }; }; log.setLevel(process.env.NODE_ENV === 'development' ? 0 : 'warn'); }
|