DEV Community

Discussion on: A hassle-free, zero dependency way to handle console.log() & co. in a production Angular application

Collapse
devmyqi profile image
Michael Wronna • Edited on

Thanks Manuel,

logging is always the first feature I implement, I mostly use a own function like:

const log = function(level,message) {
  const logdate = function() {
    const date = new Date();
    return String(date.getHours()).padStart(2,'0') + ':'
      + String(date.getMinutes()).padStart(2,'0') + ':'
      + String(date.getSeconds()).padStart(2,'0') + '.'
      + String(date.getMilliseconds()).padStart(3,'0');
    };
    const logtype = function(level) {
      const logtypes = {1:'info',2:'info',4:'info',8:'warning',
          16:'error',32:'debug'};
      return logtypes[level] ? logtypes[level] : 'undef';
   };
   if ( config.loglevel & level ) {
     console.log(`${logdate()} [${logtype(level)}] (${level}) ${message}`);
   };
};

Sometimes, especially for client code I extend this function with:

  // inside log function
  const logcmd = function(level) {
    if ( level === 8 ) { return 'warn';
    } else if ( level === 16 ) { return 'error';
    } else { return 'log'; }
  };
  // and then log with
  console[logcmd(level)]('the log string');