The JavaScript Logger allows you to capture logs generated by the Twilio Video JavaScript SDK in real time so that you can monitor your frontend applications and see how they behave in production. You can interact with the JavaScript Logger to intercept logs generated by the JavaScript SDK, modify them, and send the logs to a remote server.
The JavaScript logger is included with the Twilio Video JavaScript SDK in versions 2.10.0 and above.
You can include the JavaScript SDK in your application either by installing it with Node Package Manager (npm) or using the Twilio CDN.
See the list of supported browsers here.
Install the Video JavaScript SDK using npm:
npm install --save twilio-video
Then, you can start using the JavaScript Logger API in your application:
1const { Logger } = require('twilio-video');2const logger = Logger.getLogger('twilio-video');
You can also copy twilio-video.min.js
from the twilio-video/dist
folder after npm installing it and include it directly in your web app using a <script>
tag.
<script src="https://my-server-path/twilio-video.min.js"></script>
Using this method, you can access the JavaScript Logger API like so:
const logger = Twilio.Video.Logger.getLogger('twilio-video');
You can also include the JavaScript SDK in your application from Twilio's CDN:
<script src="https://sdk.twilio.com/js/video/releases/2.20.0/twilio-video.min.js"></script>
You should make sure you're using the latest Twilio Video JavaScript SDK release. To find the CDN link for the most recent JavaScript SDK release, visit the JavaScript SDK latest release documentation.
Using the CDN, the JavaScript SDK will set a browser global that you can use to reference the Logger API:
const logger = Twilio.Video.Logger.getLogger('twilio-video');
The Video JavaScript SDK uses the loglevel module, which is a lightweight logging library that works in all modern browsers and NodeJS environments. Using the JavaScript SDK's Logger API, you can access internal JavaScript SDK logs and perform actions as defined by the loglevel APIs. You can see output from logs in your browser's developer console.
You can set the level of logs that you see and filter out using logger.setLevel
:
1const logger = Twilio.Video.Logger.getLogger('twilio-video');2logger.setLevel('debug');
Possible logging levels are:
When you set a logging level, you will see all logs at that level and above. For example, if you set the logging level to warn
, you'll see logs at warn
and error
level. Setting the logging level to silent
filters out all logs.
The default level is warn
.
The loglevel module's plugin system allows you to add features specific to your application to the Video JavaScript SDK logger. You can intercept logs, modify them, and perform other actions based on the log message.
To interact with logs, you will need to overwrite the logger's original methodFactory
. With this approach, you can process each log before it is printed out to the console. The following example shows how to add the prefix [My Application]
to each log of debug
level and above from the Video JavaScript SDK. Note that you should update the logger's methodFactory
before you connect to a Twilio Video Room.
1// access the JS Logger API2const logger = Twilio.Video.Logger.getLogger('twilio-video');34// overwrite the loglevel module's original methodFactory5// and prefix each log with `[My Application]`6const originalFactory = logger.methodFactory;7logger.methodFactory = function (methodName, logLevel, loggerName) {8const method = originalFactory(methodName, logLevel, loggerName);9return function (datetime, logLevel, component, message, data) {10const prefix = '[My Application]';11method(prefix, datetime, logLevel, component, message, data);12};13};1415// set the logger to log debug messages and above16logger.setLevel('debug');1718// Then, connect to a Twilio Video Room using the `connect` method19// Twilio.Video.connect(token, {})...
Below is an example of what the log output would look like after connecting to a Video Room:
1[My Application] 2022-02-25T05:09:50.186Z info [connect #1] Connecting to a Room ...2[My Application] 2022-02-25T05:09:50.186Z debug [connect #1] Options: ...3[My Application] 2022-02-25T05:09:50.187Z info [connect #1] ...
The callback function returned from the methodFactory
contains parameters provided by the JavaScript SDK. You can use these parameters to gather more information about the log and act on it as desired.
Name | Description |
---|---|
datetime | The current date and time in simplified extended ISO format |
logLevel | The current logging level. Possible values are trace, debug, info, warn, error, and silent. |
component | The component where the log originated, using [name #count] format. name is the component name, either the method name or the object name where the log was generated. count is the instance count. For example, a component [RemoteVideoTrack #2: MT...] indicates that the log line originated from a RemoteVideoTrack and that there are two total instances of RemoteVideoTracks. The component includes the SID of the object if the log originates from an object. |
message | The message that is being logged |
data | An optional data object, which can be inspected for more information about the log. For example, the data might be a Room object after a Room is created, or an object containing signaling data. Any new type of data will be published in the JavaScript SDK changelog. |
The example below demonstrates how you might inspect the component
parameter to send log lines to a different remote server based on what the component includes.
1// access the JS Logger API2const logger = Twilio.Video.Logger.getLogger('twilio-video');3logger.setLevel("debug");45// overwrite the loglevel module's original methodFactory6const originalFactory = logger.methodFactory;7logger.methodFactory = function (methodName, logLevel, loggerName) {8const method = originalFactory(methodName, logLevel, loggerName);9return function (datetime, logLevel, component, message, data) {10if (component.includes('connect')) {11// assume you have a function called sendToServerFoo, which12// can send a line of text to a remote server13sendToServerFoo(datetime, logLevel, component, message, data);14}15method(datetime, logLevel, component, message, data);16};17};
The following example demonstrates inspecting the data
parameter, looking for logs with signaling data, and creating new logs based on the information returned.
1// access the JS Logger API2const logger = Twilio.Video.Logger.getLogger('twilio-video');3logger.setLevel("debug");45// overwrite the loglevel module's original methodFactory6const originalFactory = logger.methodFactory;7logger.methodFactory = function (methodName, level, loggerName) {8const method = originalFactory(methodName, level, loggerName);9return function (datetime, logLevel, component, message, data) {10method(datetime, logLevel, component, message, data);11if (message === 'event' && data.group === 'signaling') {12if (data.name === 'waiting') {13console.warn('Twilio\'s signaling server is busy.');14} else if (data.name === 'connecting') {15console.log('Connecting to Twilio\'s signaling server.');16} else if (data.name === 'open') {17console.log('Connected to Twilio\'s signaling server.');18} else if (data.name === 'closed') {19if (data.level === 'error') {20const { payload: { reason } } = data;21console.error('Connection to Twilio\'s signaling server abruptly closed:', data.reason);22} else {23console.log('Connection to Twilio\'s signaling server closed.');24}25}26}27};28};
One common use case in client side logging is forwarding logs to a remote server to monitor your frontend applications. Using loglevel's plugin system, you can intercept logs from the Video JavaScript SDK and forward them to your server. The loglevel-plugin-remote module is a pre-written plugin that sends logs to a server with minimal configuration.
NPM
Install loglevel-plugin-remote using npm:
npm i loglevel-plugin-remote --save
Then, you can use the plugin in your application:
const remote = require('loglevel-plugin-remote');
Script tag
You can also copy loglevel-plugin-remote.min.js
from the loglevel-plugin-remote/dist
folder after npm installing it and include it directly in your web app using a <script>
tag.
<script src="https://my-server-path/loglevel-plugin-remote.min.js"></script>
Using this method, you can access the plugin directly by calling methods on the remote
object.
remote.apply(Twilio.Video.Logger);
The code below uses the default settings for loglevel-plugin-remote to send all logs for the level debug
and above to the server that is serving your application at the /logger
route.
1remote.apply(Twilio.Video.Logger);2const logger = Logger.getLogger('twilio-video');3logger.setLevel('debug');
You can configure how and where the plugin sends logs. See the plugin's documentation for more information and full configuration options.