Skip to content

Commit

Permalink
Merge pull request #100 from commercetools/johnsonogwuru/improve-logging
Browse files Browse the repository at this point in the history
chore(logger): improve logging in processor
  • Loading branch information
leungkinghin-ct authored Apr 18, 2024
2 parents 0a2e685 + e77c9b9 commit 21e21f8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
24 changes: 15 additions & 9 deletions processor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"license": "ISC",
"dependencies": {
"@commercetools-backend/loggers": "22.23.3",
"@commercetools/connect-payments-sdk": "0.4.4",
"@commercetools/connect-payments-sdk": "0.5.0",
"@fastify/autoload": "5.8.0",
"@fastify/cors": "9.0.1",
"@fastify/formbody": "7.4.0",
Expand Down
16 changes: 15 additions & 1 deletion processor/src/libs/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import { createApplicationLogger } from '@commercetools-backend/loggers';
import { defaultFieldsFormatter } from '@commercetools/connect-payments-sdk';
import { config } from '../../config/config';
import { getRequestContext } from '../fastify/context/context';

export const log = createApplicationLogger();
export const log = createApplicationLogger({
formatters: [
defaultFieldsFormatter({
projectKey: config.projectKey,
version: process.env.npm_package_version,
name: process.env.npm_package_name,
correlationId: () => getRequestContext().correlationId,
pathTemplate: () => getRequestContext().pathTemplate,
path: () => getRequestContext().path,
}),
],
});
8 changes: 4 additions & 4 deletions processor/src/payment-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { log } from './libs/logger/index';

export class AppLogger implements Logger {
public debug = (obj: object, message: string) => {
log.debug(obj ? obj : message);
log.debug(message, obj || undefined);
};
public info = (obj: object, message: string) => {
log.info(obj ? obj : message);
log.info(message, obj || undefined);
};
public warn = (obj: object, message: string) => {
log.warn(obj ? obj : message);
log.warn(message, obj || undefined);
};
public error = (obj: object, message: string) => {
log.error(obj ? obj : message);
log.error(message, obj || undefined);
};
}

Expand Down
6 changes: 6 additions & 0 deletions processor/test/routes/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ContextProvider,
JWTAuthenticationHook,
JWTAuthenticationManager,
Logger,
Oauth2AuthenticationHook,
Oauth2AuthenticationManager,
RequestContextData,
Expand All @@ -23,6 +24,7 @@ describe('/operations APIs', () => {
const token = 'token';
const jwtToken = 'jwtToken';
const sessionId = 'session-id';
const logger = jest.fn() as unknown as Logger;

const spyAuthenticateJWT = jest
.spyOn(JWTAuthenticationHook.prototype, 'authenticate')
Expand All @@ -45,21 +47,25 @@ describe('/operations APIs', () => {
const spiedJwtAuthenticationHook = new JWTAuthenticationHook({
authenticationManager: jest.fn() as unknown as JWTAuthenticationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
logger,
});

const spiedOauth2AuthenticationHook = new Oauth2AuthenticationHook({
authenticationManager: jest.fn() as unknown as Oauth2AuthenticationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
logger,
});

const spiedSessionHeaderAuthenticationHook = new SessionHeaderAuthenticationHook({
authenticationManager: jest.fn() as unknown as SessionHeaderAuthenticationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
logger,
});

const spiedAuthorityAuthorizationHook = new AuthorityAuthorizationHook({
authorizationManager: jest.fn() as unknown as AuthorityAuthorizationManager,
contextProvider: jest.fn() as unknown as ContextProvider<RequestContextData>,
logger,
});

const spiedPaymentService = new PaypalPaymentService({
Expand Down

0 comments on commit 21e21f8

Please sign in to comment.