jonschlinkert/log-utils

`log-utils` clashes with `eazy-logger` because it changes the system `console.log` function

Opened this issue · 3 comments

I added log-utils to my gulp tasks and browser-sync stopped working with the following error:

eazy-logger/index.js:224
    console.log.apply(console, args);
                ^

TypeError: console.log.apply is not a function

I console.logged console.log and, well, it is not a function once I imported log-utils:

{ error: [Getter],
  info: [Getter],
  success: [Getter],
  warning: [Getter],
  timestamp: [Getter],
  ok: [Function],
  heading: [Function] }

Without log-utils it returns:

[Function: bound consoleCall]

I guess this situation is quite rare considering that I couldn't google anything that would help me to trace the issue. So it'd better be logged somewhere.

I'm not sure why you use console.log as a base object for this module. The docs do not suggest any use for that.
I changed console.log to {} here

const log = console.log;
and it fixed the issue.

doowb commented

Hi @kapooostin thanks for the issue. I just ran into this too and will try to get a fix in place. Feel free to submit a PR with the fix if you'd like.

For some background... the ansi-colors package used to handle lazy loading differently and log-utils made use of that feature by extending it. During the refactor of log-utils, ansi-colors was updated to a version that does not require the same type of lazy loading.

Also, since the docs and tests all show passing the returned value from log to console.log, I think the update you're suggesting is a good one.

Hi @doowb
To pass the tests I have to use an empty function instead of an object.
If I ignore the first test checking for the type of the module export, then this code passes:

'use strict';

const timestamp = require('time-stamp');
const colors = require('ansi-colors');

const log = {
  error: colors.red(colors.symbols.cross),
  info: colors.cyan(colors.symbols.info),
  success: colors.green(colors.symbols.check),
  warning: colors.yellow(colors.symbols.warning),
  timestamp: () => {
    return '[' + colors.gray(timestamp('HH:mm:ss')) + ']';
  },
  ok: str => {
    let ok = colors.green(colors.symbols.check);
    return str.replace(/^(\s*)(.*?)$/, (m, s, v) => {
      return s + ok + ' ' + v;
    });
  },
  heading: (...args) => {
    let str = args.filter(v => v !== void 0).map(String).join(' ');
    return colors.bold.underline(str);
  },
};

log.__proto__ = colors;
module.exports = log;

Though the tests do not check the actual values and output of defined functions. But they test for the things, not mentioned in docs or examples, for instance for log.symbols property.

After the change above there is no lazy loading for the first four properties, as far as I understand.