2

I want to disable console.log's like THREE.WebGLRenderer: Context Lost, OBJLoader: 1.8330078125ms and so on. Do you have any suggestions?

4 Answers4

6

There is no built-in way to disable these messages, with the exception of OBJLoader2.setLogging.

Messages like these are extremely useful for debugging, not just in your development environment, but also when your code is out in the field.

But if you're hard-set on eliminating these messages, you can redirect logging that uses the console object.

// Place this at the start of your code
const log = console.log;
console.log = () => {};
const warn = console.warn;
console.warn = () => {};
const error = console.error;
console.error = () => {};

With this, anything calling console.log, console.warn, and console.error will be silenced, even es6 modules outside the scope of your main file. This even applies to console message managers like debug.

But YOU can still write to the console by using the redirected functions. For example:

// In your code...
log("test message"); // will print "test message" to the console

This works only because you saved references to the original functions off into the variables like const log.

TheJim01
  • 8,411
  • 1
  • 30
  • 54
  • I now have so many THREE deprecation warnings that my entire computer didn't just lock up, it literally just up and hard-reset itself. These messages aren't helpful. I don't personally need to know that one of my libraries I'm using isn't set up for the version of THREE that another library is using. They should just have a 'check deprecation' mode that enables logging, or... just a 'verbose mode'.... like every other sane library out there. – Kyle Baker Jan 11 '21 at 00:20
2

Download the source, search and replace all console.log, console.warn, and console.error messages. Rebuild the library.

git clone https://github.com/mrdoob/three.js.git
cd three.js
npm install
find src -type f -name '*.js' -exec sed -i '' s/console\\\./\\/\\/console\\./ {} +
find examples/jsm -type f -name '*.js' -exec sed -i '' s/console\\\./\\/\\/console\\./ {} +
find examples/js -type f -name '*.js' -exec sed -i '' s/console\\\./\\/\\/console\\./ {} +
npm run build

note those search and replace lines assume each console line is on a single line by itself.

After searching and replacing (and before building) you can check the results with

git diff

If the results are not correct you can reset all the files to their previous state with

git reset --hard

and then try different expressions or use your favorite text editor's search and replace across files.

enter image description here

People often whine they don't want to change the source but three.js in particular is arguably about changing the source. Three's policy is that you should download a particular version of three.js and write your code to match that specific version. They are then free to break anything and everything with each new version. They make no effort to stay backward compatible between versions so hack your version however you need it to be hacked for your needs.

In the case above, modifying the library is especially trivial given you can practically automate it so if you do take a newer version, after you've fixed all the new incompatibilities you can run these steps again.

gman
  • 100,619
  • 31
  • 269
  • 393
2

The short answer is something like this, which is a spin off of code suggested by mr.doob himself, ironically:

const vrgc = {};

vrgc.console = {
  log: console.log,
  info: console.info,
  warn: console.warn,
  error: console.error,
};

Object.keys(vrgc.console)
.forEach(
  key => {
    console.warn(`hiding console.${key} calls from THREE`)

    console[key] = function() {
      if ( (typeof arguments[ 0 ] === 'string') && (arguments[ 0 ].substr( 0, 5 ) === 'THREE') ) {
          return
          // ignore THREE
      } 
      else {
         const originalFunc = vrgc.console[key];
         originalFunc.apply( console, arguments );
      }
    }
  }
);

Unfortunately, this request has a loooong history of 6+ years... see here, here, here, here, et al...

Kyle Baker
  • 3,424
  • 2
  • 23
  • 33
0

Adds a logging parameter to the WebGL renderer.

Useful for situations where you don't want logging at all such as production or in a testing environment. Retains true as default but would think false would be a better option? https://github.com/mrdoob/three.js/pull/5835

  • The PR you linked was closed, not merged, and was for a version now five years old. The discussion the surrounding the PR was relevant, but you should make that clear in your answer. – TheJim01 Nov 21 '19 at 14:21