Skip to content

Commit

Permalink
migrated most of the logging to winston
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Feb 14, 2022
1 parent dd730ba commit d2ad9f0
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 107 deletions.
4 changes: 1 addition & 3 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ const pool = createPool(
const executeQuery = (query: Query) => {
console.log("executing query");

const res = pool.acquire();

return res
return pool.acquire()
.then(async (client: Client) => {
console.log("aquired client from pool");
const result = await client.execute(query);
Expand Down
19 changes: 11 additions & 8 deletions src/forge-download.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ForgeSDK from "forge-apis";

const fs = require("fs").promises;
import * as fs from "fs";
import * as config from "./config";
import { logger } from './logger';

const ObjectsApi = new ForgeSDK.ObjectsApi();
const ItemsApi = new ForgeSDK.ItemsApi();
Expand All @@ -19,17 +19,20 @@ const ItemsApi = new ForgeSDK.ItemsApi();
exports.download = async (projectID: string, itemID: string, fileName: string, destination: string, credentials: ForgeSDK.AuthToken) => {
const storageLocation = await getStorageLocation(projectID, itemID, credentials);

console.log("Downloading file " + fileName);
logger.info(`download: Downloading file "${fileName}" to ${destination}`);

return ObjectsApi.getObject("wip.dm.prod", storageLocation, {}, config.authClient, credentials)
.then((res: ForgeSDK.ApiResponse) => {
console.log("Downloaded file " + fileName + " to " + destination);
fs.writeFile(`${destination}/${fileName}`, res.body);
try {
fs.writeFileSync(`${destination}/${fileName}`, res.body);
logger.info(`download: Downloaded file ${fileName} to ${destination}`);
} catch (err) {
logger.error(`download: Error writing file "${fileName}"`, err);
}
return itemID;
})
.catch((err: ForgeSDK.ApiError) => {
console.log("error while processing " + storageLocation);
console.log(err);
logger.error(`download: Error downloading "${fileName}": ${err.statusCode}`, err);
});
};

Expand All @@ -41,6 +44,6 @@ const getStorageLocation = (projectID: string, itemID: string, credentials: Forg
storageID = storageID.substring(storageID.indexOf("/") + 1);
return storageID;
}).catch((err: ForgeSDK.ApiError) => {
console.log("unable to get storage location for " + itemID + ": " + err.statusCode);
logger.error(`download: Unable to get storage location for ${itemID}: ${err.statusCode}`, err);
});
};
12 changes: 4 additions & 8 deletions src/forge-upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ForgeSDK from 'forge-apis';
import * as config from './config';
import { logger } from './logger';
import * as fs from 'fs';

const jsonVersion: ForgeSDK.JsonApiVersionJsonapi = {version: "1.0"}
Expand Down Expand Up @@ -138,8 +139,8 @@ function createItem(fileName: string, folderID: string, objectID: string, creden
return true;
},
(err: ForgeSDK.ApiError) => {
console.log("API ERROR CODE: ", err.statusCode, "\nMESSAGE: ", err.statusMessage, "\nBODY: ", err.statusBody);
return false;
logger.error(`upload: Error creating a new item: ${err.statusCode}`, err);
return false;
}
);
}
Expand Down Expand Up @@ -171,12 +172,7 @@ function uploadFileObject(fileName: string, folderName: string, objectID: string
{contentDisposition: fileName},
config.authClient,
credentials).then(
(resp: ForgeSDK.ApiResponse) => {
if (resp.statusCode != 200) {
console.log("Upload File Error: ", resp.statusCode);
return false;
}

(res: ForgeSDK.ApiResponse) => {
return createItem(fileName, config.folderMap[folderName].fusionID, objectID, credentials);
},
(err: ForgeSDK.ApiError) => {
Expand Down
34 changes: 17 additions & 17 deletions src/forge-webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as axios from "axios";
import * as ForgeSDK from "forge-apis";
import * as config from "./config";
import { logger } from "./logger";

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 2/7/2022
Expand All @@ -22,7 +23,8 @@ const fileShareID = "urn:adsk.wipprod:fs.folder:co.T0n0mYQeS16K0lq1VuuYVQ";
* Function to manage hook lifecycle
*/
exports.setupHooks = (callbackUrl: string, credentials: ForgeSDK.AuthToken) => {
console.log("Checking hooks");
logger.info('webhooks: Checking webhooks');

axios.default({
method: "GET",
url: "https://developer.api.autodesk.com/webhooks/v1/hooks",
Expand All @@ -42,7 +44,7 @@ exports.setupHooks = (callbackUrl: string, credentials: ForgeSDK.AuthToken) => {
} else {
hookEvents.push(hook.event);
if (hook.status === "inactive") {
console.log("reactivating " + hook.event + " hook");
logger.info(`webhooks: Reactivating hook for ${hook.event}`);
reactivateHook(hook.event, hook.hookId, credentials);
}
}
Expand All @@ -62,7 +64,7 @@ exports.setupHooks = (callbackUrl: string, credentials: ForgeSDK.AuthToken) => {
}
})
.catch((err: axios.AxiosError) => {
console.log(err);
logger.error(`webhooks: Error getting webhooks: ${err.code}`, err.response);
});
};

Expand All @@ -83,10 +85,10 @@ const createHook = (event: string, callbackUrl: string, credentials: ForgeSDK.Au
},
})
.then((res: axios.AxiosResponse) => {
console.log("Sucessfully created hook for " + event);
logger.info(`webhooks: Successfully created hook for ${event}`, res.data);
})
.catch((err: axios.AxiosError) => {
err.response && console.log("error creating hook for " + event + ": " + err.response.status);
err.response && logger.error(`webhooks: Error creating hook for ${event}: ${err.code}`, err.response);
});
};

