Logging

Overview

io.Connect Browser offers a Logger API which enables web apps to create a hierarchy of sub-loggers mapped to app components where you can control the level of logging for each component.

Adding logging to your web apps can be helpful in a variety of ways. Having a well-designed and meaningful logging structure in your apps and their components can save a lot of time when debugging an app during development or troubleshooting problems with an app in production.

The Logger API is accessible via the io.logger object.

Sub-Loggers

Logger instances have a subLogger() method that creates a new sub-logger of the current logger. The name of each logger instance is a dot-delimited string containing all names of the loggers constituting a hierarchy line from the base logger (the app name) down to the current logger. This allows an effective and intuitive logging structure which can be easily adjusted to the component hierarchy in your app. For instance, a structure like app-name.main-component.sub-component gives you a clear idea from where the respective log entry originates and helps you find the necessary information much faster.

To use a logger in your interop-enabled apps, create a logger instance with the subLogger() method and assign the logger a name:

const logger = io.logger.subLogger("main-component");

Log Levels

To set the logging level at which to publish log entries, use the publishLevel() method of the logger instance:

logger.publishLevel("info");

Everything at and above the specified logging level will be logged, all else will be skipped. The available logging levels are "trace", "debug", "info", "warn" and "error".

Log Messages

To log messages, either use the log() method of the logger instance, or the respective logging level methods - error(), trace(), and more:

// The `log()` method accepts the message and the logging level as arguments.
logger.log("Couldn't load component!", "error");

// Each logging level method accepts only a message as an argument.
logger.error("Couldn't load component!");

API Reference

For a complete list of the available Logger API methods and properties, see the Logger API reference documentation.