-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(byods-sdk):added logger package to base-client,token-manager,byd…
…os and data-source-client
- Loading branch information
Showing
19 changed files
with
877 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* eslint-disable valid-jsdoc */ | ||
//import {RegistrationStatus} from '../../common/types'; | ||
|
||
import {ErrorContext, ErrorMessage, ErrorObject, ERROR_TYPE} from '../types'; | ||
import ExtendedError from './ExtendedError'; | ||
|
||
/** | ||
* Any error reported from Calling client should be stored here. | ||
*/ | ||
export class BYODSError extends ExtendedError { | ||
// public status: RegistrationStatus = RegistrationStatus.INACTIVE; | ||
|
||
/** | ||
* Instantiate the Error class with these parameters. | ||
* | ||
* @param {ErrorMessage} msg - Custom error message. | ||
* @param {ErrorContext} context - The context in which the error occurred. | ||
* @param {ERROR_TYPE} type - The type of the error. | ||
* @param {RegistrationStatus} status - Mobius status, should be default. | ||
*/ | ||
constructor( | ||
msg: ErrorMessage, | ||
// context: ErrorContext, | ||
type: ERROR_TYPE, | ||
// status: RegistrationStatus | ||
) { | ||
super(msg,type); | ||
// this.context = context; | ||
// this.status = status; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Class method exposed to callers to allow storing of error object. | ||
* | ||
* @param error - Error Object. | ||
*/ | ||
public setError(error: ErrorObject) { | ||
this.message = error.message; | ||
// this.context = error.context; | ||
this.type = error.type; | ||
} | ||
|
||
/** | ||
* Class method exposed to callers to retrieve error object. | ||
* | ||
* @returns Error. | ||
*/ | ||
public getError(): ErrorObject { | ||
return <ErrorObject>{ message: this.message, type: this.type }; | ||
} | ||
} | ||
|
||
/** | ||
* Instantiate CallingClientError. | ||
* | ||
* @param msg - Custom error message. | ||
* @param context - Error context. | ||
* @param type - Error Type. | ||
* @param status - Mobius Status, should be default. | ||
* @returns CallingClientError instance. | ||
*/ | ||
export const createClientError = ( | ||
msg: ErrorMessage, | ||
context: ErrorContext, | ||
type: ERROR_TYPE, | ||
// status: RegistrationStatus | ||
// ) => new CallingClientError(msg, context, type, status); | ||
) => new BYODSError(msg,type); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable valid-jsdoc */ | ||
import {ErrorContext, ErrorMessage, ERROR_TYPE} from '../types'; | ||
|
||
/** | ||
* | ||
*/ | ||
export default class ExtendedError extends Error { | ||
public type: ERROR_TYPE; | ||
|
||
// public context: ErrorContext; | ||
|
||
/** | ||
* @param msg - TODO. | ||
* @param context - TODO. | ||
* @param type - TODO. | ||
*/ | ||
constructor(msg: ErrorMessage,type: ERROR_TYPE) { | ||
super(msg); | ||
this.type = type || ERROR_TYPE.DEFAULT; | ||
// this.context = context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {BYODSError as BYODSError} from './catalog/BYODSDeviceError'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {IMetaContext} from '../Logger/types'; | ||
|
||
export type ErrorMessage = string; | ||
|
||
export enum ERROR_LAYER { | ||
CALL_CONTROL = 'call_control', | ||
MEDIA = 'media', | ||
} | ||
|
||
export enum ERROR_TYPE { | ||
CALL_ERROR = 'call_error', | ||
DEFAULT = 'default_error', | ||
FORBIDDEN_ERROR = 'forbidden', | ||
NOT_FOUND = 'not_found', | ||
REGISTRATION_ERROR = 'registration_error', | ||
SERVICE_UNAVAILABLE = 'service_unavailable', | ||
TIMEOUT = 'timeout', | ||
TOKEN_ERROR = 'token_error', | ||
SERVER_ERROR = 'server_error', | ||
} | ||
|
||
export enum ERROR_CODE { | ||
UNAUTHORIZED = 401, | ||
FORBIDDEN = 403, | ||
DEVICE_NOT_FOUND = 404, | ||
INTERNAL_SERVER_ERROR = 500, | ||
NOT_IMPLEMENTED = 501, | ||
SERVICE_UNAVAILABLE = 503, | ||
BAD_REQUEST = 400, | ||
REQUEST_TIMEOUT = 408, | ||
TOO_MANY_REQUESTS = 429, | ||
} | ||
|
||
export enum CALL_ERROR_CODE { | ||
INVALID_STATUS_UPDATE = 111, | ||
DEVICE_NOT_REGISTERED = 112, | ||
CALL_NOT_FOUND = 113, | ||
ERROR_PROCESSING = 114, | ||
USER_BUSY = 115, | ||
PARSING_ERROR = 116, | ||
TIMEOUT_ERROR = 117, | ||
NOT_ACCEPTABLE = 118, | ||
CALL_REJECTED = 119, | ||
NOT_AVAILABLE = 120, | ||
} | ||
|
||
export enum DEVICE_ERROR_CODE { | ||
DEVICE_LIMIT_EXCEEDED = 101, | ||
DEVICE_CREATION_DISABLED = 102, | ||
DEVICE_CREATION_FAILED = 103, | ||
} | ||
|
||
export interface ErrorContext extends IMetaContext {} | ||
|
||
export type ErrorObject = { | ||
message: ErrorMessage; | ||
type: ERROR_TYPE; | ||
context: ErrorContext; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* eslint-disable valid-jsdoc */ | ||
import {BYODS_PACKAGE_NAME} from '../constants'; | ||
import {IMetaContext} from './types'; | ||
import ExtendedError from '../Errors/catalog/ExtendedError'; | ||
import {LOGGING_LEVEL, LogContext, LOGGER, LOG_PREFIX} from './types'; | ||
|
||
/* | ||
* These are the order of log levels :- | ||
* error - 1 | ||
* warn - 2 | ||
* log - 3 | ||
* info - 4 | ||
* trace - 5 | ||
* | ||
* Where log level n denotes that level 1 -> level n will be logged. | ||
*/ | ||
|
||
let currentLogLevel = LOGGING_LEVEL.error; | ||
|
||
/** | ||
* A wrapper around console which prints to stderr or stdout | ||
* based on the level defined. | ||
* | ||
* @param message - Log Message to print. | ||
* @param level - Log level. | ||
*/ | ||
const writeToConsole = (message: string, level: LOGGER) => { | ||
switch (level) { | ||
case LOGGER.INFO: | ||
case LOGGER.LOG: { | ||
// eslint-disable-next-line no-console | ||
console.log(message); | ||
break; | ||
} | ||
case LOGGER.WARN: { | ||
console.warn(message); | ||
break; | ||
} | ||
case LOGGER.ERROR: { | ||
console.error(message); | ||
break; | ||
} | ||
case LOGGER.TRACE: { | ||
// eslint-disable-next-line no-console | ||
console.trace(message); | ||
break; | ||
} | ||
default: { | ||
// Since this is internal , we shouldn't reach here | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Format the Log message as 'timestamp Calling SDK - [level]: file:example.ts - method:methodName - Actual log message'. | ||
* | ||
* @param context - File and method. | ||
* @param level - Log level. | ||
* @returns - Formatted string. | ||
*/ | ||
const format = (context: IMetaContext, level: string): string => { | ||
const timestamp = new Date().toUTCString(); | ||
|
||
return `${BYODS_PACKAGE_NAME}: ${timestamp}: ${level}: ${LOG_PREFIX.FILE}:${context.file} - ${LOG_PREFIX.METHOD}:${context.method}`; | ||
}; | ||
|
||
/** | ||
* Used by the Calling Client to initialize the logger module | ||
* with a certain level. | ||
* | ||
* @param level - Log Level. | ||
*/ | ||
const setLogger = (level: string, module: string) => { | ||
switch (level) { | ||
case LOGGER.WARN: { | ||
currentLogLevel = LOGGING_LEVEL.warn; | ||
break; | ||
} | ||
case LOGGER.LOG: { | ||
currentLogLevel = LOGGING_LEVEL.log; | ||
break; | ||
} | ||
case LOGGER.INFO: { | ||
currentLogLevel = LOGGING_LEVEL.info; | ||
break; | ||
} | ||
case LOGGER.TRACE: { | ||
currentLogLevel = LOGGING_LEVEL.trace; | ||
break; | ||
} | ||
default: { | ||
currentLogLevel = LOGGING_LEVEL.error; | ||
} | ||
} | ||
|
||
const message = `Logger initialized for module: ${module} with level: ${currentLogLevel}`; | ||
|
||
writeToConsole( | ||
`${format({file: 'logger.ts', method: 'setLogger'}, '')} - ${LOG_PREFIX.MESSAGE}:${message}`, | ||
LOGGER.INFO | ||
); | ||
}; | ||
|
||
/** | ||
* To retrieve the current log level. | ||
* | ||
* @returns - Log level. | ||
*/ | ||
const getLogLevel = (): LOGGER => { | ||
let level; | ||
|
||
switch (currentLogLevel) { | ||
case LOGGING_LEVEL.warn: { | ||
level = LOGGER.WARN; | ||
break; | ||
} | ||
case LOGGING_LEVEL.log: { | ||
level = LOGGER.LOG; | ||
break; | ||
} | ||
case LOGGING_LEVEL.info: { | ||
level = LOGGER.INFO; | ||
break; | ||
} | ||
case LOGGING_LEVEL.trace: { | ||
level = LOGGER.TRACE; | ||
break; | ||
} | ||
default: { | ||
level = LOGGER.ERROR; | ||
} | ||
} | ||
|
||
return level; | ||
}; | ||
|
||
/** | ||
* Can be used to print only useful information. | ||
* | ||
* @param message - Caller emitted string. | ||
* @param context - File and method which called. | ||
*/ | ||
const logMessage = (message: string, context: LogContext) => { | ||
if (currentLogLevel >= LOGGING_LEVEL.log) { | ||
writeToConsole(`${format(context, '[LOG]')} - ${LOG_PREFIX.MESSAGE}:${message}`, LOGGER.LOG); | ||
} | ||
}; | ||
|
||
/** | ||
* Can be used to print informational messages. | ||
* | ||
* @param message - Caller emitted string. | ||
* @param context - File and method which called. | ||
*/ | ||
const logInfo = (message: string, context: LogContext) => { | ||
if (currentLogLevel >= LOGGING_LEVEL.info) { | ||
writeToConsole(`${format(context, '[INFO]')} - ${LOG_PREFIX.MESSAGE}:${message}`, LOGGER.INFO); | ||
} | ||
}; | ||
|
||
/** | ||
* Can be used to print warning messages. | ||
* | ||
* @param message - Caller emitted string. | ||
* @param context - File and method which called. | ||
*/ | ||
const logWarn = (message: string, context: LogContext) => { | ||
if (currentLogLevel >= LOGGING_LEVEL.warn) { | ||
writeToConsole(`${format(context, '[WARN]')} - ${LOG_PREFIX.MESSAGE}:${message}`, LOGGER.WARN); | ||
} | ||
}; | ||
|
||
/** | ||
* Can be used to print the stack trace of the entire call path. | ||
* | ||
* @param message - Caller emitted string. | ||
* @param context - File and method which called. | ||
*/ | ||
const logTrace = (message: string, context: LogContext) => { | ||
if (currentLogLevel >= LOGGING_LEVEL.trace) { | ||
writeToConsole( | ||
`${format(context, '[TRACE]')} - ${LOG_PREFIX.MESSAGE}:${message}`, | ||
LOGGER.TRACE | ||
); | ||
} | ||
}; | ||
|
||
/** | ||
* Can be used to print only errors. | ||
* | ||
* @param error - Error string . | ||
* @param context - File and method which called. | ||
*/ | ||
const logError = (error: ExtendedError, context: LogContext) => { | ||
if (currentLogLevel >= LOGGING_LEVEL.error) { | ||
writeToConsole( | ||
`${format(context, '[ERROR]')} - !${LOG_PREFIX.ERROR}!${LOG_PREFIX.MESSAGE}:${error.message}`, | ||
LOGGER.ERROR | ||
); | ||
} | ||
}; | ||
|
||
export default { | ||
log: logMessage, | ||
error: logError, | ||
info: logInfo, | ||
warn: logWarn, | ||
trace: logTrace, | ||
setLogger, | ||
getLogLevel, | ||
}; |
Oops, something went wrong.