-
Notifications
You must be signed in to change notification settings - Fork 0
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
PDT24-24 | Create logger wrapper #10
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fcc07df
feat: add logging service with winston
noxiousghost 809a8d5
chore(logging): use and fix logging information
noxiousghost a34fcab
PDT24-34 | Configure nodemailer service (#12)
noxiousghost 563390e
refactor: remove ternary operator
noxiousghost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { format, transports } from 'winston'; | ||
const isProduction = process.env.APP_ENV === 'production'; | ||
export const loggerConfig = { | ||
format: format.combine( | ||
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), | ||
format.printf(({ level, message, timestamp }) => { | ||
return `${timestamp} [${level}]: ${message}`; | ||
}), | ||
), | ||
transports: isProduction | ||
? [ | ||
new transports.File({ filename: 'logs/errors.log', level: 'error' }), | ||
new transports.File({ filename: 'logs/all.log' }), | ||
] | ||
: [new transports.Console({ format: format.combine(format.colorize({ all: true })) })], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wont there be logger file for development ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can check all the logs in our console in development. |
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Address } from 'nodemailer/lib/mailer'; | ||
export class MailerDto { | ||
sender?: Address; | ||
recipients: Address[]; | ||
subject: string; | ||
html: string; | ||
text?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MailerService } from './mailer.service'; | ||
|
||
@Module({ | ||
providers: [MailerService], | ||
exports: [MailerService], | ||
}) | ||
export class MailerModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createTransport, SendMailOptions, Transporter } from 'nodemailer'; | ||
import { Logger } from '@nestjs/common'; | ||
import { MailerDto } from '@/mailer/dto/mailer.dto'; | ||
|
||
export class MailerService { | ||
private readonly logger = new Logger(); | ||
transporter: Transporter = createTransport({ | ||
host: process.env.EMAIL_HOST, | ||
port: 465, | ||
secure: true, | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
}); | ||
|
||
async sendEmail(data: MailerDto): Promise<void> { | ||
try { | ||
const { sender, recipients, subject, html, text } = data; | ||
const mailOptions: SendMailOptions = { | ||
from: sender ?? { | ||
name: process.env.EMAIL_SENDER_NAME as string, | ||
address: process.env.EMAIL_USER as string, | ||
}, | ||
to: recipients, | ||
subject, | ||
html, | ||
text, | ||
}; | ||
await this.transporter.sendMail(mailOptions); | ||
this.logger.log('Mail sent successfully'); | ||
} catch (error) { | ||
this.logger.error(`Error sending email:`, { error }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const signupOtpMailTemplate = { | ||
subject: 'Verify your account', | ||
/** | ||
* Generates a random OTP of the specified size. | ||
* @param otpCode - code to send to through email. | ||
* @param userName - name of the user to send the email to. | ||
*/ | ||
body: (otpCode: number, userName: string): string => ` | ||
<div> | ||
<p>Welcome ${userName}</p> | ||
<p>Use this code to verify your account:<br><b>${otpCode}</b> | ||
</div> | ||
`, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After merging PDT24-25 lets use
env.NODE_ENV ===APP_ENVIRONVENT.PRODUCTION