Commit 0bc8d8f3 authored by tom's avatar tom

add logger for node

parent 12088540
...@@ -3,6 +3,7 @@ import type { NextApiRequest } from 'next'; ...@@ -3,6 +3,7 @@ import type { NextApiRequest } from 'next';
import type { RequestInit, Response } from 'node-fetch'; import type { RequestInit, Response } from 'node-fetch';
import nodeFetch from 'node-fetch'; import nodeFetch from 'node-fetch';
import { httpLogger } from 'lib/api/logger';
import * as cookies from 'lib/cookies'; import * as cookies from 'lib/cookies';
// first arg can be only a string // first arg can be only a string
...@@ -16,6 +17,12 @@ export default function fetchFactory(_req: NextApiRequest) { ...@@ -16,6 +17,12 @@ export default function fetchFactory(_req: NextApiRequest) {
}; };
const url = new URL(path, appConfig.apiUrl); const url = new URL(path, appConfig.apiUrl);
httpLogger.logger.info({
message: 'Trying to call API',
url,
req: _req,
});
return nodeFetch(url.toString(), { return nodeFetch(url.toString(), {
headers, headers,
...init, ...init,
......
...@@ -2,11 +2,14 @@ import type { NextApiRequest, NextApiResponse } from 'next'; ...@@ -2,11 +2,14 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import fetchFactory from 'lib/api/fetch'; import fetchFactory from 'lib/api/fetch';
import getUrlWithNetwork from 'lib/api/getUrlWithNetwork'; import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
import { httpLogger } from 'lib/api/logger';
type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE'; type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
export default function createHandler(getUrl: (_req: NextApiRequest) => string, allowedMethods: Array<Methods>) { export default function createHandler(getUrl: (_req: NextApiRequest) => string, allowedMethods: Array<Methods>) {
const handler = async(_req: NextApiRequest, res: NextApiResponse) => { const handler = async(_req: NextApiRequest, res: NextApiResponse) => {
httpLogger(_req, res);
if (!_req.method || !allowedMethods.includes(_req.method as Methods)) { if (!_req.method || !allowedMethods.includes(_req.method as Methods)) {
res.setHeader('Allow', allowedMethods); res.setHeader('Allow', allowedMethods);
res.status(405).end(`Method ${ _req.method } Not Allowed`); res.status(405).end(`Method ${ _req.method } Not Allowed`);
...@@ -38,6 +41,8 @@ export default function createHandler(getUrl: (_req: NextApiRequest) => string, ...@@ -38,6 +41,8 @@ export default function createHandler(getUrl: (_req: NextApiRequest) => string,
responseError = defaultError; responseError = defaultError;
} }
httpLogger.logger.error({ err: responseError, url: _req.url });
res.status(500).json(responseError); res.status(500).json(responseError);
}; };
......
import pino from 'pino-http';
export const httpLogger = pino();
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
}, },
"scripts": { "scripts": {
"dev": "next dev", "dev": "next dev",
"dev:poa_core": "./node_modules/.bin/dotenv -e ./configs/envs/.env.poa_core -e ./configs/envs/.env.common -e ./configs/envs/.env.secrets next dev", "dev:poa_core": "./node_modules/.bin/dotenv -e ./configs/envs/.env.poa_core -e ./configs/envs/.env.common -e ./configs/envs/.env.secrets next dev | ./node_modules/.bin/pino-pretty",
"build": "next build", "build": "next build",
"build:vercel": "./node_modules/.bin/dotenv -e ./configs/envs/.env.poa_core -e ./configs/envs/.env.common next build", "build:vercel": "./node_modules/.bin/dotenv -e ./configs/envs/.env.poa_core -e ./configs/envs/.env.common next build",
"build:docker": "docker build --build-arg GIT_COMMIT_SHA=$(git rev-parse HEAD) -t blockscout ./", "build:docker": "docker build --build-arg GIT_COMMIT_SHA=$(git rev-parse HEAD) -t blockscout ./",
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
"next": "12.2.5", "next": "12.2.5",
"next-react-svg": "1.1.3", "next-react-svg": "1.1.3",
"node-fetch": "^3.2.9", "node-fetch": "^3.2.9",
"pino-http": "^8.2.1",
"pino-pretty": "^9.1.1",
"react": "18.1.0", "react": "18.1.0",
"react-dom": "18.1.0", "react-dom": "18.1.0",
"react-hook-form": "^7.33.1", "react-hook-form": "^7.33.1",
......
...@@ -2,8 +2,11 @@ import type { NextApiRequest, NextApiResponse } from 'next'; ...@@ -2,8 +2,11 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import fetchFactory from 'lib/api/fetch'; import fetchFactory from 'lib/api/fetch';
import getUrlWithNetwork from 'lib/api/getUrlWithNetwork'; import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
import { httpLogger } from 'lib/api/logger';
export default async function csrfHandler(_req: NextApiRequest, res: NextApiResponse) { export default async function csrfHandler(_req: NextApiRequest, res: NextApiResponse) {
httpLogger(_req, res);
const url = getUrlWithNetwork(_req, `api/account/v1/get_csrf`); const url = getUrlWithNetwork(_req, `api/account/v1/get_csrf`);
const fetch = fetchFactory(_req); const fetch = fetchFactory(_req);
const response = await fetch(url); const response = await fetch(url);
...@@ -14,5 +17,8 @@ export default async function csrfHandler(_req: NextApiRequest, res: NextApiResp ...@@ -14,5 +17,8 @@ export default async function csrfHandler(_req: NextApiRequest, res: NextApiResp
return; return;
} }
res.status(500).json({ statusText: response.statusText, status: response.status }); const responseError = { statusText: response.statusText, status: response.status };
httpLogger.logger.error({ err: responseError, url: _req.url });
res.status(500).json(responseError);
} }
...@@ -6,8 +6,11 @@ import type { TWatchlistItem } from 'types/client/account'; ...@@ -6,8 +6,11 @@ import type { TWatchlistItem } from 'types/client/account';
import fetchFactory from 'lib/api/fetch'; import fetchFactory from 'lib/api/fetch';
import getUrlWithNetwork from 'lib/api/getUrlWithNetwork'; import getUrlWithNetwork from 'lib/api/getUrlWithNetwork';
import { httpLogger } from 'lib/api/logger';
const watchlistWithTokensHandler = async(_req: NextApiRequest, res: NextApiResponse<Array<TWatchlistItem>>) => { const watchlistWithTokensHandler = async(_req: NextApiRequest, res: NextApiResponse<Array<TWatchlistItem>>) => {
httpLogger(_req, res);
const fetch = fetchFactory(_req); const fetch = fetchFactory(_req);
const url = getUrlWithNetwork(_req, 'api/account/v1/user/watchlist'); const url = getUrlWithNetwork(_req, 'api/account/v1/user/watchlist');
const watchlistResponse = await fetch(url, { method: 'GET' }); const watchlistResponse = await fetch(url, { method: 'GET' });
...@@ -15,6 +18,7 @@ const watchlistWithTokensHandler = async(_req: NextApiRequest, res: NextApiRespo ...@@ -15,6 +18,7 @@ const watchlistWithTokensHandler = async(_req: NextApiRequest, res: NextApiRespo
const watchlistData = await watchlistResponse.json() as WatchlistAddresses; const watchlistData = await watchlistResponse.json() as WatchlistAddresses;
if (watchlistResponse.status !== 200) { if (watchlistResponse.status !== 200) {
httpLogger.logger.error({ err: { statusText: 'Watchlist token error', status: 500 }, url: _req.url });
res.status(500).end(watchlistData || 'Unknown error'); res.status(500).end(watchlistData || 'Unknown error');
return; return;
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment