Skip to content

ChartFactor Toolkit logs

Introduction

The logs of the ChartFactor Toolkit are a set of messages generated by ChartFactor Toolkit components. These messages are useful for debugging and monitoring the ChartFactor Toolkit behavior. These logs are sent to the the browser console or the Node console, depending on the environment.

Setting log levels

ChartFactor Toolkit components can generate logs of the levels below:

  • trace: The most detailed log level. It is used to log detailed information
  • debug: This log level is used to log debugging information
  • info: This log level is used to log informational messages
  • warn: This log level is used to log warning messages
  • error: This log level is used to log error messages

By default, the ChartFactor Toolkit log level is set to warn. This means that only warning and error messages are printed. To change the log level, you can use the method below:

cf.setLogLevel(level, module)

The level parameter is any of the supported log level values enumerated above. The module parameter is optional and is the name of the module to set the log level for. Supported values are:

  • aktive
  • interaction-manager
  • standard
  • tables
  • geo-map-gl
  • geo-map
  • geo-map-v2

If the module parameter is not provided, the log level applies for all modules. However, this does not override the log level of modules that were specifically set using the module parameter.

Note

In the browser environment, log levels are persisted in local storage using the loglevel key and the loglevel:{module} key.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// for all modules
cf.setLogLevel("debug");

// for a specific module
cf.setLogLevel("info", "aktive");
cf.setLogLevel("info", "interaction-manager");
cf.setLogLevel("info", "standard");
cf.setLogLevel("info", "tables");
cf.setLogLevel("info", "geo-map-gl");
cf.setLogLevel("info", "geo-map");
cf.setLogLevel("info", "geo-map-v2");

Checking log levels

To check the actual log level of a module, you can use the method below:

cf.getLogLevel(module)

The module parameter is optional and it is the name of the module to get the log level for. Supported values are:

  • aktive
  • interaction-manager
  • standard
  • tables
  • geo-map-gl
  • geo-map
  • geo-map-v2

If the module parameter is not provided, the log level for all modules is returned.

Logging messages

To use the ChartFactor Toolkit to log messages, you can use the cf.log object. This object has the following methods:

  • trace(message): Logs a message at the trace level.
  • debug(message): Logs a message at the debug level.
  • info(message): Logs a message at the info level.
  • warn(message): Logs a message at the warn level.
  • error(message): Logs a message at the error level.

Example:

1
2
3
4
5
cf.log.trace("This is a trace message");
cf.log.debug("This is a debug message");
cf.log.info("This is an info message");
cf.log.warn("This is a warn message");
cf.log.error("This is an error message");

You can also create a logger for your own module like this:

const myLog = cf.getLogger("my-module")

Then you can change the log level for this module using the same methods as before.

Lazy evaluation

If you want to log a message that is expensive to compute, you can use the cf.lazyLog(level, callback, log) method. This method takes three parameters:

  • level: The log level of the message
  • callback: A function that returns the message to log
  • log: (Optional) if you are using a custom logger, you can pass it here

Example:

1
2
3
4
5
6
7
cf.lazyLog("info", () => {
    let expensiveMessage;

    // some expensive computation

    return expensiveMessage;
});