From 2b16be3aef9c15ed55480a0d000c2171a455e55c Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Fri, 25 Jun 2021 09:14:29 +0100 Subject: [PATCH] chore(*): refactor to use logger if available --- src/SASViyaApiClient.ts | 16 +++++++++++----- src/SessionManager.ts | 6 ++++-- src/request/RequestClient.ts | 3 ++- src/utils/fetchLogByChunks.ts | 4 +++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 8b04cf9..5bc4c71 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -607,6 +607,7 @@ export class SASViyaApiClient { accessToken?: string, isForced?: boolean ): Promise { + const logger = process.logger || console if (!parentFolderPath && !parentFolderUri) { throw new Error('Path or URI of the parent folder is required.') } @@ -614,7 +615,7 @@ export class SASViyaApiClient { if (!parentFolderUri && parentFolderPath) { parentFolderUri = await this.getFolderUri(parentFolderPath, accessToken) if (!parentFolderUri) { - console.log( + logger.info( `Parent folder at path '${parentFolderPath}' is not present.` ) @@ -626,7 +627,7 @@ export class SASViyaApiClient { if (newParentFolderPath === '') { throw new Error('Root folder has to be present on the server.') } - console.log( + logger.info( `Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'` ) const parentFolder = await this.createFolder( @@ -635,7 +636,7 @@ export class SASViyaApiClient { undefined, accessToken ) - console.log( + logger.info( `Parent folder '${newFolderName}' has been successfully created.` ) parentFolderUri = `/folders/folders/${parentFolder.id}` @@ -1138,6 +1139,8 @@ export class SASViyaApiClient { authConfig?: AuthConfig, pollOptions?: PollOptions ) { + const logger = process.logger || console + let POLL_INTERVAL = 300 let MAX_POLL_COUNT = 1000 let MAX_ERROR_COUNT = 5 @@ -1258,8 +1261,8 @@ export class SASViyaApiClient { } if (this.debug && printedState !== postedJobState) { - console.log('Polling job status...') - console.log(`Current job state: ${postedJobState}`) + logger.info('Polling job status...') + logger.info(`Current job state: ${postedJobState}`) printedState = postedJobState } @@ -1449,6 +1452,9 @@ export class SASViyaApiClient { accessToken ) + if (!sourceFolderUri) { + return undefined + } const sourceFolderId = sourceFolderUri?.split('/').pop() const { result: folder } = await this.requestClient diff --git a/src/SessionManager.ts b/src/SessionManager.ts index 2120471..93a411d 100644 --- a/src/SessionManager.ts +++ b/src/SessionManager.ts @@ -158,6 +158,8 @@ export class SessionManager { etag: string | null, accessToken?: string ) { + const logger = process.logger || console + let sessionState = session.state const stateLink = session.links.find((l: any) => l.rel === 'state') @@ -170,7 +172,7 @@ export class SessionManager { ) { if (stateLink) { if (this.debug && !this.printedSessionState.printed) { - console.log('Polling session status...') + logger.info('Polling session status...') this.printedSessionState.printed = true } @@ -186,7 +188,7 @@ export class SessionManager { sessionState = state.trim() if (this.debug && this.printedSessionState.state !== sessionState) { - console.log(`Current session state is '${sessionState}'`) + logger.info(`Current session state is '${sessionState}'`) this.printedSessionState.state = sessionState this.printedSessionState.printed = false diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 62a144e..580c1fa 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -287,7 +287,8 @@ export class RequestClient implements HttpClient { }) .then((res) => res.data) .catch((error) => { - console.log(error) + const logger = process.logger || console + logger.error(error) }) } diff --git a/src/utils/fetchLogByChunks.ts b/src/utils/fetchLogByChunks.ts index 35e2be8..a149c4c 100644 --- a/src/utils/fetchLogByChunks.ts +++ b/src/utils/fetchLogByChunks.ts @@ -15,12 +15,14 @@ export const fetchLogByChunks = async ( logUrl: string, logCount: number ): Promise => { + const logger = process.logger || console + let log: string = '' const loglimit = logCount < 10000 ? logCount : 10000 let start = 0 do { - console.log( + logger.info( `Fetching logs from line no: ${start + 1} to ${ start + loglimit } of ${logCount}.`