Logger

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

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

This module is based on js-logger. For more information, see the js-logger documentation and our examples.

Source:
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

Type:
Source:

# (inner, constant) ERROR :LogLevel

Logger.ERROR

Type:
Source:

# (inner, constant) INFO :LogLevel

Logger.INFO

Type:
Source:

# (inner, constant) OFF :LogLevel

Logger.OFF

Type:
Source:

# (inner, constant) TIME :LogLevel

Logger.TIME

Type:
Source:

# (inner, constant) TRACE :LogLevel

Logger.TRACE

Type:
Source:

# (inner, constant) WARN :LogLevel

Logger.WARN

Type:
Source:

Methods

# (inner) get(name) → {Object}

Get or create a named logger. Named loggers are used to group log messages that refer to a common context.

Parameters:
Name Type Description
name String
Source:
Returns:

A logger object with the same properties and functions as Logger except for 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.

Source:
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.

Source:
Example
Logger.getHistoryMaxSize()

# (inner) getLevel() → {LogLevel}

Get the current global log level or the level of any particular logger.

Source:
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:
Name Type Description
handler loggerHandler

Your custom log handler function.

level LogLevel

The log level to filter messages.

Source:
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. The default value is 10000.

Parameters:
Name Type Description
maxSize Number

The maximum size of the log history. Set to 0 to disable the log history or -1 for an unlimited log history.

Source:
Example
Logger.setHistoryMaxSize(100)

# (inner) setLevel(level)

Set the log level for all loggers.

Parameters:
Name Type Description
level LogLevel

The new log level.

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

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