From 76c0c1a88ca07b39a09d7d1beb8e805126f0606e Mon Sep 17 00:00:00 2001 From: skutner Date: Wed, 21 Sep 2022 11:52:16 +0300 Subject: [PATCH] PharmaLedger-IMI/epi-workspace#918 using custom logger for apihub logs --- components/bricking/controllers.js | 7 ++++--- components/bricking/middlewares.js | 3 ++- components/bricking/utils.js | 8 ++++---- components/contracts/boot.js | 2 +- index.js | 7 +++---- libs/http-wrapper/src/classes/MiddlewareRegistry.js | 9 +++++---- libs/http-wrapper/src/httpUtils.js | 4 +++- utils/middlewares/index.js | 3 ++- 8 files changed, 24 insertions(+), 19 deletions(-) diff --git a/components/bricking/controllers.js b/components/bricking/controllers.js index 8f20ebb..40bde98 100644 --- a/components/bricking/controllers.js +++ b/components/bricking/controllers.js @@ -1,4 +1,5 @@ const { getBrickWithExternalProvidersFallbackAsync } = require("./utils"); +const logger = $$.getLogger("bricking", "apihub/bricking"); async function getBrick(request, response) { response.setHeader("content-type", "application/octet-stream"); @@ -18,13 +19,13 @@ function putBrick(request, response) { const utils = require("./utils"); utils.convertReadableStreamToBuffer(request, (error, brickData) => { if (error) { - console.error("[Bricking] Fail to convert Stream to Buffer!", error.message); + logger.error("[Bricking] Fail to convert Stream to Buffer!", error.message); return response.send(500); } request.fsBrickStorage.addBrick(brickData, (error, brickHash) => { if (error) { - console.error("[Bricking] Fail to manage current brick!", error.message); + logger.error("[Bricking] Fail to manage current brick!", error.message); return response.send(error.code === "EACCES" ? 409 : 500); } @@ -53,7 +54,7 @@ function downloadMultipleBricks(request, response) { return response.send(200, data); }) .catch((error) => { - console.error("[Bricking] Fail to get multiple bricks!", error.message); + logger.error("[Bricking] Fail to get multiple bricks!", error.message); return response.send(500); }); } diff --git a/components/bricking/middlewares.js b/components/bricking/middlewares.js index e340854..feaf8be 100644 --- a/components/bricking/middlewares.js +++ b/components/bricking/middlewares.js @@ -1,10 +1,11 @@ async function requestFSBrickStorageMiddleware(request, response, next) { const { domain: domainName } = request.params; + const logger = $$.getLogger("requestFSBrickStorageMiddleware", "apihub/bricking"); const domainConfig = await require("./utils").getBricksDomainConfig(domainName); if (!domainConfig || !domainConfig.path) { const message = `[Bricking] Domain '${domainName}' not found!`; - console.error(message); + logger.error(message); return response.send(404, message); } diff --git a/components/bricking/utils.js b/components/bricking/utils.js index 111f376..4e76d70 100644 --- a/components/bricking/utils.js +++ b/components/bricking/utils.js @@ -46,7 +46,7 @@ async function getBrickFromExternalProvidersAsync(request, domain, hashLink) { providerResponse = await providerResponse.text(); return providerResponse; } catch (error) { - // console.warn(`[Bricking] Failed to get brick ${hashLink} from ${providerUrl}!`, error); + // logger.warn(`[Bricking] Failed to get brick ${hashLink} from ${providerUrl}!`, error); } } @@ -60,7 +60,7 @@ async function getBrickWithExternalProvidersFallbackAsync(request, domain, hashL return brick; } } catch (error) { - console.warn(`[Bricking] Brick ${hashLink} not found. Trying to fallback to other providers...`); + logger.warn(`[Bricking] Brick ${hashLink} not found. Trying to fallback to other providers...`); } try { @@ -73,13 +73,13 @@ async function getBrickWithExternalProvidersFallbackAsync(request, domain, hashL await fsBrickStorage.addBrickAsync(externalBrick); console.info(`[Bricking] Saved external brick ${hashLink} to own storage`); } catch (error) { - console.warn("[Bricking] Fail to manage external brick saving!", error); + logger.warn("[Bricking] Fail to manage external brick saving!", error); } }); return externalBrick; } catch (error) { - console.warn(`[Bricking] Error while trying to get missing brick from fallback providers!`, error); + logger.warn(`[Bricking] Error while trying to get missing brick from fallback providers!`, error); throw error; } } diff --git a/components/contracts/boot.js b/components/contracts/boot.js index 4342d5a..cddc872 100644 --- a/components/contracts/boot.js +++ b/components/contracts/boot.js @@ -87,7 +87,7 @@ async function boot(validatorDID, serverUrl, domain, domainConfig, rootFolder, s } process.on("uncaughtException", (err) => { - console.error(`${logPrefix} unchaughtException inside worker`, err); + logger.error(`${logPrefix} unchaughtException inside worker`, err); setTimeout(() => { process.exit(1); }, 100); diff --git a/index.js b/index.js index 90bf64d..eac7928 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,15 @@ +const logger = $$.getLogger("HttpServer", "apihub"); + process.on('uncaughtException', err => { - console.error('There was an uncaught error', err); + logger.error('There was an uncaught error', err); }); -const {LOG_IDENTIFIER} = require("./moduleConstants"); - const httpWrapper = require('./libs/http-wrapper'); 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 diff --git a/libs/http-wrapper/src/classes/MiddlewareRegistry.js b/libs/http-wrapper/src/classes/MiddlewareRegistry.js index b5b6813..91aa729 100644 --- a/libs/http-wrapper/src/classes/MiddlewareRegistry.js +++ b/libs/http-wrapper/src/classes/MiddlewareRegistry.js @@ -1,4 +1,5 @@ const querystring = require('querystring'); +const logger = $$.getLogger("http-wrapper", "apihub/libs"); function matchUrl(pattern, url) { const result = { @@ -113,7 +114,7 @@ function MiddlewareRegistry() { try { execute(0, req.method.toLowerCase(), req.url, req, res); } catch (e) { - console.error(e); + logger.error(e); res.statusCode = 500; res.end("Internal server error"); } @@ -129,7 +130,7 @@ function MiddlewareRegistry() { function execute(index, method, url, ...params) { if (!registeredMiddlewareFunctions[index]) { if(index===0){ - console.error("No handlers registered yet!"); + logger.error("No handlers registered yet!"); } return; } @@ -162,12 +163,12 @@ function MiddlewareRegistry() { fn(...params, (err) => { counter++; if (counter > 1) { - console.warn('You called next multiple times, only the first one will be executed'); + logger.warn('You called next multiple times, only the first one will be executed'); return; } if (err) { - console.error(err); + logger.error(err); return; } diff --git a/libs/http-wrapper/src/httpUtils.js b/libs/http-wrapper/src/httpUtils.js index 25b2a8b..26a1f6a 100644 --- a/libs/http-wrapper/src/httpUtils.js +++ b/libs/http-wrapper/src/httpUtils.js @@ -1,3 +1,5 @@ +const logger = $$.getLogger("http-wrapper", "apihub/libs"); + function setDataHandler(request, callback) { let bodyContent = ''; @@ -24,7 +26,7 @@ function setDataHandlerMiddleware(request, response, next) { } function sendErrorResponse(error, response, statusCode) { - console.error(error); + logger.error(error); response.statusCode = statusCode; response.end(); } diff --git a/utils/middlewares/index.js b/utils/middlewares/index.js index 0cc690a..f3f9ee6 100644 --- a/utils/middlewares/index.js +++ b/utils/middlewares/index.js @@ -1,4 +1,5 @@ const responseWrapper = require('../responseWrapper'); +const logger = $$.getLogger("middlewares", "apihub/utils"); function requestBodyJSONMiddleware(request, response, next) { /** @@ -73,7 +74,7 @@ function bodyReaderMiddleware(req, res, next) { } function sendUnauthorizedResponse(req, res, reason, error) { - console.error(`[Auth] [${req.method}] ${req.url} blocked: ${reason}`, error); + logger.error(`[Auth] [${req.method}] ${req.url} blocked: ${reason}`, error); res.statusCode = 403; res.end(); }