@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
54 lines (49 loc) • 1.24 kB
text/typescript
import { LogLevels, isLogLevelString } from './index';
import SplitIO from '../../types/splitio';
import { ILogger } from './types';
import { ERROR_LOGLEVEL_INVALID } from './constants';
/**
* The public Logger utility API exposed via SplitFactory, used to update the log level.
*
* @param log - the factory logger instance to handle
*/
export function createLoggerAPI(log: ILogger): SplitIO.ILoggerAPI {
function setLogLevel(logLevel: string) {
if (isLogLevelString(logLevel)) {
log.setLogLevel(logLevel);
} else {
log.error(ERROR_LOGLEVEL_INVALID);
}
}
return {
/**
* Enables all the logs.
*/
enable() {
setLogLevel(LogLevels.DEBUG);
},
/**
* Sets a custom log Level for the SDK.
* @param logLevel - Custom LogLevel value.
*/
setLogLevel,
/**
* Sets a custom logger for the SDK logs.
* @param logger - Custom logger.
*/
setLogger(logger?: ILogger) {
log.setLogger(logger);
},
/**
* Disables all the log levels.
*/
disable() {
// Disabling is equal logLevel none
setLogLevel(LogLevels.NONE);
},
/**
* Exposed for usage with setLogLevel
*/
LogLevel: LogLevels
};
}