Logger
Source: Logger.js:38
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.
Example
// 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 msInstance Methods
getHistory(): Array.<String>
Get all logs generated during a session. All logs are recollected besides the log level selected by the user.
Returns
Array.<String>— All logs recollected from level TRACE.
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"
// ]getHistoryMaxSize()
Get the maximum count of logs preserved during a session.
Example
Logger.getHistoryMaxSize()setHistoryMaxSize(maxSize: Number)
Set the maximum count of logs to preserve during a session. By default it is set to 10000.
Parameters
maxSize(Number) — Max size of log history. Set 0 to disable history or -1 to unlimited log history.
Example
Logger.setHistoryMaxSize(100)setLevel(level: LogLevel)
Set log level to all loggers.
Parameters
level(LogLevel) — New log level to be set.
Example
// Global Level
Logger.setLevel(Logger.DEBUG)
// Module Level
Logger.get('Publish').setLevel(Logger.DEBUG)getLevel(): LogLevel
Get global current logger level. Also you can get the level of any particular logger.
Returns
Example
// Global Level
Logger.getLevel()
// Output
// {value: 2, name: 'DEBUG'}
// Module Level
Logger.get('Publish').getLevel()
// Output
// {value: 5, name: 'WARN'}get(name: String): Object
Gets or creates a named logger. Named loggers are used to group log messages that refers to a common context.
Parameters
name(String)
Returns
Object— Logger object with same properties and functions as Logger except history and handlers related functions.
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'}setHandler(handler: loggerHandler, level: LogLevel)
Add your custom log handler to Logger at the specified level.
Parameters
handler(loggerHandler) — Your custom log handler function.level(LogLevel) — 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)diagnose(config: Object | Number): Object
Returns diagnostics information about the connection and environment, formatted according to the specified parameters.
Parameters
config(Object | Number) — Configuration object for the diagnostic parametersconfig.statsCount(Number, optional, default: 60) — Number of stats objects to be included in the diagnostics report.config.historySize(Number, optional, default: 1000) — Amount of history messages to be returned.config.minLogLevel(String, 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.config.statsFormat(String, optional, default: "'JSON'") — Format of the stats objects in the diagnostics report. Use Logger.JSON or Logger.CMCD.
Returns
Object— An object containing relevant diagnostics information such as userAgent, SDK version, and stats data.
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 configurationInstance Fields
VERSION
Returns the current SDK version.
Other
TRACE: LogLevel
Logger.TRACE
DEBUG: LogLevel
Logger.DEBUG
INFO: LogLevel
Logger.INFO
TIME: LogLevel
Logger.TIME
WARN: LogLevel
Logger.WARN
ERROR: LogLevel
Logger.ERROR
OFF: LogLevel
Logger.OFF