Skip to content

Commit

Permalink
feat: improve log messages in API (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
fruneen committed Jan 22, 2024
1 parent ac30c38 commit 33c5574
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
5 changes: 3 additions & 2 deletions template/apps/api/src/io-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ let emitter: Emitter | null = null;

const publish = (roomId: string | string[], eventName: string, data: unknown) => {
if (emitter === null) {
redisErrorHandler(new Error('ioEmitter is not initialized.'));
redisErrorHandler(new Error('[Socket.IO] Emitter is not initialized.'));

return;
}

logger.debug(`published io event [${eventName}] to ${roomId}, the data is: ${JSON.stringify(data)}`);
logger.debug(`[Socket.IO] Published [${eventName}] event to ${roomId}, the data is:`);
logger.debug(data);

emitter.to(roomId).emit(eventName, data);
};
Expand Down
2 changes: 1 addition & 1 deletion template/apps/api/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const createConsoleLogger = (isDev: boolean) => {
format: getFormat(isDev),
});

logger.debug('[logger] Configured console based logger');
logger.debug('[Logger] Configured console based logger');

return logger;
};
Expand Down
12 changes: 6 additions & 6 deletions template/apps/api/src/migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const run = async (migrations: Migration[], curVersion: number) => {
.sort((a: Migration, b: Migration) => a.version - b.version);

if (!newMigrations.length) {
logger.info(`No new migrations found, stopping the process.
logger.info(`[Migrator] No new migrations found, stopping the process.
Current database version is ${curVersion}`);
return;
}
Expand All @@ -38,7 +38,7 @@ const run = async (migrations: Migration[], curVersion: number) => {
migrationLogId = generateId();
const startTime = new Date().getSeconds();
await migrationLogService.startMigrationLog(migrationLogId, startTime, migration.version); //eslint-disable-line
logger.info(`Migration #${migration.version} is running: ${migration.description}`);
logger.info(`[Migrator] Migration #${migration.version} is running: ${migration.description}`);
if (!migration.migrate) {
throw new Error('migrate function is not defined for the migration');
}
Expand All @@ -51,14 +51,14 @@ const run = async (migrations: Migration[], curVersion: number) => {
.format('h [hrs], m [min], s [sec], S [ms]');

await migrationLogService.finishMigrationLog(migrationLogId, finishTime, duration); //eslint-disable-line
logger.info(`Database has been updated to the version #${migration.version}`);
logger.info(`Time of migration #${migration.version}: ${duration}`);
logger.info(`[Migrator] Database has been updated to the version #${migration.version}`);
logger.info(`[Migrator] Time of migration #${migration.version}: ${duration}`);
}
logger.info(`All migrations has been finished, stopping the process.
logger.info(`[Migrator] All migrations has been finished, stopping the process.
Current database version is: ${lastMigrationVersion}`);
} catch (err) {
if (migration) {
logger.error(`Failed to update migration to version ${migration.version}`);
logger.error(`[Migrator] Failed to update migration to version ${migration.version}`);
logger.error(err);
if (migrationLogId) {
await migrationLogService.failMigrationLog(migrationLogId, new Date().getSeconds(), err as Error);
Expand Down
2 changes: 1 addition & 1 deletion template/apps/api/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ import logger from 'logger';
import 'scheduler/cron';
import 'scheduler/handlers/action.example.handler';

logger.info('Scheduler has started');
logger.info('[Scheduler] Server has been started');
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const mixpanel = config.MIXPANEL_API_KEY

const track = (event: string, data = {}) => {
if (!mixpanel) {
logger.error('The analytics service was not initialized');
logger.error('[Mixpanel] The analytics service was not initialized');
return;
}

Expand Down
18 changes: 16 additions & 2 deletions template/apps/api/src/services/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import sendgrid from '@sendgrid/mail';

import { renderEmailHtml, Template } from 'mailer';

import logger from 'logger';

import { From, EmailServiceConstructorProps, SendTemplateParams, SendSendgridTemplateParams } from './email.types';

class EmailService {
Expand All @@ -24,7 +26,10 @@ class EmailService {
params,
attachments,
}: SendTemplateParams<T>) {
if (!this.apiKey) return null;
if (!this.apiKey) {
logger.error('[Sendgrid] API key is not provided');
return null;
}

const html = await renderEmailHtml({ template, params });

Expand All @@ -34,6 +39,9 @@ class EmailService {
subject,
html,
attachments,
}).then(() => {
logger.debug(`[Sendgrid] Sent email to ${to}.`);
logger.debug({ subject, template, params });
});
}

Expand All @@ -44,7 +52,10 @@ class EmailService {
dynamicTemplateData,
attachments,
}: SendSendgridTemplateParams) {
if (!this.apiKey) return null;
if (!this.apiKey) {
logger.error('[Sendgrid] API key is not provided');
return null;
}

return sendgrid.send({
from: this.from,
Expand All @@ -53,6 +64,9 @@ class EmailService {
templateId,
dynamicTemplateData,
attachments,
}).then(() => {
logger.debug(`[Sendgrid] Sent email to ${to}.`);
logger.debug({ subject, templateId, dynamicTemplateData });
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion template/apps/api/src/services/socket/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async (server: http.Server) => {

await Promise.all([pubClient.connect(), subClient.connect()]);

logger.info('Socket.io server has been connected.');
logger.info('[Socket.io] Server has been connected.');

subClient.on('error', redisErrorHandler);

Expand Down
6 changes: 3 additions & 3 deletions template/packages/mailer/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { SignUpWelcome, SignUpWelcomeProps } from '../emails/sign-up-welcome';
import { VerifyEmail, VerifyEmailProps } from '../emails/verify-email';

export enum Template {
RESET_PASSWORD,
SIGN_UP_WELCOME,
VERIFY_EMAIL,
RESET_PASSWORD = 'RESET_PASSWORD',
SIGN_UP_WELCOME = 'SIGN_UP_WELCOME',
VERIFY_EMAIL = 'VERIFY_EMAIL',
}

export const EmailComponent = {
Expand Down

0 comments on commit 33c5574

Please sign in to comment.