megahertz / electron-log

Simple logging module Electron/Node.js/NW.js application. No dependencies. No complicated configuration.
MIT License
1.32k stars 128 forks source link

Color and text disappear when using custom console transport format. #397

Closed NotLazy closed 10 months ago

NotLazy commented 10 months ago

When I use the default console transport format, I get color and my logged messages:

image

But when I use a custom console transport format:

image

Here's my custom format: %c{h}:{i}:{s}.{ms}%c {sender} › {text} sender is defined using log.variables.sender = currentContext; before calling log.silly() or log.info() (the two levels shown in screenshots)

megahertz commented 10 months ago
  log.hooks.push((msg, _, transportName) => {
    if (transportName === 'console') {
      msg.data.unshift('color: blue', 'color: unset');
    }

    return msg;
  });
NotLazy commented 9 months ago
  log.hooks.push((msg, _, transportName) => {
    if (transportName === 'console') {
      msg.data.unshift('color: blue', 'color: unset');
    }

    return msg;
  });

This works for console but causes file and remote handlers to have this text in them, even though specifically locking it with transportName

in the log file:

[2024-02-05 22:09:11.408] [info] (main) color: cyan color: unset App is ready and has been initialized
[2024-02-05 22:09:11.998] [info] (app) color: cyan color: unset Loading 15
[2024-02-05 22:09:12.145] [info] (app) color: cyan color: unset Loading 17
[2024-02-05 22:09:12.961] [info] (app) color: cyan color: unset loaded
[2024-02-05 22:09:12.967] [info] (app) color: cyan color: unset loaded
megahertz commented 9 months ago

Ok, try clonning msg object instead

log.hooks.push((msg, _, transportName) => {
  if (transportName === 'console') {
    return {
      ...msg,
      data: ['color: blue', 'color: unset', ...msg.data],
    }
  }

  return msg;
});
NotLazy commented 9 months ago

Ok, try clonning msg object instead

This worked. I had tried only cloning msg or only cloning msg.data but, cloning both was the answer here. Thanks!