Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use updated logger package (DX-177) #101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@types/http-errors": "^1.8.2",
"@voiceflow/logger": "1.6.1",
"@voiceflow/logger": "2.0.0",
"@voiceflow/verror": "^1.1.0",
"chai": "^4.3.4",
"dotenv": "^10.0.0",
Expand Down
14 changes: 11 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import Logger from '@voiceflow/logger';
import { createLogger, LogFormat, Logger, LoggerOptions, LogLevel } from '@voiceflow/logger';

const options = ['local', 'test'].includes(process.env.NODE_ENV!) ? { level: 'info' as any, pretty: true } : {};
const options: LoggerOptions = ['local', 'test'].includes(process.env.NODE_ENV!)
? {
level: LogLevel.INFO,
format: LogFormat.INLINE,
}
: {
level: LogLevel.WARN,
format: LogFormat.JSON,
};

const log = new Logger(options);
const log: Logger = createLogger(options);

export default log;
4 changes: 2 additions & 2 deletions src/middlewares/exception/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ExceptionMiddleware extends AbstractMiddleware<never, never> {
}

public handleError(err: unknown, req: Request, res: Response, _next: NextFunction): void {
log.error(`Exception formatter (pre) ${JSON.stringify(err)}`);
log.error(err, 'Exception formatter (pre)');

const { statusCode, ...body } = formatError(err);

Expand All @@ -19,7 +19,7 @@ export class ExceptionMiddleware extends AbstractMiddleware<never, never> {
requestID: req.id.toString(),
};

log.error(`Exception formatter (post) ${JSON.stringify(error)}`);
log.error(error, 'Exception formatter (post)');

res.status(statusCode).send(error);
}
Expand Down
10 changes: 4 additions & 6 deletions src/responseBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ResponseBuilder {
return error.code;
}

log.warn(`Unexpected error type: '${error.name}' from '${error.stack}'`);
log.warn(error, 'Unexpected error type');

return VError.HTTP_STATUS.INTERNAL_SERVER_ERROR;
}
Expand Down Expand Up @@ -89,14 +89,13 @@ class ResponseBuilder {
* @param codeOverride optionally override the code specified in the error
* @param req request object
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
private static errorResponse<T>(
error: (Error & { data?: T }) | string,
codeOverride?: HttpStatus,
req?: Request & { user?: { id: number } }
): ErrorResponse<T> {
if (error && (error as any).isAxiosError) {
log.error(`@backend-utils:errorResponse - error:axios:${JSON.stringify(ResponseBuilder.getAxiosError(error as AxiosError))}`);
log.error(ResponseBuilder.getAxiosError(error as AxiosError), '@backend-utils:errorResponse - error:axios');
}

if (!(error instanceof Error)) {
Expand Down Expand Up @@ -127,10 +126,9 @@ class ResponseBuilder {

if (response.code >= 500) {
log.error(
error,
// eslint-disable-next-line sonarjs/no-nested-template-literals
`500+ error: ${req?.originalUrl} ${req?.user ? ` User ID: ${req?.user.id}` : ''} ${error.stack} ${
error.data ? JSON.stringify(error.data) : ''
}`
`500+ error: ${req?.originalUrl} ${req?.user ? ` User ID: ${req?.user.id}` : ''}`
);
}

Expand Down
Loading