Skip to content

Commit 0422b27

Browse files
refactor(api-service): logger to pinologger (#8128)
1 parent 0d8a84d commit 0422b27

File tree

57 files changed

+431
-277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+431
-277
lines changed

apps/api/migrations/encrypt-api-keys/encrypt-api-keys-migration.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { createHash } from 'crypto';
88
import { NestFactory } from '@nestjs/core';
99

1010
import { AppModule } from '../../src/app.module';
11+
import { getLogger } from '../../src/app/shared/services/logger.service';
12+
13+
const logger = getLogger('EncryptApiKeysMigration');
1114

1215
export async function encryptApiKeysMigration() {
13-
// eslint-disable-next-line no-console
14-
console.log('start migration - encrypt api keys');
16+
logger.info('start migration - encrypt api keys');
1517

1618
const app = await NestFactory.create(AppModule, {
1719
logger: false,
@@ -20,12 +22,10 @@ export async function encryptApiKeysMigration() {
2022
const environments = await environmentRepository.find({});
2123

2224
for (const environment of environments) {
23-
// eslint-disable-next-line no-console
24-
console.log(`environment ${environment._id}`);
25+
logger.info(`environment ${environment._id}`);
2526

2627
if (!environment.apiKeys) {
27-
// eslint-disable-next-line no-console
28-
console.log(`environment ${environment._id} - is not contains api keys, skipping..`);
28+
logger.info(`environment ${environment._id} - is not contains api keys, skipping..`);
2929
continue;
3030
}
3131

@@ -34,8 +34,7 @@ export async function encryptApiKeysMigration() {
3434
isEncrypted(key.key);
3535
})
3636
) {
37-
// eslint-disable-next-line no-console
38-
console.log(`environment ${environment._id} - api keys are already encrypted, skipping..`);
37+
logger.info(`environment ${environment._id} - api keys are already encrypted, skipping..`);
3938
continue;
4039
}
4140

@@ -47,11 +46,11 @@ export async function encryptApiKeysMigration() {
4746
$set: { apiKeys: updatePayload },
4847
}
4948
);
50-
// eslint-disable-next-line no-console
51-
console.log(`environment ${environment._id} - api keys updated`);
49+
50+
logger.info(`environment ${environment._id} - api keys updated`);
5251
}
53-
// eslint-disable-next-line no-console
54-
console.log('end migration');
52+
53+
logger.info('end migration');
5554
}
5655

5756
export function encryptApiKeysWithGuard(apiKeys: IApiKey[]): IEncryptedApiKey[] {

apps/api/migrations/encrypt-credentials/encrypt-credentials-migration.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import { IntegrationEntity } from '@novu/dal';
2-
import { IntegrationRepository } from '@novu/dal';
1+
import { IntegrationEntity, IntegrationRepository } from '@novu/dal';
32
import { ICredentialsDto, secureCredentials } from '@novu/shared';
43
import { encryptSecret } from '@novu/application-generic';
4+
import { getLogger } from '../../src/app/shared/services/logger.service';
5+
6+
const logger = getLogger('EncryptCredentialsMigration');
57

68
export async function encryptOldCredentialsMigration() {
7-
// eslint-disable-next-line no-console
8-
console.log('start migration - encrypt credentials');
9+
logger.info('start migration - encrypt credentials');
910

1011
const integrationRepository = new IntegrationRepository();
1112
const integrations = await integrationRepository.find({} as any);
1213

1314
for (const integration of integrations) {
14-
// eslint-disable-next-line no-console
15-
console.log(`integration ${integration._id}`);
15+
logger.info(`integration ${integration._id}`);
1616

1717
const updatePayload: Partial<IntegrationEntity> = {};
1818

1919
if (!integration.credentials) {
20-
// eslint-disable-next-line no-console
21-
console.log(`integration ${integration._id} - is not contains credentials, skipping..`);
20+
logger.info(`integration ${integration._id} - is not contains credentials, skipping..`);
2221
continue;
2322
}
2423

@@ -30,16 +29,14 @@ export async function encryptOldCredentialsMigration() {
3029
$set: updatePayload,
3130
}
3231
);
33-
// eslint-disable-next-line no-console
34-
console.log(`integration ${integration._id} - credentials updated`);
32+
logger.info(`integration ${integration._id} - credentials updated`);
3533
}
36-
// eslint-disable-next-line no-console
37-
console.log('end migration');
34+
logger.info('end migration');
3835
}
3936

4037
export function encryptCredentialsWithGuard(integration: IntegrationEntity): ICredentialsDto {
4138
const encryptedCredentials: ICredentialsDto = {};
42-
const credentials = integration.credentials;
39+
const { credentials } = integration;
4340

4441
for (const key in credentials) {
4542
const credential = credentials[key];
@@ -66,8 +63,7 @@ function alreadyEncrypted(credential: string, integration: IntegrationEntity, cr
6663
const encrypted = credential.includes('nvsk.');
6764

6865
if (encrypted) {
69-
// eslint-disable-next-line no-console
70-
console.log(`integration ${integration._id} - credential ${credentialKey} is already updated`);
66+
logger.info(`integration ${integration._id} - credential ${credentialKey} is already updated`);
7167
}
7268

7369
return encrypted;

apps/api/migrations/secure-to-boolean/secure-to-boolean-migration.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import '../../src/config';
22
import { IntegrationRepository } from '@novu/dal';
33
import { NestFactory } from '@nestjs/core';
4-
import { Logger } from '@nestjs/common';
54
import { AppModule } from '../../src/app.module';
5+
import { getLogger } from '../../src/app/shared/services/logger.service';
66

77
const integrationRepository = new IntegrationRepository();
8-
const MIGRATION_CONTEXT = 'Migration';
8+
9+
const logger = getLogger('SecureToBooleanMigration');
910

1011
export async function run() {
1112
const app = await NestFactory.create(AppModule);
1213

13-
Logger.log('Start migration - update credentials.secure from string to boolean', MIGRATION_CONTEXT);
14+
logger.info('Start migration - update credentials.secure from string to boolean');
1415

15-
Logger.log('Updating from "true" to true...', MIGRATION_CONTEXT);
16+
logger.info('Updating from "true" to true...');
1617
const resultTrue = await updateTrueValues();
17-
Logger.log(`Matched: ${resultTrue.matchedCount} Modified: ${resultTrue.modifiedCount} \n`, MIGRATION_CONTEXT);
18+
logger.info(`Matched: ${resultTrue.matchedCount} Modified: ${resultTrue.modifiedCount} \n`);
1819

19-
Logger.log('Updating from "false" to false...', MIGRATION_CONTEXT);
20+
logger.info('Updating from "false" to false...');
2021
const resultFalse = await updateFalseValues();
21-
Logger.log(`Matched: ${resultFalse.matchedCount} Modified: ${resultFalse.modifiedCount} \n`, MIGRATION_CONTEXT);
22+
logger.info(`Matched: ${resultFalse.matchedCount} Modified: ${resultFalse.modifiedCount} \n`);
2223

23-
Logger.log('End migration.\n', MIGRATION_CONTEXT);
24+
logger.info('End migration.\n');
2425
await app.close();
2526
}
2627

apps/api/src/app.module.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable global-require */
2-
import { DynamicModule, Logger, Module, Provider } from '@nestjs/common';
2+
import { DynamicModule, Module, Provider } from '@nestjs/common';
33
import { APP_INTERCEPTOR } from '@nestjs/core';
44
import { cacheService, TracingModule } from '@novu/application-generic';
55
import { Client, NovuModule } from '@novu/framework/nest';
@@ -175,8 +175,4 @@ modules.push(
175175
controllers: [],
176176
providers,
177177
})
178-
export class AppModule {
179-
constructor() {
180-
Logger.log(`BOOTSTRAPPED NEST APPLICATION`);
181-
}
182-
}
178+
export class AppModule {}

apps/api/src/app/analytics/usecases/hubspot-identify-form/hubspot-identify-form.usecase.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { HttpService } from '@nestjs/axios';
2-
import { Injectable, Logger } from '@nestjs/common';
2+
import { Injectable } from '@nestjs/common';
33
import { AxiosError } from 'axios';
4+
import { PinoLogger } from '@novu/application-generic';
45
import { HubspotIdentifyFormCommand } from './hubspot-identify-form.command';
56

67
const LOG_CONTEXT = 'HubspotIdentifyFormUsecase';
@@ -10,7 +11,12 @@ export class HubspotIdentifyFormUsecase {
1011
private readonly hubspotPortalId = '44416662';
1112
private readonly hubspotFormId = 'fc39aa98-4285-4322-9514-52da978baae8';
1213

13-
constructor(private httpService: HttpService) {}
14+
constructor(
15+
private httpService: HttpService,
16+
private logger: PinoLogger
17+
) {
18+
this.logger.setContext(this.constructor.name);
19+
}
1420

1521
async execute(command: HubspotIdentifyFormCommand) {
1622
try {
@@ -30,10 +36,10 @@ export class HubspotIdentifyFormUsecase {
3036
},
3137
};
3238

33-
await this.httpService.post(hubspotSubmitUrl, hubspotData);
39+
this.httpService.post(hubspotSubmitUrl, hubspotData);
3440
} catch (error) {
3541
if (error instanceof AxiosError) {
36-
Logger.error(
42+
this.logger.error(
3743
`Failed to submit to Hubspot message=${error.message}, status=${error.status}`,
3844
{
3945
organizationId: command.organizationId,

apps/api/src/app/auth/auth.controller.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
Header,
88
HttpCode,
99
HttpStatus,
10-
Logger,
1110
NotFoundException,
1211
Param,
1312
Post,
@@ -21,7 +20,7 @@ import { MemberEntity, MemberRepository, UserRepository } from '@novu/dal';
2120
import { AuthGuard } from '@nestjs/passport';
2221
import { PasswordResetFlowEnum, UserSessionData } from '@novu/shared';
2322
import { ApiExcludeController, ApiTags } from '@nestjs/swagger';
24-
import { buildOauthRedirectUrl } from '@novu/application-generic';
23+
import { buildOauthRedirectUrl, PinoLogger } from '@novu/application-generic';
2524
import { UserRegistrationBodyDto } from './dtos/user-registration.dto';
2625
import { UserRegister } from './usecases/register/user-register.usecase';
2726
import { UserRegisterCommand } from './usecases/register/user-register.command';
@@ -56,25 +55,27 @@ export class AuthController {
5655
private authService: AuthService,
5756
private userRegisterUsecase: UserRegister,
5857
private loginUsecase: Login,
59-
private switchEnvironmentUsecase: SwitchEnvironment,
6058
private switchOrganizationUsecase: SwitchOrganization,
6159
private memberRepository: MemberRepository,
6260
private passwordResetRequestUsecase: PasswordResetRequest,
6361
private passwordResetUsecase: PasswordReset,
64-
private updatePasswordUsecase: UpdatePassword
65-
) {}
62+
private updatePasswordUsecase: UpdatePassword,
63+
private logger: PinoLogger
64+
) {
65+
this.logger.setContext(this.constructor.name);
66+
}
6667

6768
@Get('/github')
6869
githubAuth() {
69-
Logger.verbose('Checking Github Auth');
70+
this.logger.trace('Checking Github Auth');
7071

7172
if (!process.env.GITHUB_OAUTH_CLIENT_ID || !process.env.GITHUB_OAUTH_CLIENT_SECRET) {
7273
throw new BadRequestException(
7374
'GitHub auth is not configured, please provide GITHUB_OAUTH_CLIENT_ID and GITHUB_OAUTH_CLIENT_SECRET as env variables'
7475
);
7576
}
7677

77-
Logger.verbose('Github Auth has all variables.');
78+
this.logger.trace('Github Auth has all variables.');
7879

7980
return {
8081
success: true,

apps/api/src/app/auth/framework/community.user.auth.guard.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class CommunityUserAuthGuard extends AuthGuard([PassportStrategyEnum.JWT,
1111
private readonly logger: PinoLogger
1212
) {
1313
super();
14+
this.logger.setContext(this.constructor.name);
1415
}
1516

1617
getAuthenticateOptions(context: ExecutionContext): IAuthModuleOptions<any> {

apps/api/src/app/billing/e2e/create-usage-records.e2e-ee.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* eslint-disable global-require */
2-
import { Logger } from '@nestjs/common';
32
import sinon from 'sinon';
43
import { expect } from 'chai';
54
import { ApiServiceLevelEnum, StripeBillingIntervalEnum } from '@novu/shared';
65
// eslint-disable-next-line no-restricted-imports
76
import { StripeUsageTypeEnum } from '@novu/ee-billing/src/stripe/types';
7+
// eslint-disable-next-line no-restricted-imports
8+
import { Logger } from '@nestjs/common';
89

910
const mockMonthlyBusinessSubscription = {
1011
id: 'subscription_id',

apps/api/src/app/billing/e2e/verify-customer.e2e-ee.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable global-require */
2+
// eslint-disable-next-line no-restricted-imports
23
import { Logger } from '@nestjs/common';
34
import sinon from 'sinon';
45
import { CommunityOrganizationRepository } from '@novu/dal';

apps/api/src/app/blueprint/usecases/get-grouped-blueprints/get-grouped-blueprints.usecase.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
1+
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { NotificationTemplateEntity, NotificationTemplateRepository } from '@novu/dal';
3-
import { buildGroupedBlueprintsKey, CachedResponse } from '@novu/application-generic';
3+
import { buildGroupedBlueprintsKey, CachedResponse, PinoLogger } from '@novu/application-generic';
44
import { IGroupedBlueprint } from '@novu/shared';
55

66
import { GroupedBlueprintResponse } from '../../dto/grouped-blueprint.response.dto';
@@ -10,7 +10,12 @@ const WEEK_IN_SECONDS = 60 * 60 * 24 * 7;
1010

1111
@Injectable()
1212
export class GetGroupedBlueprints {
13-
constructor(private notificationTemplateRepository: NotificationTemplateRepository) {}
13+
constructor(
14+
private notificationTemplateRepository: NotificationTemplateRepository,
15+
private logger: PinoLogger
16+
) {
17+
this.logger.setContext(this.constructor.name);
18+
}
1419

1520
@CachedResponse({
1621
builder: (command: GetGroupedBlueprintsCommand) => buildGroupedBlueprintsKey(command.environmentId),
@@ -54,7 +59,7 @@ export class GetGroupedBlueprints {
5459
const storedBlueprint = storedBlueprints.find((blueprint) => blueprint._id === localPopularId);
5560

5661
if (!storedBlueprint) {
57-
Logger.warn(
62+
this.logger.warn(
5863
`Could not find stored popular blueprint id: ${localPopularId}, BLUEPRINT_CREATOR:
5964
${NotificationTemplateRepository.getBlueprintOrganizationId()}`
6065
);

apps/api/src/app/bridge/usecases/get-bridge-status/get-bridge-status.usecase.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Logger, Injectable } from '@nestjs/common';
1+
import { Injectable } from '@nestjs/common';
22
import { HealthCheck, GetActionEnum } from '@novu/framework/internal';
33
import { ExecuteBridgeRequest, ExecuteBridgeRequestCommand, ExecuteBridgeRequestDto } from '@novu/application-generic';
44
import { WorkflowOriginEnum } from '@novu/shared';

apps/api/src/app/change/change.module.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
DynamicModule,
44
forwardRef,
55
ForwardReference,
6-
Logger,
76
MiddlewareConsumer,
87
Module,
98
NestModule,
@@ -13,6 +12,9 @@ import { SharedModule } from '../shared/shared.module';
1312
import { ChangesController } from './changes.controller';
1413
import { USE_CASES } from './usecases';
1514
import { AuthModule } from '../auth/auth.module';
15+
import { getLogger } from '../shared/services/logger.service';
16+
17+
const logger = getLogger('ChangeModule');
1618

1719
const enterpriseImports = (): Array<Type | DynamicModule | Promise<DynamicModule> | ForwardReference> => {
1820
const modules: Array<Type | DynamicModule | Promise<DynamicModule> | ForwardReference> = [];
@@ -23,7 +25,7 @@ const enterpriseImports = (): Array<Type | DynamicModule | Promise<DynamicModule
2325
}
2426
}
2527
} catch (e) {
26-
Logger.error(e, `Unexpected error while importing enterprise modules`, 'EnterpriseImport');
28+
logger.error(e, `Unexpected error while importing enterprise modules`, 'EnterpriseImport');
2729
}
2830

2931
return modules;

0 commit comments

Comments
 (0)