Skip to content

Commit

Permalink
Merge pull request #913 from superfastcms/fix/logger-http
Browse files Browse the repository at this point in the history
fix: upgrading pinoHttp
  • Loading branch information
kazaneshimizu authored Aug 22, 2023
2 parents 189a50b + 9f20bcf commit 8b536b8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"notistack": "^3.0.1",
"ora": "^7.0.1",
"pino": "^8.11.0",
"pino-http": "^8.3.3",
"pino-http": "^8.4.0",
"pino-pretty": "^10.0.0",
"react": "^18.2.0",
"react-device-detect": "^2.2.3",
Expand Down
2 changes: 1 addition & 1 deletion src/api/router/apiRouter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cookieParser from 'cookie-parser';
import express from 'express';
import { env } from '../../env.js';
import { expressLogger } from '../../utilities/logger.js';
import { expressLogger } from '../../utilities/expressLogger.js';
import { authentications } from '../controllers/authentications.js';
import { collections } from '../controllers/collections.js';
import { contents } from '../controllers/contents.js';
Expand Down
43 changes: 43 additions & 0 deletions src/utilities/expressLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Request, RequestHandler } from 'express';
import pino, { LoggerOptions } from 'pino';
import { pinoHttp, stdSerializers } from 'pino-http';

const httpLoggerOptions: LoggerOptions = {
level: process.env.PUBLIC_LOG_LEVEL || 'info',
redact: {
paths: ['req.headers.authorization', 'req.headers.cookie'],
censor: '--redacted--',
},
transport: {
target: 'pino-pretty',
options: {
ignore: 'hostname,pid',
hideObject: process.env.PUBLIC_LOG_HIDE_OBJECT || true,
sync: true,
translateTime: 'SYS:HH:MM:ss',
messageFormat: '{req.method} {req.url} {res.statusCode} {responseTime}ms',
},
},
};

const redactQuery = (originalPath: string) => {
const url = new URL(originalPath, 'http://example.com/');

// Access token contained in request parameters.
if (url.searchParams.has('access_token')) {
url.searchParams.set('access_token', '--redacted--');
}

return url.pathname + url.search;
};

export const expressLogger = pinoHttp({
logger: pino(httpLoggerOptions),
serializers: {
req(request: Request) {
const output = stdSerializers.req(request);
output.url = redactQuery(output.url);
return output;
},
},
}) as RequestHandler;
41 changes: 0 additions & 41 deletions src/utilities/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Request, RequestHandler } from 'express';
import pino, { LoggerOptions } from 'pino';
import pinoHTTP, { stdSerializers } from 'pino-http';

const pinoOptions: LoggerOptions = {
level: process.env.PUBLIC_LOG_LEVEL || 'info',
Expand All @@ -9,44 +7,5 @@ const pinoOptions: LoggerOptions = {
censor: '--redacted--',
},
};
const httpLoggerOptions: LoggerOptions = {
level: process.env.PUBLIC_LOG_LEVEL || 'info',
redact: {
paths: ['req.headers.authorization', 'req.headers.cookie'],
censor: '--redacted--',
},
transport: {
target: 'pino-pretty',
options: {
ignore: 'hostname,pid',
hideObject: process.env.PUBLIC_LOG_HIDE_OBJECT || true,
sync: true,
translateTime: 'SYS:HH:MM:ss',
messageFormat: '{req.method} {req.url} {res.statusCode} {responseTime}ms',
},
},
};

const redactQuery = (originalPath: string) => {
const url = new URL(originalPath, 'http://example.com/');

// Access token contained in request parameters.
if (url.searchParams.has('access_token')) {
url.searchParams.set('access_token', '--redacted--');
}

return url.pathname + url.search;
};

export const expressLogger = pinoHTTP({
logger: pino(httpLoggerOptions),
serializers: {
req(request: Request) {
const output = stdSerializers.req(request);
output.url = redactQuery(output.url);
return output;
},
},
}) as RequestHandler;

export const logger = pino(pinoOptions);
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8651,10 +8651,10 @@ pino-abstract-transport@^1.0.0, [email protected]:
readable-stream "^4.0.0"
split2 "^4.0.0"

pino-http@^8.3.3:
version "8.3.3"
resolved "https://registry.npmjs.org/pino-http/-/pino-http-8.3.3.tgz"
integrity sha512-p4umsNIXXVu95HD2C8wie/vXH7db5iGRpc+yj1/ZQ3sRtTQLXNjoS6Be5+eI+rQbqCRxen/7k/KSN+qiZubGDw==
pino-http@^8.4.0:
version "8.4.0"
resolved "https://registry.yarnpkg.com/pino-http/-/pino-http-8.4.0.tgz#ebfd601f7244d7bd9ead578843025505da4e3f56"
integrity sha512-9I1eRLxsujQJwLQTrHBU0wDlwnry2HzV2TlDwAsmZ9nT3Y2NQBLrz+DYp73L4i11vl/eudnFT8Eg0Kp62tMwEw==
dependencies:
get-caller-file "^2.0.5"
pino "^8.0.0"
Expand Down

1 comment on commit 8b536b8

@vercel
Copy link

@vercel vercel bot commented on 8b536b8 Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

superfast – ./

superfast-superfast.vercel.app
superfastcms.vercel.app
superfast-git-main-superfast.vercel.app

Please sign in to comment.