Expand All @@ -104,10 +106,10 @@ const reactivateHook = (event: string, hookID: string, credentials: ForgeSDK.Aut
},
})
.then((res: axios.AxiosResponse) => {
console.log("successfully reactivated " + event + " hook");
logger.info(`webhooks: Successfully reactivated ${event} hook`, res.data);
})
.catch((err: axios.AxiosError) => {
err.response && console.log("error reactivating hook for " + event + ": " + err.response.status);
logger.error(`webhooks: Error reactivating hook for ${event}: ${err.code}`, err.response);
});
};

Expand All @@ -120,17 +122,15 @@ const deleteHook = (event: string, hookID: string, credentials: ForgeSDK.AuthTok
},
})
.then((res: axios.AxiosResponse) => {
console.log("deleted " + event + " hook with bad callback url");
logger.info(`webhooks: Deleted ${event} hook with bad callback URL`, res.data);
})
.catch((err: axios.AxiosError) => {
if (err.response) {
console.log("error deleting hook for " + event + ": " + err.response.status);
}
err.response && logger.error(`webhooks: Error deleteing hook for ${event}: ${err.code}`, err.response);
});
};

exports.setupToken = (credentials: ForgeSDK.AuthToken) => {
console.log("Checking webhook token");
logger.info('webhooks: Checking webhook token');

axios.default({
method: "POST",
Expand All @@ -144,15 +144,15 @@ exports.setupToken = (credentials: ForgeSDK.AuthToken) => {
},
})
.then((res: axios.AxiosResponse) => {
console.log(res.data.detail);
logger.info('webhooks: Added new webhook token', res.data)
})
.catch((err: axios.AxiosError) => {
if (err.response) {
if (err.response.status == 400) {
console.log("Webhook token already exists, updating");
logger.info('webhooks: Webhook token already exists, updating');
updateToken(credentials);
} else {
console.log("Error creating a new webhook token");
logger.error(`webhooks: Error creating webhook token: ${err.code}`, err.response);
}
}
});
Expand All @@ -172,9 +172,9 @@ const updateToken = (credentials: ForgeSDK.AuthToken) => {
},
})
.then((res: axios.AxiosResponse) => {
console.log("Successfully updated webhook token");
logger.info('webhooks: Successfully updated webhook token', res.data);
})
.catch((err: axios.AxiosError) => {
console.log("Error updating webhook token");
logger.error(`webhooks: Error updating webhook token: ${err.code}`, err);
});
};
31 changes: 14 additions & 17 deletions src/localhook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as chokidar from 'chokidar';
import { AuthToken } from 'forge-apis';
import * as uploader from './forge-upload';
import { logger } from './logger';

