Skip to content

Commit

Permalink
added pino logger and Logflare drain (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaiq-dev authored Apr 27, 2024
1 parent feec94b commit 7a2de44
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 16 deletions.
6 changes: 5 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ CYPHER_ENCRYPTION_KEY=

JWT_EXPIRY=172800 #2 Days
JWT_SECRET=
ADD_PEOPLE_INVITE_EXPIRY=86400 #1 Day
ADD_PEOPLE_INVITE_EXPIRY=86400 #1 Day

# Logflare logging
LOGFLARE_API_KEY=
LOGFLARE_SOURCE_ID=
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"mapbox-gl": "^3.2.0",
"next": "14.1.4",
"next-auth": "^5.0.0-beta.16",
"pino-logflare": "^0.4.2",
"react": "^18",
"react-dom": "^18",
"react-icons": "^5.0.1",
Expand All @@ -40,6 +41,7 @@
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/mapbox-gl": "^3.1.0",
"@types/node": "^20",
"@types/pino": "^7.0.5",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.4.19",
Expand Down
3 changes: 0 additions & 3 deletions src/app/(auth)/signin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ export default function SigninPage() {
setLoading(true);
const resp = await signin({ ...values });

console.log({ resp });

if (!resp.ok) {
setLoading(false);
console.log("Error occurred", resp.error);
return;
}

Expand Down
3 changes: 0 additions & 3 deletions src/app/(auth)/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ export default function SignupPage() {
setLoading(true);
const resp = await createAccount(values);

console.log({ resp });

if (!resp.ok) {
setLoading(false);
console.log("Error occurred", resp.error);
return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/app/_actions/create-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { createValidationContext } from "@/lib/auth";
import prisma from "@/lib/db";
import logger from "@/logging";
import { ActionResponse } from "@/types";

export type CreateAccountInput = {
Expand All @@ -13,6 +14,9 @@ export const createAccount = async (
input: CreateAccountInput
): Promise<ActionResponse<{ validationId: string }>> => {
const { email } = input;

logger.info("New signup request received for '%s'", email);

const existing = await prisma.user.findUnique({
select: {
id: true,
Expand Down
3 changes: 3 additions & 0 deletions src/app/_actions/signin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { createValidationContext } from "@/lib/auth";
import { isUserRegistered } from "@/lib/auth/utils";
import logger from "@/logging";
import { ActionResponse } from "@/types";

export interface SigninInput {
Expand All @@ -14,6 +15,8 @@ export const signin = async (
const { email } = input;
const isUser = await isUserRegistered(email);

logger.info("New signin request received for %s, %o", email, { isUser });

if (!isUser) {
return {
ok: false,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { User } from "@prisma/client/edge";
import { COOKIE_SECURE_AUTH_TOKEN } from "@/constants/app-strings";
import prisma from "@/lib/db";
import { JWTPayload } from "jose";
import logger from "../../logging";
import { decode, encode } from "./jwt";

export interface AuthorizationOptions<P extends JWTPayload> {
Expand Down Expand Up @@ -98,7 +99,8 @@ export const getSession = async (
return [true, user];
}
} catch (error) {
console.error(error);
logger.error("getSession exception while retreiving user");
logger.error(error);
}

return [false, null];
Expand Down
1 change: 0 additions & 1 deletion src/lib/cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class Cypher {

return Buffer.concat([iv, encryptedValueBuffer, cipher.getAuthTag()]).toString("hex");
} catch (error) {
console.log("[pa/cypher] Error encrypting", error);
return null;
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/lib/mailer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logger from "@/logging";
import { StringRecord } from "@/types";
import mail, { MailService } from "@sendgrid/mail";

Expand Down Expand Up @@ -42,8 +43,8 @@ export class Mailer {

// Dev mode wil print the mail to console
if (this.devMode) {
console.log(
`[pa/mailer] New email to ${to} for ${template} - ${JSON.stringify(dynamicTemplateData)}`
logger.info(
`[pa/mailer:dev] New email to ${to} for ${template} - ${JSON.stringify(dynamicTemplateData)}`
);
return;
}
Expand All @@ -58,8 +59,11 @@ export class Mailer {
templateId: this.templates[template],
dynamicTemplateData,
});

logger.info("Sent %s email to %s", template, to);
} catch (error) {
console.log("[pa/mailer] Failed to send email", JSON.stringify(error));
logger.error("Error occured while sending %s email to %s", template, to);
logger.error(error);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/logging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pino from "pino";
import { logflarePinoVercel } from "pino-logflare";

const { stream, send } = logflarePinoVercel({
apiKey: process.env.LOGFLARE_API_KEY!,
sourceToken: process.env.LOGFLARE_SOURCE_ID!,
});

const logger = pino(
{
browser: {
transmit: {
level: "info",
send: send,
},
},
level: "debug",
base: {
env: process.env.NODE_ENV,
revision: process.env.VERCEL_GITHUB_COMMIT_SHA,
},
},
stream
);

export default logger;
Loading

0 comments on commit 7a2de44

Please sign in to comment.