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.
// 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.
Name | Type | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config | Object | | Configuration object for the diagnostic parameters Properties
|
An object containing relevant diagnostics information such as userAgent, SDK version, and stats data.
- Type:
- Object
// 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.
Name | Type | Description |
---|---|---|
name | String |
Logger object with same properties and functions as Logger except history and handlers related functions.
- Type:
- Object
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.
All logs recollected from level TRACE.
- Type:
- Array.<String>
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.
Logger.getHistoryMaxSize()
(inner) getLevel() → {LogLevel}
Get global current logger level. Also you can get the level of any particular logger.
- Type:
- LogLevel
// 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.
Name | Type | Description |
---|---|---|
handler | loggerHandler | Your custom log handler function. |
level | LogLevel | Log level to filter messages. |
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.
Name | Type | Description |
---|---|---|
maxSize | Number | Max size of log history. Set 0 to disable history or -1 to unlimited log history. |
Logger.setHistoryMaxSize(100)
(inner) setLevel(level)
Set log level to all loggers.
Name | Type | Description |
---|---|---|
level | LogLevel | New log level to be set. |
// Global Level
Logger.setLevel(Logger.DEBUG)
// Module Level
Logger.get('Publish').setLevel(Logger.DEBUG)