Page tree

 

Support to control logging of user scripts via global configuration.

Type
Since
2.2.0

When userscripts fail to perform correctly, a more verbose logging would be helpful to localize the issue with the code. The more verbose the logging output is, the more likely to localize the issue more quickly. But a larger number of scripts that log verbose, make it also difficult to read a log file.

This feature allows to switch verbose logging of userscripts on and off at runtime without deploying the scripts.

Details

The following sections show how to control the verbose logging to the client log in the browser.

Configuration

With Userscripts for Confluence you can configure whether verbose logging is on or off and which modules are requested to do verbose logging.

Note that the scripts need to query the configuration to determine the requested logging for this module.

The configuration is found via the Administration Cog within the General Configuration of Confluence by the section Userscripts.

Please check the checkbox Verbose Client Logging to activate verbose logging.

If no module name is specified with Client Logging Modules, then all modules will do verbose logging. In the example above the module hello-world is requested to do verbose logging. All other modules will still only log warnings and errors.

The module is printing its name as a prefix to its log message. Per convention this would be in brackets like this: [hello-world] Hello Userscripts!

Userscript

The userscripts needs to determine whether verbose logging has been requested by the administrators.

The function isVerboseLoggingRequestedFor allows to request the status of of verbose logging for a given module. The returned value is a boolean of true if verbose logging is requested and false if verbose logging is off.

Hello World Script using Verbose Logging
"use strict";

AJS.toInit(function () {
  const MODULE_NAME = 'hello-world';
  const logToConsole = USERSCRIPT4C.isVerboseLoggingRequestedFor(MODULE_NAME);

  if (logToConsole) {
    AJS.log('[' + MODULE_NAME + '] Hello Userscripts!');
  }
});