Skip to content

Commit

Permalink
PharmaLedger-IMI/epi-workspace#918 using custom logger for apihub logs
Browse files Browse the repository at this point in the history
  • Loading branch information
skutner committed Sep 21, 2022
1 parent fa95e5b commit 76c0c1a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 19 deletions.
7 changes: 4 additions & 3 deletions components/bricking/controllers.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
});
}
Expand Down
3 changes: 2 additions & 1 deletion components/bricking/middlewares.js
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions components/bricking/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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 {
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/contracts/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions libs/http-wrapper/src/classes/MiddlewareRegistry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const querystring = require('querystring');
const logger = $$.getLogger("http-wrapper", "apihub/libs");

function matchUrl(pattern, url) {
const result = {
Expand Down Expand Up @@ -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");
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion libs/http-wrapper/src/httpUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const logger = $$.getLogger("http-wrapper", "apihub/libs");

function setDataHandler(request, callback) {
let bodyContent = '';

Expand All @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion utils/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const responseWrapper = require('../responseWrapper');
const logger = $$.getLogger("middlewares", "apihub/utils");

function requestBodyJSONMiddleware(request, response, next) {
/**
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 76c0c1a

Please # to comment.