Skip to content

Logging

Goel Biju edited this page Feb 8, 2021 · 5 revisions

Logging in the SciGateway frontend is done using loglevel. This allows for creating log messages with differing severity using the browser console.

To use it, import it in your file

import * as log from 'loglevel';

Then, you can use these methods to create log messages with the corresponding severity

log.error('error');
log.warn('warn');
log.info('info');
log.debug('debug');
log.trace('trace');

You can set the default log level globally by using:

log.setDefaultLevel(log.levels.DESIRED_LEVEL)

Where DESIRED_LEVEL can be any of the following values, from most to least verbose:

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • SILENT

This will disable all messages below the level specified. This is useful to set different log levels for production and development.

The log level can also be set using the browser's LocalStorage - for example, this would be useful to use to debug the production site. You can set this variable by running the command below in the browser console:

localStorage.setItem("loglevel", "DESIRED_LEVEL");

setDefaultLevel will not change the log level if you have this variable set, if you instead use setLevel this will override and overwrite your LocalStorage value. If you want to temporarily override your LocalStorage value but not overwrite, use setLevel(log.levels.DESIRED_LEVEL, false) which tells loglevel to not persist the new value.

Note that this only logs messages to the browser console and so is only visible to the client and is inherently not persisted. If you want to record log events permanently, then another mechanism of sending off the log messages to a server will be required.

Clone this wiki locally