From 10eb375368bc59881713195c7d65b2925d5fc32d Mon Sep 17 00:00:00 2001 From: Sandor Trombitas Date: Fri, 16 Aug 2024 12:33:00 +0300 Subject: [PATCH] chore: improve logging with timestamps --- ts-binary-wrapper/src/common.ts | 35 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/ts-binary-wrapper/src/common.ts b/ts-binary-wrapper/src/common.ts index e579fdb041..f88baf1743 100644 --- a/ts-binary-wrapper/src/common.ts +++ b/ts-binary-wrapper/src/common.ts @@ -66,6 +66,10 @@ export class WrapperConfiguration { } } +const logErrorWithTimeStamps = (...args) => { + console.error(`${new Date().toISOString()}:`, ...args); +} + export function determineBinaryName(platform: string, arch: string): string { let osname = platform; let archname = arch; @@ -180,7 +184,7 @@ export function runWrapper(executable: string, cliArguments: string[]): number { const debug = debugEnabled(cliArguments); if (debug) { - console.error('Executing: ' + executable + ' ' + cliArguments.join(' ')); + logErrorWithTimeStamps('Executing: ' + executable + ' ' + cliArguments.join(' ')); } const res = spawnSync(executable, cliArguments, { @@ -195,14 +199,14 @@ export function runWrapper(executable: string, cliArguments: string[]): number { if (res.status !== null) { if (debug) { - console.error(res); + logErrorWithTimeStamps(res); } return res.status; } else { - console.error(res); + logErrorWithTimeStamps(res); if (!formatErrorMessage((res.error as SpawnError).code)) { - console.error('Failed to spawn child process. (' + executable + ')'); + logErrorWithTimeStamps('Failed to spawn child process. (' + executable + ')'); } return 2; @@ -243,7 +247,7 @@ export function formatErrorMessage(message: string): boolean { return false; } - console.error(getWarningMessage(warning)); + logErrorWithTimeStamps(getWarningMessage(warning)); return true; } @@ -253,6 +257,7 @@ export function downloadExecutable( filenameShasum: string, ): Promise { return new Promise(function(resolve) { + logErrorWithTimeStamps('Starting download'); const options = new URL(`${downloadUrl}?utm_source=${integrationName}`); const temp = path.join(__dirname, Date.now().toString()); const fileStream = fs.createWriteStream(temp); @@ -282,19 +287,19 @@ export function downloadExecutable( if (filenameShasum && actualShasum != filenameShasum) { cleanupAfterError(Error('Shasum comparison failed!\n' + debugMessage)); } else { - console.error(debugMessage); + logErrorWithTimeStamps(debugMessage); // finally rename the file and change permissions fs.renameSync(temp, filename); fs.chmodSync(filename, 0o755); - console.error('Downloaded successfull! '); + logErrorWithTimeStamps('Downloaded successfull! '); } resolve(undefined); }); - console.error( - "Downloading from '" + downloadUrl + "' to '" + filename + "'", + logErrorWithTimeStamps( + "Downloading from '" + options.toString() + "' to '" + filename + "'", ); const req = https.get(options, (res) => { @@ -346,9 +351,9 @@ export async function downloadWithBackup( filenameShasum, ); if (error) { - console.error(error); - console.error( - 'Download failed! Trying to download from backup location...', + logErrorWithTimeStamps(error); + logErrorWithTimeStamps( + `Failed to download from ${downloadUrl}! Trying to download from ${backupUrl} location...`, ); const backupError = await downloadExecutable( backupUrl, @@ -356,12 +361,12 @@ export async function downloadWithBackup( filenameShasum, ); - console.error(backupError); + logErrorWithTimeStamps(backupError); return backupError; } } catch (err) { // Handle any unexpected errors - console.error('An unexpected error occurred:', err); + logErrorWithTimeStamps('An unexpected error occurred:', err); throw err; // Rethrow if you want to propagate the error upwards } } @@ -389,7 +394,7 @@ export async function logError( // finally log the error to the console as well if (printToConsole) { - console.error('\n' + err); + logErrorWithTimeStamps('\n' + err); formatErrorMessage(err.message); } }