log-utils icon indicating copy to clipboard operation
log-utils copied to clipboard

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

Open kapooostin opened this issue 4 years ago • 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.

kapooostin avatar Aug 08 '20 13:08 kapooostin

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 https://github.com/jonschlinkert/log-utils/blob/57283513c63bdb97ec733669879c3b257ce4b770/index.js#L11 and it fixed the issue.

kapooostin avatar Aug 08 '20 14:08 kapooostin

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.

doowb avatar Aug 11 '20 16:08 doowb

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.

kapooostin avatar Aug 12 '20 10:08 kapooostin