From fa95e5b3e930f6dbdac8b4642afee5dde83b6442 Mon Sep 17 00:00:00 2001 From: skutner Date: Wed, 21 Sep 2022 11:06:12 +0300 Subject: [PATCH] PharmaLedger-IMI/epi-workspace#918 using custom logger for apihub logs --- components/admin/demo.pla.js | 8 ++-- components/anchoring/controllers/index.js | 8 ++-- components/anchoring/strategies/ethx/index.js | 4 +- .../strategies/fsx/filePersistence.js | 6 +-- components/anchoring/strategies/oba/index.js | 27 ++++++------ .../tests/acquireAndReleaseLockTest.js | 1 - .../tests/acquireLockAfterPreviousExpired.js | 1 - components/bdns/index.js | 14 +++--- components/bricking/utils.js | 5 +-- components/bricksFabric/controllers.js | 1 - components/bricksFabric/index.js | 3 +- .../bricksFabric/strategies/BrickStorage.js | 17 ++++---- components/cloudWallet/controller.js | 32 +++++++------- components/contracts/boot.js | 5 ++- components/contracts/index.js | 3 +- components/contracts/utils.js | 8 ++-- components/debugLogger/controllers.js | 13 +++--- components/enclave/index.js | 10 ++--- components/keySsiNotifications/index.js | 5 ++- components/mainDSU/controller.js | 17 ++++---- components/mqHub/adapters/localMQAdapter.js | 27 ++++++------ components/mqHub/auth/JWTIssuer.js | 10 ++--- components/mqHub/index.js | 27 ++++++------ components/requestForwarder/index.js | 6 +-- components/secrets/index.js | 9 ++-- components/staticServer/index.js | 14 +++--- components/stream/controller.js | 9 ++-- components/stream/worker-script.js | 14 ++---- config/config-migrator.js | 7 +-- config/index.js | 33 +++++++------- index.js | 43 +++++++++---------- libs/Notifications.js | 3 +- libs/http-wrapper/src/classes/Client.js | 4 +- libs/http-wrapper/src/classes/Server.js | 1 - middlewares/authorisation/index.js | 4 +- middlewares/genericErrorMiddleware/index.js | 5 ++- middlewares/logger/index.js | 13 +++--- middlewares/oauth/lib/AccessTokenValidator.js | 4 +- middlewares/oauth/lib/OauthMiddleware.js | 6 ++- middlewares/requestEnhancements/index.js | 4 +- middlewares/responseHeader/index.js | 4 +- utils/request-utils.js | 6 ++- 42 files changed, 229 insertions(+), 212 deletions(-) diff --git a/components/admin/demo.pla.js b/components/admin/demo.pla.js index afb9cbb..a09ceb3 100644 --- a/components/admin/demo.pla.js +++ b/components/admin/demo.pla.js @@ -1,5 +1,7 @@ require("../../../../psknode/bundles/openDSU"); openDSURequire('overwrite-require'); +const logger = $$.getLogger("demo.pla.js", "apihub/admin"); + const opendsu = openDSURequire("opendsu"); const http = opendsu.loadApi("http"); @@ -55,7 +57,7 @@ async function storeVariable(dns, prop, value) { "variableName": prop, "variableContent": value })); - console.log(`Finished storing variable ${prop}=${value} for ${dns}`); + logger.info(`Finished storing variable ${prop}=${value} for ${dns}`); } catch (e) { console.trace(e); process.exit(1); @@ -69,7 +71,7 @@ async function createDomain(domainName, cloneFrom) { "domainName": domainName, "cloneFromDomain": cloneFrom })); - console.log(`Finished createDomain ${domainName} based on ${cloneFrom}`); + logger.info(`Finished createDomain ${domainName} based on ${cloneFrom}`); } catch (e) { console.trace(e); process.exit(1); @@ -83,7 +85,7 @@ async function registerTemplate(path, content) { path, content })); - console.log(`Finished registering template for path ${path}`); + logger.info(`Finished registering template for path ${path}`); } catch (e) { console.trace(e); process.exit(1); diff --git a/components/anchoring/controllers/index.js b/components/anchoring/controllers/index.js index a1cf4d0..fab1150 100644 --- a/components/anchoring/controllers/index.js +++ b/components/anchoring/controllers/index.js @@ -1,7 +1,7 @@ const {ALIAS_SYNC_ERR_CODE} = require("../utils"); const utils = require("../utils"); const anchoringStrategies = require("../strategies"); - +const logger = $$.getLogger("controllers", "apihub/anchoring"); const getStrategy = async (request) => { let receivedDomain; let domainConfig; @@ -49,7 +49,7 @@ function getWritingHandler(response) { } else if (err.code === 403) { return response.send(403, errorMessage); } - console.log(err); + logger.error(err); return response.send(500, errorMessage); } @@ -62,7 +62,7 @@ async function updateAnchor(action, request, response) { try { strategy = await getStrategy(request); } catch (e) { - console.log(e); + logger.error(e); return response.send(500, e); } strategy[action](getWritingHandler(response)); @@ -92,7 +92,7 @@ async function readDataForAnchor(action, request, response) { try { strategy = await getStrategy(request); } catch (e) { - console.log(e); + logger.error(e); return response.send(500, e); } strategy[action](getReadingHandler(response)); diff --git a/components/anchoring/strategies/ethx/index.js b/components/anchoring/strategies/ethx/index.js index e5886a1..a177d47 100644 --- a/components/anchoring/strategies/ethx/index.js +++ b/components/anchoring/strategies/ethx/index.js @@ -3,7 +3,7 @@ const {ALIAS_SYNC_ERR_CODE} = require("../../utils"); function Ethx(server, domainConfig, anchorId, newAnchorValue, jsonData) { const openDSU = require("opendsu"); const http = openDSU.loadAPI("http"); - + const logger = $$.getLogger("Eth", "apihub/anchoring"); const createEndpoint = (action) => { let endpoint = domainConfig.option.endpoint; @@ -46,7 +46,7 @@ function Ethx(server, domainConfig, anchorId, newAnchorValue, jsonData) { error.code = ALIAS_SYNC_ERR_CODE; return callback(error); } - console.log(err); + logger.error(err); callback(err); return; } diff --git a/components/anchoring/strategies/fsx/filePersistence.js b/components/anchoring/strategies/fsx/filePersistence.js index 18bb375..17c9a6c 100644 --- a/components/anchoring/strategies/fsx/filePersistence.js +++ b/components/anchoring/strategies/fsx/filePersistence.js @@ -116,7 +116,7 @@ function FileOperations() { const path = require('path'); let anchoringFolder; const endOfLine = require("os").EOL; - + const logger = $$.getLogger("FileOperations", "apihub/anchoring"); self.InitializeFolderStructure = function (rootFolder, configuredPath) { let storageFolder = path.join(rootFolder, configuredPath); anchoringFolder = path.resolve(storageFolder); @@ -125,7 +125,7 @@ function FileOperations() { fs.mkdirSync(anchoringFolder, {recursive: true}); } } catch (e) { - console.log("error creating anchoring folder", e); + logger.error("error creating anchoring folder", e); throw new Error(`Failed to create folder ${anchoringFolder}`); } } @@ -137,7 +137,7 @@ function FileOperations() { let forbiddenCharacters = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g); if (forbiddenCharacters.test(anchorId)) { - console.log(`Found forbidden characters in anchorId ${anchorId}`); + logger.error(`Found forbidden characters in anchorId ${anchorId}`); return callback(new Error(`anchorId ${anchorId} contains forbidden characters`)); } return callback(undefined); diff --git a/components/anchoring/strategies/oba/index.js b/components/anchoring/strategies/oba/index.js index 45f348c..fd52df6 100644 --- a/components/anchoring/strategies/oba/index.js +++ b/components/anchoring/strategies/oba/index.js @@ -1,27 +1,26 @@ const {getEthereumSyncServiceSingleton} = require("./ethereumSyncService"); -const LOG_IDENTIFIER = "[OBA]"; function OBA(server, domainConfig, anchorId, anchorValue, ...args) { let {FS, ETH} = require("../index"); const fsHandler = new FS(server, domainConfig, anchorId, anchorValue, ...args); const ethHandler = new ETH(server, domainConfig, anchorId, anchorValue, ...args); const ethSyncService = getEthereumSyncServiceSingleton(server); + const logger = $$.getLogger("OBA", "apihub/anchoring"); this.createAnchor = function (callback) { - console.log("Create anchor", anchorId, anchorValue); fsHandler.createAnchor((err, res) => { if (err) { return callback(err); } - console.log(`${LOG_IDENTIFIER} optimistic create anchor ended with success.`); + logger.info(`optimistic create anchor ended with success.`); ethSyncService.storeAnchor("createAnchor", anchorId, anchorValue, domainConfig,(err) => { if (err) { - console.log(`${LOG_IDENTIFIER} failed to store anchor ${fsHandler.commandData.anchorId} in db.`); + logger.error(`Failed to store anchor ${fsHandler.commandData.anchorId} in db.`); return; } - console.log(`${LOG_IDENTIFIER} anchor ${fsHandler.commandData.anchorId} stored in db successfully.`); + logger.info(`Anchor ${fsHandler.commandData.anchorId} stored in db successfully.`); return callback(undefined, res); }) }); @@ -32,14 +31,14 @@ function OBA(server, domainConfig, anchorId, anchorValue, ...args) { if (err) { return callback(err); } - console.log(`${LOG_IDENTIFIER} optimistic append anchor ended with success.`); + logger.info(`Optimistic append anchor ended with success.`); ethSyncService.storeAnchor("appendAnchor", anchorId, anchorValue, domainConfig, (err) => { if (err) { - console.log(`${LOG_IDENTIFIER} failed to store anchor ${fsHandler.commandData.anchorId} in db.`); + logger.error(`failed to store anchor ${fsHandler.commandData.anchorId} in db.`); return; } - console.log(`${LOG_IDENTIFIER} anchor ${fsHandler.commandData.anchorId} stored in db successfully.`); + logger.info(`Anchor ${fsHandler.commandData.anchorId} stored in db successfully.`); return callback(undefined, res); }) @@ -47,10 +46,10 @@ function OBA(server, domainConfig, anchorId, anchorValue, ...args) { } function readAllVersionsFromBlockchain(callback) { - console.log(`${LOG_IDENTIFIER} preparing to read info about anchorId ${fsHandler.commandData.anchorId} from the blockchain...`); + logger.info(`Preparing to read info about anchorId ${fsHandler.commandData.anchorId} from the blockchain...`); ethHandler.getAllVersions((err, anchorVersions) => { if (err) { - console.log(`${LOG_IDENTIFIER} anchorId ${fsHandler.commandData.anchorId} syncing blockchain failed. ${err}`); + logger.error(`AnchorId ${fsHandler.commandData.anchorId} syncing blockchain failed. ${err}`); return callback(err); } @@ -63,20 +62,20 @@ function OBA(server, domainConfig, anchorId, anchorValue, ...args) { } if (history === "") { - console.log(`${LOG_IDENTIFIER} anchorId ${fsHandler.commandData.anchorId} synced but no history found.`); + logger.info(`AnchorId ${fsHandler.commandData.anchorId} synced but no history found.`); //if we don't retrieve info from blockchain we exit return callback(undefined, anchorVersions); } - console.log(`${LOG_IDENTIFIER} found info about anchorId ${fsHandler.commandData.anchorId} in blockchain.`); + logger.info(`Found info about anchorId ${fsHandler.commandData.anchorId} in blockchain.`); //storing locally the history of the anchorId read from the blockchain fsHandler.fps.createAnchor(anchorId, history, (err) => { if (err) { - console.log(`${LOG_IDENTIFIER} failed to store info about anchorId ${fsHandler.commandData.anchorId} on local because of ${err}`); + logger.error(`Failed to store info about anchorId ${fsHandler.commandData.anchorId} on local because of ${err}`); return callback(err); } - console.log(`${LOG_IDENTIFIER} anchorId ${fsHandler.commandData.anchorId} fully synced.`); + logger.info(`AnchorId ${fsHandler.commandData.anchorId} fully synced.`); //even if we read all the versions of anchorId we return only the last one return callback(undefined, anchorVersions); }); diff --git a/components/anchoring/strategies/tests/acquireAndReleaseLockTest.js b/components/anchoring/strategies/tests/acquireAndReleaseLockTest.js index 64d514d..0203b16 100644 --- a/components/anchoring/strategies/tests/acquireAndReleaseLockTest.js +++ b/components/anchoring/strategies/tests/acquireAndReleaseLockTest.js @@ -26,7 +26,6 @@ assert.callback("Acquire and release lock", (callback) => { await $$.promisify(newFsLock.acquireLock)(); callback(); } catch (e) { - console.log(e); assert.true(typeof e === "undefined"); } }) diff --git a/components/anchoring/strategies/tests/acquireLockAfterPreviousExpired.js b/components/anchoring/strategies/tests/acquireLockAfterPreviousExpired.js index c55f213..19d6ed0 100644 --- a/components/anchoring/strategies/tests/acquireLockAfterPreviousExpired.js +++ b/components/anchoring/strategies/tests/acquireLockAfterPreviousExpired.js @@ -19,7 +19,6 @@ assert.callback("Acquire lock after previous expired", (callback) => { assert.true(typeof err === "undefined"); setTimeout(()=>{ newFsLock.acquireLock((err)=>{ - console.log(err); assert.true(typeof err === "undefined"); callback(); }) diff --git a/components/bdns/index.js b/components/bdns/index.js index 5fca767..9a61251 100644 --- a/components/bdns/index.js +++ b/components/bdns/index.js @@ -1,4 +1,6 @@ function BDNS(server) { + const logger = $$.getLogger("BDNS", "apihub/bdns"); + const DOMAIN_TEMPLATE = { "replicas": [], "brickStorages": [ @@ -45,12 +47,12 @@ function BDNS(server) { Object.assign(newRegistry, bdnsExtensions); bdnsCache = JSON.stringify(newRegistry); } catch (e) { - console.log(`Failed to get bdns hosts from url`, e); + logger.error(`Failed to get bdns hosts from url`, e); } } try { - console.log("Testing to see if admin component is active and can be used to expand BDNS configuration."); + logger.info("Testing to see if admin component is active and can be used to expand BDNS configuration."); let adminService = require("./../admin").getAdminService(); let getDomains = $$.promisify(adminService.getDomains); let domains = await getDomains(); @@ -66,9 +68,9 @@ function BDNS(server) { Object.assign(newRegistry, bdnsExtensions); bdnsCache = JSON.stringify(newRegistry); } - console.log("BDNS configuration was updated accordingly to information retrieved from admin service"); + logger.info("BDNS configuration was updated accordingly to information retrieved from admin service"); } catch (err) { - console.info("Admin service not available, skipping the process of loading dynamic configured domains. This is not a problem, it's a configuration."); + logger.info("Admin service not available, skipping the process of loading dynamic configured domains. This is not a problem, it's a configuration."); } } @@ -77,6 +79,7 @@ function BDNS(server) { await initialize(); } catch (e) { response.statusCode = 500; + logger.error('Failed to initialize BDNS', e); return response.end('Failed to initialize BDNS'); } @@ -85,8 +88,9 @@ function BDNS(server) { response.statusCode = 200; response.end(bdnsCache); } else { - console.log("Bdns config not available at this moment. A 404 response will be sent."); + logger.info("Bdns config not available at this moment. A 404 response will be sent."); response.statusCode = 404; + logger.error('BDNS hosts not found'); return response.end('BDNS hosts not found'); } } diff --git a/components/bricking/utils.js b/components/bricking/utils.js index 0fde1c9..111f376 100644 --- a/components/bricking/utils.js +++ b/components/bricking/utils.js @@ -1,7 +1,6 @@ const { clone } = require("../../utils"); const { getLocalBdnsEntryListExcludingSelfAsync, getHeadersWithExcludedProvidersIncludingSelf } = require("../../utils/request-utils"); -const config = require("../../config"); - +const logger = $$.getLogger("bricking", "apihub/bricking") function convertReadableStreamToBuffer(readStream, callback) { let buffers = []; @@ -13,7 +12,7 @@ function convertReadableStreamToBuffer(readStream, callback) { } async function getBricksDomainConfig(domain) { - console.log("Looking for domain", domain); + logger.info("Looking for domain", domain); const config = require("../../config"); let domainConfiguration = await config.getSafeDomainConfig(domain); diff --git a/components/bricksFabric/controllers.js b/components/bricksFabric/controllers.js index 999f2d7..98c4eca 100644 --- a/components/bricksFabric/controllers.js +++ b/components/bricksFabric/controllers.js @@ -3,7 +3,6 @@ function createHandler(flow, server) { return function storeTransaction (request, response, next) { - console.log('store anchored called'); //strategy is already booted up flow.storeData(request.body, server, (err, result) => { if (err) { diff --git a/components/bricksFabric/index.js b/components/bricksFabric/index.js index a02afd6..d88378f 100644 --- a/components/bricksFabric/index.js +++ b/components/bricksFabric/index.js @@ -10,12 +10,13 @@ function AutoSavePendingTransactions (flow, timeout, server) { function BricksFabric(server) { + const logger = $$.getLogger("BricksFabric", "apihub/bricksFabric"); require('./strategies/BrickStorage.js'); const bricksFabricStrategy = require('./utils').getBricksFabricStrategy(); if (!bricksFabricStrategy) { - console.log("Unable to initialized 'bricksFabrick' component. Strategy not found!"); + logger.error("Unable to initialized 'bricksFabrick' component. Strategy not found!"); return; } const rootFolder = require('./utils').getRootFolder(); diff --git a/components/bricksFabric/strategies/BrickStorage.js b/components/bricksFabric/strategies/BrickStorage.js index f0e21fb..5f76efa 100644 --- a/components/bricksFabric/strategies/BrickStorage.js +++ b/components/bricksFabric/strategies/BrickStorage.js @@ -4,6 +4,8 @@ const BRICKSFABRIC_ERROR_CODE = 'bricks fabric error'; function BrickStorage() { + const logger = $$.getLogger("BrickStorage", "apihub/bricksFabric"); + this.init = function (brickFabricRootFolder,noOfTransactionsPerBlock) { this.rootFolder = brickFabricRootFolder; this.transactionsPerBlock = noOfTransactionsPerBlock; @@ -70,7 +72,7 @@ function BrickStorage() { try { server.makeLocalRequest(blockMethod, blockPath, data, blockHeaders, (err, result) => { if (err) { - console.log(err); + logger.error(err); __pushBuffer(); this.isCommitingBlock = false; callback(err, undefined); @@ -82,8 +84,6 @@ function BrickStorage() { this.pendingTransactions.splice(0, this.pendingTransactions.length); __pushBuffer(); this.isCommitingBlock = false; - //console.log(result); - console.log('block finished'); callback(undefined, result); } @@ -92,13 +92,12 @@ function BrickStorage() { }); } catch (err) { - console.log("bricks fabric", err); + logger.error("bricks fabric", err); } } function __pushBuffer(){ if (this.pendingBuffer.length > 0) { - console.log("push buffer to pending block", this.pendingBuffer); for (let i = 0; i < this.pendingBuffer.length; i++) { this.pendingTransactions.push(this.pendingBuffer[i]); } @@ -108,19 +107,19 @@ function BrickStorage() { this.storeData = function(anchorData, server, callback) { if (this.isCommitingBlock === true) { - console.log("transaction cached"); + logger.info("transaction cached"); this.pendingBuffer.push(anchorData); callback(undefined,"Transaction was added to the block."); return; } - console.log("transaction pushed to pending block"); + logger.info("transaction pushed to pending block"); this.pendingTransactions.push(anchorData); if (this.pendingTransactions.length >= this.transactionsPerBlock) { - // console.log("commit block callback"); + // logger.info("commit block callback"); this.completeBlock(server, callback); }else { - //console.log("pending callback"); + //logger.info("pending callback"); callback(undefined,"Transaction was added to the block."); } } diff --git a/components/cloudWallet/controller.js b/components/cloudWallet/controller.js index 472be43..6845587 100644 --- a/components/cloudWallet/controller.js +++ b/components/cloudWallet/controller.js @@ -4,7 +4,7 @@ const worker_threads = "worker_threads"; const { Worker } = require(worker_threads); const config = require("../../config"); const path = require("swarmutils").path; - +const logger = $$.getLogger("CloudWallet", "apihub/cloudWallet"); let dsuBootPath; const dsuWorkers = {}; @@ -40,13 +40,13 @@ function addDsuWorker(seed, cookie) { resolver: new Promise((resolve, reject) => { crypto.randomBytes(64, (err, randomBuffer) => { if (err) { - console.log("[CloudWallet] Error while generating worker authorizationKey", err); + logger.error("Error while generating worker authorizationKey", err); return reject(err); } const authorizationKey = randomBuffer.toString("hex"); dsuWorker.authorizationKey = authorizationKey; - console.log(`[CloudWallet] Starting worker for handling seed ${seed}`); + logger.info(`Starting worker for handling seed ${seed}`); const worker = new Worker(dsuBootPath, { workerData: { seed, @@ -62,8 +62,8 @@ function addDsuWorker(seed, cookie) { return reject(message.error); } if (message.port) { - console.log( - `[CloudWallet] Running worker on PORT ${message.port} for seed ${seed}. Startup took ${getElapsedTime( + logger.info( + `Running worker on PORT ${message.port} for seed ${seed}. Startup took ${getElapsedTime( workerStartTime )}` ); @@ -72,11 +72,11 @@ function addDsuWorker(seed, cookie) { } }); worker.on("error", (error) => { - console.log("[CloudWallet] worker error", error); + logger.error("worker error", error); }); worker.on("exit", (code) => { if (code !== 0) { - console.log(`[CloudWallet] Worker stopped with exit code ${code}`); + logger.info(`Worker stopped with exit code ${code}`); // remove the worker from list in order to be recreated when needed delete dsuWorkers[seed]; } @@ -142,14 +142,14 @@ function forwardRequestToWorker(dsuWorker, req, res) { res.statusCode = statusCode; res.end(bodyContent); } catch (err) { - console.log("[CloudWallet] worker response error", err); + logger.error("worker response error", err); res.statusCode = 500; res.end(); } }); }); workerRequest.on("error", (err) => { - console.log("[CloudWallet] worker request error", err); + logger.error("worker request error", err); res.statusCode = 500; res.end(); }); @@ -157,7 +157,7 @@ function forwardRequestToWorker(dsuWorker, req, res) { if (method === "POST" || method === "PUT") { let data = []; req.on("data", (chunk) => { - console.log("[CloudWallet] data.push(chunk);", chunk); + logger.info("data.push(chunk);", chunk); data.push(chunk); }); @@ -167,7 +167,7 @@ function forwardRequestToWorker(dsuWorker, req, res) { workerRequest.write(bodyContent); workerRequest.end(); } catch (err) { - console.log("[CloudWallet] worker response error", err); + logger.error("worker response error", err); res.statusCode = 500; res.end(); } @@ -178,7 +178,7 @@ function forwardRequestToWorker(dsuWorker, req, res) { } function init(server) { - console.log(`Registering CloudWallet component`); + logger.info(`Registering CloudWallet component`); dsuBootPath = config.getConfig("componentsConfig", "cloudWallet", "dsuBootPath"); @@ -186,15 +186,15 @@ function init(server) { dsuBootPath = path.resolve(path.join(process.env.PSK_ROOT_INSTALATION_FOLDER, dsuBootPath)); } - console.log(`[CloudWallet] Using boot script for worker: ${dsuBootPath}`); + logger.info(`Using boot script for worker: ${dsuBootPath}`); cacheContainerPath = require("path").join(server.rootFolder, config.getConfig("externalStorage"), `cache`); //if a listening event is fired from this point on... //it means that a restart was triggered server.on("listening", () => { - console.log(`[CloudWallet] Restarting process in progress...`); - console.log(`[CloudWallet] Stopping a number of ${Object.keys(dsuWorkers).length} thread workers`); + logger.info(`Restarting process in progress...`); + logger.info(`Stopping a number of ${Object.keys(dsuWorkers).length} thread workers`); for (let seed in dsuWorkers) { let worker = dsuWorkers[seed]; if (worker && worker.terminate) { @@ -217,7 +217,7 @@ function handleCloudWalletRequest(request, response) { forwardRequestToWorker(dsuWorker, request, response); }) .catch((error) => { - console.log("[CloudWallet] worker resolver error", error); + logger.error("worker resolver error", error); response.setHeader("Content-Type", "text/html"); response.statusCode = 400; response.end(INVALID_DSU_HTML_RESPONSE); diff --git a/components/contracts/boot.js b/components/contracts/boot.js index bfe46d6..4342d5a 100644 --- a/components/contracts/boot.js +++ b/components/contracts/boot.js @@ -1,6 +1,7 @@ async function boot(validatorDID, serverUrl, domain, domainConfig, rootFolder, storageFolder) { + const logger = $$.getLogger("boot", "apihub/contracts"); const logPrefix = `[contract-worker][${validatorDID}][domain]`; - console.log( + logger.info( `${logPrefix} Booting contracts for domain ${domain} and domainConfig ${JSON.stringify(domainConfig)} booting...`, domainConfig ); @@ -78,7 +79,7 @@ async function boot(validatorDID, serverUrl, domain, domainConfig, rootFolder, s }); }); - console.log(`${logPrefix} ready`); + logger.info(`${logPrefix} ready`); parentPort.postMessage("ready"); } catch (error) { parentPort.postMessage({ error }); diff --git a/components/contracts/index.js b/components/contracts/index.js index 8265520..c624c0a 100644 --- a/components/contracts/index.js +++ b/components/contracts/index.js @@ -6,6 +6,7 @@ const { } = require("./utils"); function Contract(server) { + const logger = $$.getLogger("Contract", "apihub/contracts"); const config = require("../../config"); const serverUrl = `${server.protocol}://${config.getConfig("host")}:${config.getConfig("port")}`; @@ -38,7 +39,7 @@ function Contract(server) { return callback(`[Contracts] Cannot boot worker for domain '${domain}' due to missing validatorDID`); } - console.log(`[Contracts] Starting contract handler for domain '${domain}'...`, domainConfig); + logger.info(`[Contracts] Starting contract handler for domain '${domain}'...`, domainConfig); const { rootFolder } = server; const externalStorageFolder = require("path").join(rootFolder, config.getConfig("externalStorage")); diff --git a/components/contracts/utils.js b/components/contracts/utils.js index 8dd3b08..09f960a 100644 --- a/components/contracts/utils.js +++ b/components/contracts/utils.js @@ -1,3 +1,5 @@ +const logger = $$.getLogger("utils", "contracts"); + function escapePath(path) { return path ? path.replace(/\\/g, "\\\\").replace(".js", "") : ""; } @@ -12,7 +14,7 @@ function ensureContractConstitutionIsPresent(domain, domainConfig) { // ensure we have the SSI for the contracts DSU speficied inside domainConfig.contracts.constitution if (process.env.PSK_APIHUB_DEFAULT_CONTRACTS_DOMAIN_SSI) { contractsConfig.constitution = process.env.PSK_APIHUB_DEFAULT_CONTRACTS_DOMAIN_SSI; - console.log( + logger.info( `[Contracts] no constitution found for domain ${domain}. Found process.env.PSK_APIHUB_DEFAULT_CONTRACTS_DOMAIN_SSI: ${contractsConfig.constitution}` ); } else { @@ -24,7 +26,7 @@ function ensureContractConstitutionIsPresent(domain, domainConfig) { const pskFolder = process.env.PSK_ROOT_INSTALATION_FOLDER || path.resolve("." + __dirname + "/../../../.."); const defaultDomainSeedPath = path.join(pskFolder, "modules/apihub-contracts/domain-seed"); - console.log( + logger.info( `[Contracts] no constitution found for domain ${domain}. Trying to load constitution at ${defaultDomainSeedPath}...` ); @@ -33,7 +35,7 @@ function ensureContractConstitutionIsPresent(domain, domainConfig) { const defaultDomainSeedData = fs.readFileSync(defaultDomainSeedPath); contractsConfig.constitution = defaultDomainSeedData.toString(); } catch (error) { - console.log(`Cannot access default domain-seed at: ${defaultDomainSeedPath}`); + logger.error(`Cannot access default domain-seed at: ${defaultDomainSeedPath}`); } } } diff --git a/components/debugLogger/controllers.js b/components/debugLogger/controllers.js index e20976a..183f880 100644 --- a/components/debugLogger/controllers.js +++ b/components/debugLogger/controllers.js @@ -13,6 +13,8 @@ const levels = { debug: 'debug', }; +const logger = $$.getLogger("debugLogger", "apihub/debugLogger"); + function createHandlerAppendToLog(server) { return function appendToLog(request, response) { if (!request.body || !request.body.message) { @@ -45,7 +47,8 @@ function createHandlerAppendToLog(server) { fs.writeFile(fileName, JSON.stringify(json), (err) => { if (err) { response.send(500); - console.log(err); + logger.error(err); + logger.error(err); return; } else { response.send(200, data); @@ -56,7 +59,7 @@ function createHandlerAppendToLog(server) { fs.writeFile(fileName, JSON.stringify([data]), (err) => { if (err) { response.send(500); - console.log(err); + logger.error(err); return; } else { response.send(200, data); @@ -65,15 +68,15 @@ function createHandlerAppendToLog(server) { }); } } catch (err) { - console.log(err); - console.log('Error writing file to disk'); + logger.error(err); + logger.error('Error writing file to disk'); } }; } function createHandlerReadFromLog(server) { return function readFromLog(request, response) { - console.log('running'); + logger.log('running'); const today = new Date().toISOString().split('T')[0]; const anchorID = request.params.anchorID; const queryObject = url.parse(request.url, true).query; diff --git a/components/enclave/index.js b/components/enclave/index.js index ea13648..96ad2f0 100644 --- a/components/enclave/index.js +++ b/components/enclave/index.js @@ -5,7 +5,7 @@ const w3cDID = openDSU.loadAPI("w3cdid"); const path = require("path"); function LokiEnclaveFacade(server) { - + const logger = $$.getLogger("LokiEnclaveFacade", "apihub/enclave"); let didDocument; w3cDID.createIdentity("key", undefined, process.env.REMOTE_ENCLAVE_SECRET, (err, didDoc) => { @@ -13,7 +13,7 @@ function LokiEnclaveFacade(server) { didDocument.waitForMessages(async (err, res) => { if (err) { - console.log(err); + logger.error(err); return } @@ -21,7 +21,7 @@ function LokiEnclaveFacade(server) { processCommand(JSON.parse(res)); } catch (err) { - console.log(err); + logger.error(err); } }); }); @@ -42,7 +42,7 @@ function LokiEnclaveFacade(server) { return JSON.stringify({ "commandResult": dbResult, "commandID": resObj.commandID }) } catch (err) { - console.log(err); + logger.error(err); return err; } } @@ -50,7 +50,7 @@ function LokiEnclaveFacade(server) { function sendResult(didDocument, result, clientDID) { didDocument.sendMessage(result, clientDID, (err, res) => { if (err) { - console.log(err); + logger.error(err); } }) } diff --git a/components/keySsiNotifications/index.js b/components/keySsiNotifications/index.js index 24c73ee..5fd6373 100644 --- a/components/keySsiNotifications/index.js +++ b/components/keySsiNotifications/index.js @@ -1,4 +1,5 @@ function KeySSINotifications(server) { + const logger = $$.getLogger("KeySSINotifications", "apihub/keySsiNotifications"); let notificationManager; const utils = require('../../utils'); const readBody = utils.streams.readStringFromStream; @@ -70,7 +71,7 @@ function KeySSINotifications(server) { response.send(200, message); } catch (err) { //here we expect to get errors when a connection has reached timeout - console.log(err); + logger.error(err); response.send(400, 'opps'); } }); @@ -84,7 +85,7 @@ function KeySSINotifications(server) { require('./../../libs/Notifications').getManagerInstance(workingDirPath, (err, instance) => { if (err) { - return console.log(err); + return logger.error(err); } notificationManager = instance; diff --git a/components/mainDSU/controller.js b/components/mainDSU/controller.js index 52403e2..363014d 100644 --- a/components/mainDSU/controller.js +++ b/components/mainDSU/controller.js @@ -1,11 +1,12 @@ const config = require("../../config"); +const logger = $$.getLogger("controller", "apihub/mainDSU"); let mainDSUSeedSSI = null; let rootFolderPath; let mainDSUSeedSSIFilePath; function init(server) { - console.log(`Registering MainDSU component`); + logger.info(`Registering MainDSU component`); rootFolderPath = server.rootFolder; mainDSUSeedSSIFilePath = require("path").join(server.rootFolder, config.getConfig("externalStorage"), "maindsu"); } @@ -28,33 +29,33 @@ async function handleDefaultMainDSURequest(request, response) { try { const fileContent = await $$.promisify(fs.readFile)(mainDSUSeedSSIFilePath, { encoding: "utf-8" }); mainDSUSeedSSI = keySSISpace.parse(fileContent); - console.log(`[MainDSU] Read existing mainDSU from ${mainDSUSeedSSIFilePath}: ${mainDSUSeedSSI.getIdentifier()}`); + logger.info(`[MainDSU] Read existing mainDSU from ${mainDSUSeedSSIFilePath}: ${mainDSUSeedSSI.getAnchorId().getIdentifier()}`); return sendMainDSUSeedSSI(response); } catch (error) { - console.log(`[MainDSU] Failed to read/parse keySSI from ${mainDSUSeedSSIFilePath}. Generating new keySSI...`, error); + logger.error(`[MainDSU] Failed to read/parse keySSI from ${mainDSUSeedSSIFilePath}. Generating new keySSI...`, error); } try { const environmentJsPath = require("path").join(rootFolderPath, "environment.js"); - console.log(`[MainDSU] Loading environment.js config file from: ${environmentJsPath}`); + logger.info(`[MainDSU] Loading environment.js config file from: ${environmentJsPath}`); const environmentConfig = require(environmentJsPath); const seedSSI = await $$.promisify(keySSISpace.createSeedSSI)(environmentConfig.vaultDomain); const mainDSU = await $$.promisify(resolver.createDSUForExistingSSI)(seedSSI); - console.log(`[MainDSU] Settings config for seed ${seedSSI.getIdentifier()}`, environmentConfig); + logger.info(`[MainDSU] Settings config for seed ${seedSSI.getAnchorId().getIdentifier()}`, environmentConfig); await $$.promisify(mainDSU.writeFile)("/environment.json", JSON.stringify(environmentConfig)); mainDSUSeedSSI = seedSSI; - console.log("[MainDSU] Generated mainDSUSeedSSI: ", mainDSUSeedSSI.getIdentifier(), mainDSUSeedSSI); + logger.info("[MainDSU] Generated mainDSUSeedSSI: ", mainDSUSeedSSI.getAnchorId().getIdentifier(), mainDSUSeedSSI); - console.log(`[MainDSU] Writing generated mainDSU to ${mainDSUSeedSSIFilePath}: ${mainDSUSeedSSI.getIdentifier()}`); + logger.info(`[MainDSU] Writing generated mainDSU to ${mainDSUSeedSSIFilePath}: ${mainDSUSeedSSI.getAnchorId().getIdentifier()}`); await $$.promisify(fs.writeFile)(mainDSUSeedSSIFilePath, mainDSUSeedSSI.getIdentifier(), "utf-8"); sendMainDSUSeedSSI(response); } catch (error) { - console.log("[MainDSU] Failed to create seedSSI", error); + logger.error("[MainDSU] Failed to create seedSSI", error); response.statusCode = 500; response.setHeader("Content-Type", "text/html"); response.end("Failed to create seedSSI"); diff --git a/components/mqHub/adapters/localMQAdapter.js b/components/mqHub/adapters/localMQAdapter.js index 2207619..efb583c 100644 --- a/components/mqHub/adapters/localMQAdapter.js +++ b/components/mqHub/adapters/localMQAdapter.js @@ -1,4 +1,5 @@ function LocalMQAdapter(server, prefix, domain, configuration) { + const logger = $$.getLogger("LocalMQAdapter", "apihub/mqHub"); const subscribers = {}; const config = require("../../../config"); const utils = require('./../../../utils'); @@ -62,7 +63,7 @@ function LocalMQAdapter(server, prefix, domain, configuration) { fileNamesAsTimestamp = sanitizeFileName(fileNamesAsTimestamp); let valid = (new Date(Number(fileNamesAsTimestamp))).getTime() > 0; if (!valid) { - console.log(`Found garbage in queue ${queueName} (file: ${fileNamesAsTimestamp}). Ignoring it!`); + logger.log(`Found garbage in queue ${queueName} (file: ${fileNamesAsTimestamp}). Ignoring it!`); } return valid; }); @@ -231,10 +232,10 @@ function LocalMQAdapter(server, prefix, domain, configuration) { return _readMessage(queueName, (err, message) => { deliverMessage(subs, message, (err, successCount) => { if (err) { - console.log(err); + logger.error(err); } - console.log(`Successfully sent message to a number of ${successCount} subs.`); + logger.info(`Successfully sent message to a number of ${successCount} subs.`); }); }); } else { @@ -266,7 +267,7 @@ function LocalMQAdapter(server, prefix, domain, configuration) { let queueName = request.params.queueName; readBody(request, (err, message) => { if (err) { - console.log(`Caught an error during body reading from put message request`, err); + logger.error(`Caught an error during body reading from put message request`, err); return send(queueName, response, 500); } @@ -279,13 +280,13 @@ function LocalMQAdapter(server, prefix, domain, configuration) { return; } }catch(err){ - console.log("Not able to confirm message size. Going on with the flow..."); + logger.error("Not able to confirm message size. Going on with the flow..."); } } putMessage(queueName, message, (err) => { if (err) { - console.log(`Caught an error during adding message to queue`, err); + logger.error(`Caught an error during adding message to queue`, err); return send(queueName, response, 500, err.sendToUser ? err.message : undefined); } send(queueName, response, 200); @@ -309,7 +310,7 @@ function LocalMQAdapter(server, prefix, domain, configuration) { let {queueName, messageId} = request.params; deleteMessage(queueName, messageId, (err) => { if (err) { - console.log(`Caught an error during deleting message ${messageId} from queue ${queueName}`, err); + logger.error(`Caught an error during deleting message ${messageId} from queue ${queueName}`, err); } send(queueName, response, err ? 500 : 200); }); @@ -319,13 +320,13 @@ function LocalMQAdapter(server, prefix, domain, configuration) { const queueName = request.params.queueName; readMessage(queueName, (err, message) => { if (err) { - console.log(`Caught an error during message reading from ${queueName}`, err); + logger.error(`Caught an error during message reading from ${queueName}`, err); send(queueName, response, 500); return; } deleteMessage(queueName, message.messageId, (err) => { if (err) { - console.log(`Caught an error during message deletion from ${queueName} on the take handler`, err); + logger.error(`Caught an error during message deletion from ${queueName} on the take handler`, err); return send(queueName, response, 500); } @@ -334,10 +335,10 @@ function LocalMQAdapter(server, prefix, domain, configuration) { }); } - console.log(`Loading Local MQ Adapter for domain: ${domain}`); - console.log(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`); - console.log(`Warning: Local MQ Adapter should be used only during development!`); - console.log(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`); + logger.warn(`Loading Local MQ Adapter for domain: ${domain}`); + logger.warn(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`); + logger.warn(`Warning: Local MQ Adapter should be used only during development!`); + logger.warn(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`); const mqConfig = config.getConfig("componentsConfig", "mq"); if (mqConfig && mqConfig.connectionTimeout) { diff --git a/components/mqHub/auth/JWTIssuer.js b/components/mqHub/auth/JWTIssuer.js index 40deb25..298beea 100644 --- a/components/mqHub/auth/JWTIssuer.js +++ b/components/mqHub/auth/JWTIssuer.js @@ -10,7 +10,7 @@ const defaultSettings = { } function JWTIssuer(workingDir) { - + const logger = $$.getLogger("JWTIssuer", "apihub/mqHub"); let seeder; const config = require("./../../../config"); @@ -33,17 +33,17 @@ function JWTIssuer(workingDir) { seeder = await $$.promisify(fs.readFile)(getSeederFilePath()); } catch (err) { if (err.code !== "ENOENT") { - console.log("Not able to read the Issuer persistence file needed by JWT Auth Support layer!", err); + logger.error("Not able to read the Issuer persistence file needed by JWT Auth Support layer!", err); } } if (seeder) { try { seeder = keyssiApi.parse(seeder.toString()); - console.log("MQ JWT AUTH Issuer loaded."); + logger.info("MQ JWT AUTH Issuer loaded."); return; } catch (err) { - console.log("Failed to load MQ JWT AUTH Issuer info. Creating a new Issuer!", + logger.error("Failed to load MQ JWT AUTH Issuer info. Creating a new Issuer!", "\nPrevious tokens will not be valid anymore!!!"); } } @@ -52,7 +52,7 @@ function JWTIssuer(workingDir) { seeder = await $$.promisify(keyssiApi.createSeedSSI)(DOMAIN_NAME); await $$.promisify(fs.writeFile)(getSeederFilePath(), seeder.getIdentifier()); - console.log("New MQ JWT AUTH Issuer created and saved for later use."); + logger.info("New MQ JWT AUTH Issuer created and saved for later use."); } this.createToken = function (domain, options, callback) { diff --git a/components/mqHub/index.js b/components/mqHub/index.js index 9f13e9c..351c0e7 100644 --- a/components/mqHub/index.js +++ b/components/mqHub/index.js @@ -21,6 +21,7 @@ const defaultSettings = { } async function MQHub(server, signalAsyncLoading, doneLoading) { + const logger = $$.getLogger("MQHub", "apihub/mqHub"); signalAsyncLoading(); @@ -35,7 +36,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { const domain = request.params.domain; issuer.createToken(domain, {credentials: request.params.hashDID}, (err, tokenObj) => { if (err) { - console.log("Not able to create a new token.", err); + logger.error("Not able to create a new token.", err); response.statusCode = 500; return response.end(); } @@ -66,7 +67,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { async function putMessageHandler(request, response, next) { const domainName = request.params.domain; if (domains.indexOf(domainName) === -1) { - console.log(`Caught an request to the MQs for domain ${domainName}. Looks like the domain doesn't have mq component enabled.`); + logger.error(`Caught an request to the MQs for domain ${domainName}. Looks like the domain doesn't have mq component enabled.`); response.statusCode = 405; response.end(); return; @@ -75,7 +76,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { let token = request.headers['authorization']; if(! await allowUnregisteredDID(domainName) && !token){ - console.log(`No token was available on the request and the domain ${domainName} configuration prohibits unregisteredDIDs to use the MQ api.`); + logger.error(`No token was available on the request and the domain ${domainName} configuration prohibits unregisteredDIDs to use the MQ api.`); response.statusCode = 403; response.end(); return; @@ -87,7 +88,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { errorMsg = "Token not valid: "; } if (err || !valid) { - console.log(`${errorMsg} < ${token} >`, err ? err : ""); + logger.error(`${errorMsg} < ${token} >`, err ? err : ""); response.statusCode = 403; response.end(); return; @@ -101,7 +102,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { async function getMessageHandler(request, response, next) { const domainName = request.params.domain; if (domains.indexOf(domainName) === -1) { - console.log(`Caught an request to the MQs for domain ${domainName}. Looks like the domain doesn't have mq component enabled.`); + logger.error(`Caught an request to the MQs for domain ${domainName}. Looks like the domain doesn't have mq component enabled.`); response.statusCode = 405; response.end(); return; @@ -110,7 +111,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { let token = request.headers['authorization']; if(! await allowUnregisteredDID(domainName) && !token){ - console.log(`No token was available on the request and the domain ${domainName} configuration prohibits unregisteredDIDs to use the MQ api.`); + logger.error(`No token was available on the request and the domain ${domainName} configuration prohibits unregisteredDIDs to use the MQ api.`); response.statusCode = 403; response.end(); return; @@ -122,7 +123,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { errorMsg = "Ownership not confirmed based on token: "; } if (err || !isOwner) { - console.log(`${errorMsg} < ${token} >`, err ? err : ""); + logger.error(`${errorMsg} < ${token} >`, err ? err : ""); response.statusCode = 403; response.end(); return; @@ -157,15 +158,15 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { const adapterTypeName = domainConfig["mq_type"] || "local"; const adapter = adapterImpls[adapterTypeName]; if (!adapter) { - console.log(`Not able to recognize the mq_type < ${adapterTypeName} > from the domain < ${domain} > config.`); + logger.error(`Not able to recognize the mq_type < ${adapterTypeName} > from the domain < ${domain} > config.`); return; } try { - console.log(`Preparing to register mq endpoints for domain < ${domain} > ... `); + logger.info(`Preparing to register mq endpoints for domain < ${domain} > ... `); adapter(server, URL_PREFIX, domainToBeUsedByAdapter || domain, domainConfig); } catch (err) { - console.log(`Caught an error during initialization process of the mq for domain < ${domain} >`, err); + logger.error(`Caught an error during initialization process of the mq for domain < ${domain} >`, err); return; } @@ -179,13 +180,11 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { let adminService = require("./../../components/admin").getAdminService(); let getDomains = $$.promisify(adminService.getDomains); let virtualDomains = await getDomains(); - //console.log("virtualDomains", virtualDomains); for(let i=0; i.`); + logger.info(`Successfully register mq endpoints for virtual domain < ${domainInfo.pk} >.`); domains.push(domainInfo.pk); } } @@ -197,7 +196,7 @@ async function MQHub(server, signalAsyncLoading, doneLoading) { for (let i = 0; i < confDomains.length; i++) { let domain = confDomains[i]; if(testIfMQEnabled(domain)){ - console.log(`Successfully register mq endpoints for domain < ${domain} >.`); + logger.info(`Successfully register mq endpoints for domain < ${domain} >.`); domains.push(domain); } } diff --git a/components/requestForwarder/index.js b/components/requestForwarder/index.js index 3b98fd7..3df5088 100644 --- a/components/requestForwarder/index.js +++ b/components/requestForwarder/index.js @@ -1,5 +1,5 @@ const registeredUrl = "/forwardRequestForAuthenticatedClient"; - +const logger = $$.getLogger("requestForwarder", "apihub/requestForwarder"); module.exports = function(server){ server.post(registeredUrl, require("./../../utils/middlewares/index").requestBodyJSONMiddleware); @@ -19,7 +19,7 @@ module.exports = function(server){ http = require("https"); } - console.log(`Forwarding request ${options.method} to url ${url}`); + logger.info(`Forwarding request ${options.method} to url ${url}`); try { let request = http.request(url, options, (response) => { @@ -39,7 +39,7 @@ module.exports = function(server){ request.write(body); request.end(); } catch (e) { - console.log("Error on request: ", e); + logger.error("Error on request: ", e); res.statusCode = 500; res.end(); } diff --git a/components/secrets/index.js b/components/secrets/index.js index b631648..37625cf 100644 --- a/components/secrets/index.js +++ b/components/secrets/index.js @@ -2,6 +2,7 @@ const fs = require("fs"); const path = require("path"); function secrets(server) { + const logger = $$.getLogger("secrets", "apihub/secrets"); const secretsFolderPath = path.join(server.rootFolder, "external-volume", "secrets"); server.get("/getSSOSecret/:appName", function (request, response) { let userId = request.headers["user-id"]; @@ -42,22 +43,20 @@ function secrets(server) { function writeSecret(filePath, secret, request, response) { fs.access(filePath, (err)=>{ if (!err) { - console.log("File Already exists"); + logger.error("File Already exists"); response.statusCode = 403; response.end(Error(`File ${filePath} already exists`)); return; } - console.log("Writing file to ", filePath); fs.writeFile(filePath, secret, (err)=>{ if (err) { - console.log("Error at writing file", err); + logger.error("Error at writing file", err); response.statusCode = 500; response.end(err); return; } - console.log("file written success") response.statusCode = 200; response.end(); }); @@ -87,7 +86,7 @@ function secrets(server) { body = Buffer.concat(data).toString(); msgToPersist = JSON.parse(body).secret; } catch (e) { - console.log("Failed to parse body", data); + logger.error("Failed to parse body", data); response.statusCode = 500; response.end(e); } diff --git a/components/staticServer/index.js b/components/staticServer/index.js index 86714ff..9a494a5 100644 --- a/components/staticServer/index.js +++ b/components/staticServer/index.js @@ -4,7 +4,7 @@ function StaticServer(server) { const utils = require("../../utils"); const config = require("../../config"); let componentsConfig = config.getConfig("componentsConfig"); - + const logger = $$.getLogger("StaticServer", "apihub/staticServer"); let excludedFilesRegex; if (componentsConfig && componentsConfig.staticServer && componentsConfig.staticServer.excludedFiles) { excludedFilesRegex = componentsConfig.staticServer.excludedFiles.map(str => new RegExp(str)); @@ -20,7 +20,7 @@ function StaticServer(server) { }); function serverTarget(targetPath) { - console.log("Serving summary for dir:", targetPath); + logger.info("Serving summary for dir:", targetPath); fs.stat(targetPath, function (err, stats) { if (err) { res.statusCode = 404; @@ -116,13 +116,13 @@ function StaticServer(server) { try{ adminService = require("./../admin").getAdminService(); }catch(err){ - //console.log("Caught an error durring admin service initialization", err); + //logger.error("Caught an error durring admin service initialization", err); return callback(err); } adminService.checkForTemplate(req.url, (err, template)=>{ if(err){ - //console.log("Not able to find template for", req.url); + //logger.error("Not able to find template for", req.url); //console.trace(err); return callback(err); } @@ -130,15 +130,11 @@ function StaticServer(server) { let fileContent = template.content; const urlObject = new URL(req.url, `http://${req.headers.host}`); let hostname = urlObject.hostname; - console.log("Preparing to read vars for ", hostname); return adminService.getDomainSpecificVariables(hostname, (err, variables)=>{ if(err || !variables){ - console.log("Not able to get any variable for ", hostname); - //console.log(err); return callback(err); } let domainVariables = Object.keys(variables); - console.log("Domain variables found:", JSON.stringify(domainVariables)); for(let i=0; i { + const logger = $$.getLogger("worker-script", "apihub/stream"); //we inject a supplementary tag in order make it more clear the source of the log - let originalLog = console.log; - console.log = function (...args) { - originalLog("\t[StreamHandler]", ...args); - }; const worker_threads = "worker_threads"; const { parentPort, workerData } = require(worker_threads); - console.log(`Node worker started for: `, workerData); + logger.info(`Node worker started for: `, workerData); const resolver = require("opendsu").loadApi("resolver"); const dsu = await $$.promisify(resolver.loadDSU)(workerData.keySSI); @@ -17,7 +14,7 @@ module.exports = async () => { const CHUNK_SIZE = 1024 * 1024; parentPort.on("message", async (task) => { - console.log("Handling task", task); + logger.info("Handling task", task); const { requestedPath } = task; let { range } = task; @@ -40,9 +37,6 @@ module.exports = async () => { start = parseInt(range, 10); end = start + CHUNK_SIZE; } - console.log(`Handling range start ${start} end ${end}`); - - console.log("Refreshing DSU...") await $$.promisify(dsu.refresh)(); const streamRange = { start, end }; @@ -73,7 +67,7 @@ module.exports = async () => { }); process.on("uncaughtException", (error) => { - console.error("[StreamHandler] uncaughtException inside node worker", error); + logger.error("[StreamHandler] uncaughtException inside node worker", error); setTimeout(() => process.exit(1), 100); }); diff --git a/config/config-migrator.js b/config/config-migrator.js index 5949ecc..64dc858 100644 --- a/config/config-migrator.js +++ b/config/config-migrator.js @@ -1,3 +1,5 @@ +const logger = $$.getLogger("config-migrator", "apihub/config"); + function removeConfigComponent(config) { if (config.componentsConfig && config.componentsConfig.config) { delete config.componentsConfig.config; @@ -24,7 +26,6 @@ function traverseObjectProvidedPrimitiveValues(item, onItemTraversal) { function replaceInternalVolumePathsWithExternalVolume(config) { traverseObjectProvidedPrimitiveValues(config, (item, key) => { let value = item[key]; - console.log("Traversed", key, value); if (key === "path" && typeof value === "string" && value.indexOf("internal-volume") !== -1) { item[key] = value.replace("internal-volume", "external-volume"); } @@ -135,7 +136,7 @@ function migrate(oldConfig, configFolderPath) { const path = require("path"); const fs = require("fs"); const apihubJsonConfigPath = path.join(configFolderPath, "apihub.json"); - console.log(`Generating apihub.json config file at ${apihubJsonConfigPath}...`); + logger.info(`Generating apihub.json config file at ${apihubJsonConfigPath}...`); if (!fs.existsSync(configFolderPath)) { fs.mkdirSync(configFolderPath, { recursive: true }); @@ -150,7 +151,7 @@ function migrate(oldConfig, configFolderPath) { Object.keys(domainConfigs).forEach((domain) => { const domainConfig = domainConfigs[domain]; const domainConfigPath = path.join(domainConfigsFolderPath, `${domain}.json`); - console.log(`Generating config file for domain '${domain}' at ${domainConfigPath}...`); + logger.info(`Generating config file for domain '${domain}' at ${domainConfigPath}...`); fs.writeFileSync(domainConfigPath, JSON.stringify(domainConfig, null, 2)); }); diff --git a/config/index.js b/config/index.js index 772d8f8..98d9e3a 100644 --- a/config/index.js +++ b/config/index.js @@ -1,6 +1,7 @@ let apihubConfig; let tokenIssuers; let domainConfigs = {}; +const logger = $$.getLogger("index", "apihub/config"); function checkIfFileExists(filePath) { try { @@ -8,7 +9,7 @@ function checkIfFileExists(filePath) { fs.accessSync(filePath); return true; } catch (error) { - console.log(`File ${filePath} doesn't exists or no access is possible!`); + logger.error(`File ${filePath} doesn't exists or no access is possible!`); } return false; } @@ -23,20 +24,20 @@ function loadAllDomainConfigs(configFolderPath) { .filter((domainFile) => domainFile.endsWith(".json")) .forEach((domainFile) => { const domain = domainFile.substring(0, domainFile.lastIndexOf(".")); - console.log(`Loading config for domain '${domain}'`); + logger.info(`Loading config for domain '${domain}'`); try { const domainConfig = fs.readFileSync(path.join(domainsFolderPath, domainFile)); domainConfigs[domain] = JSON.parse(domainConfig); } catch (error) { - console.log(`Could not read config for domain '${domain}'`, error); + logger.error(`Could not read config for domain '${domain}'`, error); } }); } catch (error) { - console.log(`Could not read domain configs at ${domainsFolderPath}`, error); + logger.error(`Could not read domain configs at ${domainsFolderPath}`, error); } } else { - console.log(`Domain configs folder not found at ${domainsFolderPath}`); + logger.error(`Domain configs folder not found at ${domainsFolderPath}`); } } @@ -46,16 +47,16 @@ function ensureConfigsAreLoaded() { if(!apihubConfig) { let apihubJson; if (typeof process.env.PSK_CONFIG_LOCATION === "undefined") { - console.log("PSK_CONFIG_LOCATION env variable not set. Not able to load any external config. Using default configuration.") + logger.info("PSK_CONFIG_LOCATION env variable not set. Not able to load any external config. Using default configuration.") apihubJson = {}; } else { const fs = require("fs"); const configFolderPath = path.resolve(process.env.PSK_CONFIG_LOCATION); - console.log("Trying to read the apihub.json file from the location pointed by PSK_CONFIG_LOCATION env variable."); + logger.info("Trying to read the apihub.json file from the location pointed by PSK_CONFIG_LOCATION env variable."); const apihubConfigPath = path.join(configFolderPath, 'apihub.json'); if(!checkIfFileExists(apihubConfigPath)) { - console.log("Trying to read the server.json file from the location pointed by PSK_CONFIG_LOCATION env variable."); + logger.info("Trying to read the server.json file from the location pointed by PSK_CONFIG_LOCATION env variable."); const serverJsonConfigPath = path.join(configFolderPath, 'server.json'); let serverJson; @@ -123,7 +124,7 @@ function ApihubConfig(conf) { conf = createConfig(conf, defaultConf); conf.defaultComponents = defaultConf.activeComponents; if(conf.isDefaultComponent){ - console.log("\n\nBe aware that there is a method on the config called isDefaultComponent. You need to check and change your config name.\n\n"); + logger.info("\n\nBe aware that there is a method on the config called isDefaultComponent. You need to check and change your config name.\n\n"); } conf.isDefaultComponent = function(componentName) { return defaultConf.activeComponents.indexOf(componentName) !== -1 || defaultConf.componentsConfig[componentName]; @@ -153,20 +154,20 @@ function getTokenIssuers(callback) { } const filePath = path.join(path.resolve(process.env.PSK_CONFIG_LOCATION), "issuers-public-identities"); - console.log( + logger.info( `Trying to read the token-issuers.txt file from the location pointed by PSK_CONFIG_LOCATION env variable: ${filePath}` ); fs.access(filePath, fs.F_OK, (err) => { if (err) { - console.log(`${filePath} doesn't exist so skipping it`); + logger.info(`${filePath} doesn't exist so skipping it`); tokenIssuers = []; callback(null, tokenIssuers); } fs.readFile(filePath, "utf8", function (err, data) { if (err) { - console.log(`Cannot load ${filePath}`, err); + logger.error(`Cannot load ${filePath}`, err); return; } @@ -199,7 +200,7 @@ async function getSafeDomainConfig(domain, ...configKeys){ const getDomainInfo = $$.promisify(adminService.getDomainInfo); let domainInfo = await getDomainInfo(domain); if(domainInfo && domainInfo.active && domainInfo.cloneFromDomain){ - console.log(`Config for domain '${domain}' was loaded from admin service.`); + logger.info(`Config for domain '${domain}' was loaded from admin service.`); return getDomainConfig(domainInfo.cloneFromDomain); } }catch(err){ @@ -229,12 +230,12 @@ function getDomainConfig(domain, ...configKeys) { } if (typeof process.env.PSK_CONFIG_LOCATION === "undefined") { - console.log('PSK_CONFIG_LOCATION env variable not set. Not able to load domain config. Using default configuration.') + logger.info('PSK_CONFIG_LOCATION env variable not set. Not able to load domain config. Using default configuration.') return getConfigResult({}); } const domainConfigPath = getDomainConfigFilePath(domain); - console.log(`Trying to read the config for domain '${domain}' at location: ${domainConfigPath}`); + logger.info(`Trying to read the config for domain '${domain}' at location: ${domainConfigPath}`); try { const fsName = "fs"; @@ -243,7 +244,7 @@ function getDomainConfig(domain, ...configKeys) { domainConfigs[domain] = domainConfig; return getConfigResult(domainConfig); } catch (error) { - console.log(`Config for domain '${domain}' cannot be loaded from location: ${domainConfigPath}.`); + logger.error(`Config for domain '${domain}' cannot be loaded from location: ${domainConfigPath}.`); domainConfigs[domain] = null; return domainConfigs[domain]; } diff --git a/index.js b/index.js index 598f4eb..90bf64d 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ const Server = httpWrapper.Server; const TokenBucket = require('./libs/TokenBucket'); const START_TOKENS = 6000000; const CHECK_FOR_RESTART_COMMAND_FILE_INTERVAL = 500; +const logger = $$.getLogger("HttpServer", "apihub"); (function loadDefaultComponents(){ //next require lines are only for browserify build purpose @@ -47,10 +48,9 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart const server = new Server(sslConfig); server.config = conf; server.rootFolder = rootFolder; - let listenCallback = (err) => { if (err) { - console.log(LOG_IDENTIFIER, err); + logger.error(err); if (!dynamicPort && callback) { return OpenDSUSafeCallback(callback)(createOpenDSUErrorWrapper(`Failed to listen on port <${port}>`, err)); } @@ -65,14 +65,13 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart dynamicPort -= 1; } let timeValue = retryTimeout || CHECK_FOR_RESTART_COMMAND_FILE_INTERVAL; - console.log(LOG_IDENTIFIER, `setting a timeout value of before retrying ${timeValue}`); - setTimeout(bootup, ); + setTimeout(bootup, timeValue); } } }; function bootup(){ - console.log(LOG_IDENTIFIER, `Trying to listen on port ${port}`); + logger.info(`Trying to listen on port ${port}`); server.listen(port, conf.host, listenCallback); }; @@ -85,12 +84,12 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart const fs = require(fsname); fs.readFile(restartServerFile, function(error, content) { if (!error && content.toString() !== "") { - console.log(`${LOG_IDENTIFIER} ### Preparing to restart because of the request done by file: <${restartServerFile}> File content: ${content}`); + logger.log(`### Preparing to restart because of the request done by file: <${restartServerFile}> File content: ${content}`); server.close(); server.listen(port, conf.host, () => { fs.writeFile(restartServerFile, "", function(){ //we don't care about this file.. we just clear it's content the prevent recursive restarts - console.log(`${LOG_IDENTIFIER} ### Restart operation finished.`); + logger.log(`### Restart operation finished.`); }); }); } @@ -103,7 +102,7 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart function bindFinished(err) { if (err) { - console.log(err); + logger.error(err); if (callback) { return OpenDSUSafeCallback(callback)(createOpenDSUErrorWrapper(`Failed to bind on port <${port}>`, err)); } @@ -151,7 +150,7 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart }); }); } else { - console.log(`${LOG_IDENTIFIER} Rate limit mechanism disabled!`); + logger.info(`Rate limit mechanism disabled!`); } server.options('/*', function (req, res) { @@ -163,14 +162,14 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart headers['Access-Control-Allow-Credentials'] = true; headers['Access-Control-Max-Age'] = '3600'; //one hour headers['Access-Control-Allow-Headers'] = `Content-Type, Content-Length, X-Content-Length, Access-Control-Allow-Origin, User-Agent, Authorization, token`; - + if(conf.CORS){ - console.log("Applying custom CORS headers"); + logger.info("Applying custom CORS headers"); for(let prop in conf.CORS){ headers[prop] = conf.CORS[prop]; } } - + res.writeHead(200, headers); res.end(); }); @@ -212,7 +211,7 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart if (componentPath.startsWith('.') && !conf.isDefaultComponent(componentName)) { componentPath = path.resolve(path.join(process.env.PSK_ROOT_INSTALATION_FOLDER, componentPath)); } - console.log(`${LOG_IDENTIFIER} Preparing to register middleware from path ${componentPath}`); + logger.info(`Preparing to register middleware from path ${componentPath}`); let middlewareImplementation; try{ @@ -253,7 +252,7 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart .filter(activeComponentName => { let include = conf.componentsConfig[activeComponentName]; if(!include){ - console.log(`${LOG_IDENTIFIER} Not able to find config for component called < ${activeComponentName} >. Excluding it from the active components list!`); + logger.info(`Not able to find config for component called < ${activeComponentName} >. Excluding it from the active components list!`); } return include; }) @@ -261,9 +260,9 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart const addRequiredComponent = (componentName) => { if(!middlewareList.includes(`${componentName}`)) { - console.warn(`WARNING: ${componentName} component is not configured inside activeComponents!`) - console.warn(`WARNING: temporary adding ${componentName} component to activeComponents! Please make sure to include ${componentName} component inside activeComponents!`) - + logger.warn(`WARNING: ${componentName} component is not configured inside activeComponents!`) + logger.warn(`WARNING: temporary adding ${componentName} component to activeComponents! Please make sure to include ${componentName} component inside activeComponents!`) + const addComponentToComponentList = (list) => { const indexOfStaticServer = list.indexOf("staticServer"); if(indexOfStaticServer !== -1) { @@ -273,7 +272,7 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart list.push(componentName); } } - + addComponentToComponentList(middlewareList); // need to also register to defaultComponents in order to be able to load the module correctly addComponentToComponentList(conf.defaultComponents); @@ -307,9 +306,9 @@ function HttpServer({ listeningPort, rootFolder, sslConfig, dynamicPort, restart addRootMiddlewares(); addComponents(()=>{ //at this point all components were installed and we need to register the fallback handler - console.log(LOG_IDENTIFIER, "Registering the fallback handler. Any endpoint registered after this one will have zero changes to be executed."); + logger.info("Registering the fallback handler. Any endpoint registered after this one will have zero changes to be executed."); server.use(function (req, res) { - console.log(LOG_IDENTIFIER, "Response handled by fallback handler."); + logger.info("Response handled by fallback handler."); res.statusCode = 404; res.end(); }); @@ -340,13 +339,13 @@ module.exports.getHttpWrapper = function () { }; module.exports.getServerConfig = function () { - console.log(`${LOG_IDENTIFIER} apihub.getServerConfig() method is deprecated, please use server.config to retrieve necessary info.`); + logger.warn(`apihub.getServerConfig() method is deprecated, please use server.config to retrieve necessary info.`); const config = require('./config'); return config.getConfig(); }; module.exports.getDomainConfig = function (domain, ...configKeys) { - console.log(`${LOG_IDENTIFIER} apihub.getServerConfig() method is deprecated, please use server.config.getDomainConfig(...) to retrieve necessary info.`); + logger.warn(`apihub.getServerConfig() method is deprecated, please use server.config.getDomainConfig(...) to retrieve necessary info.`); const config = require('./config'); return config.getDomainConfig(domain, ...configKeys); }; diff --git a/libs/Notifications.js b/libs/Notifications.js index 3c7084d..76559b1 100644 --- a/libs/Notifications.js +++ b/libs/Notifications.js @@ -4,6 +4,7 @@ function NotificationsManager(workingFolderPath, storageFolderPath) { const queues = {}; const subscribers = {}; const swarmUtils = require('swarmutils'); + const logger = $$.getLogger("NotificationsManager", "apihub/libs"); this.createQueue = function (queueName, callback) { if (typeof queues[queueName] !== "undefined") { @@ -143,7 +144,7 @@ function NotificationsManager(workingFolderPath, storageFolderPath) { try { require('fs').unlinkSync(notificationObject.filename); } catch (err) { - console.log(err); + logger.error(err); } } } diff --git a/libs/http-wrapper/src/classes/Client.js b/libs/http-wrapper/src/classes/Client.js index 412145e..46c0a5c 100644 --- a/libs/http-wrapper/src/classes/Client.js +++ b/libs/http-wrapper/src/classes/Client.js @@ -192,10 +192,12 @@ function parametersPreProcessing(params) { return res; } +const logger = $$.getLogger("Client", "apihub/http-wrapper"); + const handler = { get(target, propName) { if (!target[propName]) { - console.log(propName, "Not implemented!"); + logger.error(propName, "Not implemented!"); } else { return function () { const args = parametersPreProcessing(arguments); diff --git a/libs/http-wrapper/src/classes/Server.js b/libs/http-wrapper/src/classes/Server.js index b2dc27f..cc28030 100644 --- a/libs/http-wrapper/src/classes/Server.js +++ b/libs/http-wrapper/src/classes/Server.js @@ -85,7 +85,6 @@ function Server(sslOptions) { }); req.on('error', err => { - console.log("reject will be called. err :", err); return callback(err); }); diff --git a/middlewares/authorisation/index.js b/middlewares/authorisation/index.js index 9fb7479..f137ff9 100644 --- a/middlewares/authorisation/index.js +++ b/middlewares/authorisation/index.js @@ -4,7 +4,9 @@ const crypto = openDSU.loadApi("crypto"); const {sendUnauthorizedResponse} = require("../../utils/middlewares"); function Authorisation(server) { - console.log(`Registering Authorisation middleware`); + const logger = $$.getLogger("Authorisation", "apihub/authorisation"); + + logger.info(`Registering Authorisation middleware`); const config = require("../../config"); const skipJWTAuthorisation = config.getConfig("skipJWTAuthorisation"); diff --git a/middlewares/genericErrorMiddleware/index.js b/middlewares/genericErrorMiddleware/index.js index 48ea495..a49e8a6 100644 --- a/middlewares/genericErrorMiddleware/index.js +++ b/middlewares/genericErrorMiddleware/index.js @@ -1,5 +1,6 @@ function setupGenericErrorMiddleware(server) { const constants = require("./../../moduleConstants"); + const logger = $$.getLogger("setupGenericErrorMiddleware", "apihub/genericErrorMiddleware"); server.use(function (req, res, next) { const capturedWrites = []; @@ -9,7 +10,7 @@ function setupGenericErrorMiddleware(server) { res.write = function(chunk, encoding, callback){ if(typeof callback === "function" || typeof encoding === "function"){ - console.log(`${constants.LOG_IDENTIFIER}`, + logger.error(`${constants.LOG_IDENTIFIER}`, "Generic Error Middleware is running and has detected that a callback was used for response.write method call.", "Be aware that this middleware can generate undesired behaviour in this case.", new Error()); } @@ -39,7 +40,7 @@ function setupGenericErrorMiddleware(server) { next(); }); - console.log(`${constants.LOG_IDENTIFIER}`, "generic error middleware was loaded. This middleware will prevent any error to leak when sending a >=400 response to the client."); + logger.error(`${constants.LOG_IDENTIFIER}`, "generic error middleware was loaded. This middleware will prevent any error to leak when sending a >=400 response to the client."); } module.exports = setupGenericErrorMiddleware; diff --git a/middlewares/logger/index.js b/middlewares/logger/index.js index b3d4015..d707656 100644 --- a/middlewares/logger/index.js +++ b/middlewares/logger/index.js @@ -1,5 +1,6 @@ function Logger(server) { - console.log(`Registering Logger middleware`); + const logger = $$.getLogger("Logger", "apihub/logger"); + logger.info(`Registering Logger middleware`); const getRequestDuration = (start) => { const diff = process.hrtime(start); @@ -24,20 +25,20 @@ function Logger(server) { const { statusCode } = res; durationInMilliseconds = getRequestDuration(start); let log = `${remoteAddress}:${port} - [${datetime}] ${method}:${url} ${statusCode} ${durationInMilliseconds.toLocaleString()}ms`; - console.log(log); + logger.info(log); if(req.getLogs){ const visualIndex = "\t"; const requestLogs = req.getLogs(); if(requestLogs.length > 0){ - console.log("Request logs:"); + logger.info("Request logs:"); for(let i=0; i