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
  • providers (for data providers)

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
  • providers (for data providers)

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;
});

ChartFactor Node logs

ChartFactor data provider modules such as BigQuery, Databricks, Elasticsearch, and Redshift/Postgres can be deployed in a Node environment. The log levels and output produced by these modules can be managed as described below.

log levels

You can set the log levels using the environment variables CF_LOG_LEVEL_PROVIDERS and CF_LOG_LEVEL_AKTIVE.

  • CF_LOG_LEVEL_PROVIDERS: This variable sets the log levels for data providers. Supported values are the same as the ones used in the browser environment: trace, debug, info, warn, and error.
  • CF_LOG_LEVEL_AKTIVE: This variable sets the log levels for the aktive module. Supported values are also trace, debug, info, warn, and error.

If both variables are set, their respective log levels will apply. Otherwise, the warn default level will be used.

log outputs

In a Node environment, the ChartFactor Toolkit logs are sent to the console by default.

You can set the log output to a specific file by providing a method factory as illustrated below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const cf = global.cf;

const originalFactory = cf.log.methodFactory;

cf.log.methodFactory = function (methodName, logLevel, loggerName) {
    const rawMethod = originalFactory(methodName, logLevel, loggerName);

    return (...args) => {
        // Define the log file path
        const logFilePath = process.env.CF_LOG_PATH || path.join(__dirname, '..', 'log', 'chartfactor.log');

        if (!fs.existsSync(logFilePath)) {
            fs.mkdirSync(path.dirname(logFilePath), { recursive: true });
            fs.writeFileSync(logFilePath, '');
        }

        // Append the log entry to the file
        const logMessage = `${new Date().toISOString()} ${methodName.toUpperCase()}: ${args.join(' ')}\n`;
        fs.appendFileSync(logFilePath, logMessage);

        // Write to the original `loglevel` output
        rawMethod(...args);
    };
};

// Apply the new method factory
cf.log.setLevel(cf.log.getLevel());

In the example above, the CF_LOG_PATH environment variable will be used to determine the path to the chartfactor log file. Please refer to the ChartFactor Node project which you can use as a reference implementation on how to write data provider logs to a file.