How-to guide·DebuggingJavaScript

JavaScript logging

Set the log level of all BIM.works JavaScript libraries from code with log.setLogger, or from the browser console with bimworks.setLogLevel, optionally persisted in localStorage.

The SDK, the viewers and the shared libraries all log through one logger, log from @sascom/commonjs. This guide changes its level.

Before you start#

The levels, from quiet to verbose, are NONE, ERROR, WARN, INFO and DEBUG. A development build of the libraries starts at INFO; the production builds start at NONE, so an application normally sees nothing unless it asks.

From your code#

JavaScript#
import {log} from "@sascom/commonjs";

log.setLogger("ERROR");   // errors only, from now on
log.info("not shown");
log.error("shown");

setLogger takes the level as a string and replaces the logger immediately for every library in the page. It does not persist anything.

From the browser console#

For live debugging without touching the application, the libraries put a bimworks object on window:

JavaScript#
bimworks.setLogLevel("DEBUG");         // this page load only
bimworks.setLogLevel("DEBUG", true);   // and remember it: stored as localStorage "bimworks_loglevel"
bimworks.getLogLevel();                // prints the active logger

With the second argument true, the level is written to the bimworks_loglevel item of localStorage and read back on every load of any page on that origin that uses the libraries, until you change it or remove the item (localStorage.removeItem("bimworks_loglevel")). An unknown level is an error, reported in the console.

Check#

After bimworks.setLogLevel("DEBUG", true) and a reload, loading a model prints the viewer's progress messages in the console.