Manages all log messages from SDK modules, you can use this logger to add your custom messages and set your custom log handlers to forward all messages to your own monitoring system.

By default all loggers are set in level OFF (Logger.OFF), and there are available the following log levels.

This module is based on js-logger you can refer to its documentation or following our examples.

Examples
// Log a message
Logger.info('This is an info log', 445566)
// [Global] 2021-04-05T15:58:44.893Z - This is an info log 445566
// Create a named logger
const myLogger = Logger.get('CustomLogger')
myLogger.setLevel(Logger.WARN)
myLogger.warn('This is a warning log')
// [CustomLogger] 2021-04-05T15:59:53.377Z - This is a warning log
// Profiling
// Start timing something
Logger.time('Timer name')

// ... some time passes ...

// Stop timing something.
Logger.timeEnd('Timer name')
// Timer name: 35282.997802734375 ms

Members

(inner, constant) DEBUG :LogLevel

Logger.DEBUG

(inner, constant) ERROR :LogLevel

Logger.ERROR

(inner, constant) INFO :LogLevel

Logger.INFO

(inner, constant) OFF :LogLevel

Logger.OFF

(inner, constant) TIME :LogLevel

Logger.TIME

(inner, constant) TRACE :LogLevel

Logger.TRACE

(inner) VERSION

Returns the current SDK version.

(inner, constant) WARN :LogLevel

Logger.WARN

Methods

(inner) diagnose(config) → {Object}

Returns diagnostics information about the connection and environment, formatted according to the specified parameters.

Parameters:
NameTypeDescription
configObject | Number

Configuration object for the diagnostic parameters

Properties
NameTypeAttributesDefaultDescription
statsCountNumber<optional>
60

Number of stats objects to be included in the diagnostics report.

historySizeNumber<optional>
1000

Amount of history messages to be returned.

minLogLevelString<optional>

Levels of history messages to be included. examples of minLogLevel values in level order: 1 - TRACE 2 - DEBUG 3 - INFO 4 - WARN 5 - ERROR If 'INFO' (3) given, return INFO (3), WARN (4), and ERROR (5) level messages.

statsFormatString<optional>
'JSON'

Format of the stats objects in the diagnostics report. Use Logger.JSON or Logger.CMCD.

Returns:

An object containing relevant diagnostics information such as userAgent, SDK version, and stats data.

Type: 
Object
Example
// Example using default parameters
const diagnosticsDefault = Logger.diagnose();

// Example specifying statsCount and format
const diagnostics = Logger.diagnose({ statsCount: 30, minLogLevel: 'INFO', format: Logger.CMCD });

// Output: Diagnostics object with specified configuration

(inner) get(name) → {Object}

Gets or creates a named logger. Named loggers are used to group log messages that refers to a common context.

Parameters:
NameTypeDescription
nameString
Returns:

Logger object with same properties and functions as Logger except history and handlers related functions.

Type: 
Object
Example
const myLogger = Logger.get('MyLogger')
// Set logger level
myLogger.setLevel(Logger.DEBUG)

myLogger.debug('This is a debug log')
myLogger.info('This is a info log')
myLogger.warn('This is a warning log')

// Get logger level
myLogger.getLevel()
// {value: 3, name: 'INFO'}

(inner) getHistory() → {Array.<String>}

Get all logs generated during a session. All logs are recollected besides the log level selected by the user.

Returns:

All logs recollected from level TRACE.

Type: 
Array.<String>
Example
Logger.getHistory()
// Outupt
// [
//   "[Director] 2021-04-05T14:09:26.625Z - Getting publisher connection data for stream name:  1xxx2",
//   "[Director] 2021-04-05T14:09:27.064Z - Getting publisher response",
//   "[Publish]  2021-04-05T14:09:27.066Z - Broadcasting"
// ]

(inner) getHistoryMaxSize()

Get the maximum count of logs preserved during a session.

Example
Logger.getHistoryMaxSize()

(inner) getLevel() → {LogLevel}

Get global current logger level. Also you can get the level of any particular logger.

Returns:
Type: 
LogLevel
Example
// Global Level
Logger.getLevel()
// Output
// {value: 2, name: 'DEBUG'}

// Module Level
Logger.get('Publish').getLevel()
// Output
// {value: 5, name: 'WARN'}

(inner) setHandler(handler, level)

Add your custom log handler to Logger at the specified level.

Parameters:
NameTypeDescription
handlerloggerHandler

Your custom log handler function.

levelLogLevel

Log level to filter messages.

Example
const myHandler = (messages, context) => {
 // You can filter by logger
 if (context.name === 'Publish') {
   sendToMyLogger(messages[0])
 }

 // You can filter by logger level
 if (context.level.value >= Logger.INFO.value) {
   sendToMyLogger(messages[0])
 }
}

Logger.setHandler(myHandler, Logger.INFO)

(inner) setHistoryMaxSize(maxSize)

Set the maximum count of logs to preserve during a session. By default it is set to 10000.

Parameters:
NameTypeDescription
maxSizeNumber

Max size of log history. Set 0 to disable history or -1 to unlimited log history.

Example
Logger.setHistoryMaxSize(100)

(inner) setLevel(level)

Set log level to all loggers.

Parameters:
NameTypeDescription
levelLogLevel

New log level to be set.

Example
// Global Level
Logger.setLevel(Logger.DEBUG)

// Module Level
Logger.get('Publish').setLevel(Logger.DEBUG)