const polltime: number = 100;

Expand Down Expand Up @@ -54,8 +55,9 @@ function extractFolder(path: string): string[] {
* Signal indexing complete, display tracked folders
*/
watcher.on('ready', () => {
console.log('\nIndexing complete\nPolling every ' + polltime + ' milliseconds\nWatching directories:');
watcher.getWatched()['.'].forEach((element) => console.log(element + '/'));
let watched: string = "";
watcher.getWatched()['.'].forEach((element) => watched += ` ${element}/\n`);
logger.info(`localhook: Indexing complete\n Polling every ${polltime} milliseconds\n Watching directories:\n${watched}`);
});

/* Gavin Williams | will1742 | 01/19/22
Expand All @@ -65,16 +67,11 @@ watcher.on('ready', () => {
watcher.on('add', (path: string) => {
try {
[folder, file] = extractFolder(path);
} catch (e) {
console.log("Invalid Path");
} catch (err) {
logger.error(`localhook: Invalid path`, err);
return;
}
console.log(file + ' locally added to ' + folder);
console.log(
'\x1b[93mlocalhook.js::add:',
'\x1b[0mAttempting upload...'
);

logger.info(`localhook: ${file} locally added to ${folder}`);
uploader.uploadFile(file, folder.toLowerCase(), credentials);
});

Expand All @@ -85,11 +82,11 @@ watcher.on('add', (path: string) => {
watcher.on('unlink', (path: string) => {
try {
[folder, file] = extractFolder(path);
} catch (e) {
console.log("Invalid Path");
} catch (err) {
logger.error(`localhook: Invalid path`, err);
return;
}
console.log(file + ' has been removed from ' + folder);
logger.info(`localhook: ${file} has been removed from ${folder}`);
});

/* Kevin Pan | pan261 | 01/30/22
Expand All @@ -99,15 +96,15 @@ watcher.on('unlink', (path: string) => {
watcher.on('change', (path: string) => {
try {
[folder, file] = extractFolder(path);
} catch (e) {
console.log("Invalid Path");
} catch (err) {
logger.error(`localhook: Invalid path`, err);
return;
}
console.log(file + ' has been modified in ' + folder)
logger.info(`localhook: ${file} has been modified in ${folder}`);
});

// Error handling so errors don't crash the server
watcher.on('error', (err: any) => {
console.log("Chokidar error:", err)
logger.error(`localhook: Chokidar error`, err);
})

46 changes: 38 additions & 8 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
import winston = require('winston');
import * as winston from 'winston';

const timezone = () => {
return new Date().toLocaleString('en-US', {
timeZone: 'America/New_York'
});
}

const consoleFormat = (info: winston.Logform.TransformableInfo) => {
return `${[info.timestamp]}: ${info.level}: ${info.message}`;
}

const fileFormat = (info: winston.Logform.TransformableInfo) => {
let meta = info.metadata;
let timestamp = meta.timestamp

if (Object.keys(meta).length === 1) {
return `${meta.timestamp}: ${info.level}: ${info.message}`;
}

delete info.timestamp;

return `${timestamp}: ${info.level}: ${info.message}\nData:` + JSON.stringify(info.metadata, null, '\t') + '\n';
}

export const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp({format: 'MMM-DD-YYYY HH:mm:ss'}),
winston.format.align(),
winston.format.printf(info => `${info.level}: ${[info.timestamp]}: ${info.message}`),
),
transports: [
new winston.transports.Console({
level: 'error'
level: 'info',
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.timestamp({format: timezone}),
winston.format.prettyPrint(),
winston.format.printf(info => consoleFormat(info)),
)
}),
new winston.transports.File({
level: 'info',
filename: './logs/ffs.log'
filename: `${__dirname}/logs/ffs.log`,
format: winston.format.combine(
winston.format.timestamp({format: timezone}),
winston.format.metadata(),
winston.format.prettyPrint(),
winston.format.printf(info => fileFormat(info)),
)
})
]
});
Loading

0 comments on commit d2ad9f0

Please sign in to comment.