@huddly/sdk-interfaces
Version:
Module for describing the Huddly SDK using interfaces
99 lines • 3.76 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
/**
* Huddly Logger class.
*
* Use HUDDLY_LOG_LEVEL env variable to configure the log level on the SDK. The following values apply
* - DEBUG - log all gRPC messages
* - INFO - log INFO and ERROR message
* - ERROR - log only errors (default)
* - NONE - won't log any
*
* Use HUDDLY_LOG_CHANNEL env variable to configure where the logs will be directed to. The following
* values apply:
* - CONSOLE - Write logs to system console
* - FILE - Write logs to a dedicated file provided by HUDDLY_LOG_FILE
*
* USE HUDDLY_LOG_FILE env variable to specify the file where the logs will be directed to.
*
* @example
* // Print all logs and redirect them to a file
* export HUDDLY_LOG_LEVEL=DEBUG && HUDDLY_LOG_CHANNEL=FILE && HUDDLY_LOG_FILE=/users/johndoe/huddlysdk.log
* node sdkexample.js
*
* // Print INFO && ERROR logs and redirect them to console out
* export HUDDLY_LOG_LEVEL=INFO
* node sdkexample.js
*
* // Disable all log output
* export HUDDLY_LOG_LEVEL=NONE
* node sdkexample.js
*/
class Logger {
static setLogger(logger) {
Logger.customLogger = logger;
}
static warn(message, component = 'Generic Component') {
if (['DEBUG'].indexOf(process.env.HUDDLY_LOG_LEVEL) > -1) {
if (Logger.customLogger) {
Logger.customLogger.warn(message, component);
return;
}
this.redirectLogMsg(this.formatLogMsg('WARN', component, message));
}
}
static info(message, component = 'Generic Component') {
if (['INFO', 'DEBUG'].indexOf(process.env.HUDDLY_LOG_LEVEL) > -1) {
if (Logger.customLogger) {
Logger.customLogger.info(message, component);
return;
}
this.redirectLogMsg(this.formatLogMsg('INFO', component, message));
}
}
static debug(message, component = 'Generic Component') {
if (['DEBUG'].indexOf(process.env.HUDDLY_LOG_LEVEL) > -1) {
if (Logger.customLogger) {
Logger.customLogger.debug(message, component);
return;
}
this.redirectLogMsg(this.formatLogMsg('DEBUG', component, message));
}
}
static error(message, stackTrace, component = 'Generic Component') {
if (['NONE'].indexOf(process.env.HUDDLY_LOG_LEVEL) == -1) {
if (Logger.customLogger) {
Logger.customLogger.error(message, stackTrace, component);
return;
}
this.redirectLogMsg(this.formatLogMsg('ERROR', component, message));
this.redirectLogMsg(stackTrace);
}
}
static redirectLogMsg(message) {
if (process.env.HUDDLY_LOG_CHANNEL === 'FILE') {
const logFile = process.env.HUDDLY_LOG_FILE || 'huddlysdk.log';
fs_1.default.appendFileSync(logFile, `${message}\n`);
}
else {
console.log(message);
}
}
static formatDate(date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
static getTime(date) {
return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}`;
}
static formatLogMsg(logLevel, component, message) {
const now = new Date();
return `${this.formatDate(now)} | ${this.getTime(now)} | ${logLevel} | ${component} | ${message}`;
}
}
exports.default = Logger;
Logger.customLogger = undefined;
//# sourceMappingURL=Logger.js.map