Change console.log timestamp in react-native or nodejs apps

Viewed 532

After recent updates the log format in the console on react-native and maybe node apps, has become like:

enter image description here

This results after doing a simple console.log() call. In previous versions the timestamp format was different, and a lot shorter. Is there a way to customize that date format?

I know what day is today, time only would save lots of space on my monitor.

I got no luck looking into the docs/source of react-native-cli.

Environment: npm 6.14.8, react 16.13.1, react-native 0.63.4

1 Answers

Searching a bit i found 3 ways to check logs:

  1. Using android studio, the log format on the logcat can be changed using the settings icon, and you can remove the date
  2. Using react-native log-android which formats differently the log using the logkitty package. example scrn of the log prefix outcome:

log-android outcome

  1. The default format on the metro bundler exists inside node_modules/metro/src/lib/formatLogTimestamp.js It is currently (as of version 0.59):
module.exports = date =>
  chalk.dim(
    `[${date.toDateString()} ${date.toLocaleTimeString("en-US", {
      hour12: false
    })}.${String(date.getMilliseconds()).padEnd(3, "0")}] `
  );

Change that to:

module.exports = date =>
  chalk.dim(
    `[${date.toLocaleTimeString("en-US", {
      hour12: false
    })}.${String(date.getMilliseconds()).padEnd(3, "0")}] `
  );

After the change you need to restart metro, and voila! you have only the time without the date. log without date

(Obviously you need to make the change every time you remove/install the metro package)

Related