Skip to content

Commit

Permalink
use pino for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dlustre committed Apr 26, 2024
1 parent d61ddcd commit f877947
Show file tree
Hide file tree
Showing 8 changed files with 2,462 additions and 3,209 deletions.
3 changes: 3 additions & 0 deletions apps/server/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pino from "pino";

export const logger = pino({ level: "debug" });
5 changes: 3 additions & 2 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"clean": "rm -rf .turbo node_modules",
"with-env": "dotenv -e ../../.env -- ",
"dev": "pnpm with-env sls offline --noPrependStageInUrl",
"sls:deploy": "pnpm with-env sls deploy",
"sls:test": "pnpm with-env sls invoke local --function"
"deploy": "pnpm with-env sls deploy",
"test:daily": "pnpm with-env sls invoke local --function updateDaily | pino-pretty",
"test:weekly": "pnpm with-env sls invoke local --function getWeekly | pino-pretty"
},
"engines": {
"node": ">=20.12.1"
Expand Down
7 changes: 4 additions & 3 deletions apps/server/src/functions/cron/getWeeklyHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { format } from "date-fns";
import { logger } from "logger";

import {
getWeekInfo,
Expand All @@ -17,7 +18,7 @@ export const main = async (_event, _context) => {

const now = new Date();
const formattedTime = format(now, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx");
console.log(`Weekly task executed at: ${formattedTime}`);
logger.info(`Weekly task executed at: ${formattedTime}`);

const formattedDate = format(now, "MM/dd/yyyy");

Expand All @@ -33,11 +34,11 @@ export const main = async (_event, _context) => {
// log errors if any
results.forEach((result) => {
if (result.status === "rejected") {
console.error(result.reason);
logger.error(result.reason);
}
});
} catch (error) {
console.error("Failed to execute weekly task", error);
logger.error("Failed to execute weekly task", error);
} finally {
await pool({ connectionString }).end();
}
Expand Down
16 changes: 8 additions & 8 deletions apps/server/src/functions/cron/updateDailyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import {
import { createDrizzle, pool } from "@zotmeal/db";
import { RESTAURANT_TO_ID } from "@zotmeal/utils";

import { logger } from "../../../logger";

const connectionString =
process.env.DATABASE_URL ?? "postgres://admin:admin@localhost:5434/zotmeal";

export const main = async (_event, _context) => {
try {
const db = createDrizzle({ connectionString });
const now = new Date();
const formattedTime = format(now, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx");
console.log(`Start update daily job at ${formattedTime}`);
logger.info("Start update daily job...");

const date = format(now, "MM/dd/yyyy");
const date = format(new Date(), "MM/dd/yyyy");

await Promise.allSettled(
Object.keys(RESTAURANT_TO_ID).map((restaurantName) =>
Expand All @@ -28,12 +28,12 @@ export const main = async (_event, _context) => {
),
);

console.log("Finished update daily job.");
logger.info("Finished update daily job.");
} catch (error) {
console.error("Failed to execute weekly task", error);
logger.error("Failed to execute weekly task", error);
} finally {
console.log("Closing connection pool...");
logger.info("Closing connection pool...");
await pool({ connectionString }).end();
console.log("Closed connection pool.");
logger.info("Closed connection pool.");
}
};
3 changes: 3 additions & 0 deletions packages/api/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pino from "pino";

export const logger = pino({ level: "debug" });
3 changes: 2 additions & 1 deletion packages/api/src/services/getWeekInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RestaurantSchema } from "@zotmeal/db/src/schema";
import { DateRegex } from "@zotmeal/validators";

import type { UpdateDailyParams } from "./updateDaily";
import { logger } from "../../logger";
import { updateDaily } from "./updateDaily";

export const GetWeekInfoSchema = z.object({
Expand Down Expand Up @@ -41,7 +42,7 @@ export async function getWeekInfo(
// log errors from the promises
results.forEach((result, i) => {
if (result.status === "rejected") {
console.error(`Error updating day ${i + 1}:`, result.reason);
logger.error(`Error updating day ${i + 1}:`, result.reason);
}
});
}
7 changes: 5 additions & 2 deletions packages/api/src/services/updateDaily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DateRegex } from "@zotmeal/validators";

import type { GetMenuParams } from "../menus/services/parse";
import { getCampusDish, parseCampusDish } from "..";
import { logger } from "../../logger";

export const UpdateDailySchema = z.object({
date: DateRegex,
Expand All @@ -19,7 +20,9 @@ export async function updateDaily(
params: UpdateDailyParams,
): Promise<void> {
try {
console.log(`(${params.date}) Updating ${params.restaurantName}...`);
logger.info(
`Updating ${params.restaurantName} menu for (${params.date})...`,
);

const { date, restaurantName } = UpdateDailySchema.parse(params);

Expand All @@ -39,7 +42,7 @@ export async function updateDaily(
});
}),
);
console.log(`Updated ${params.restaurantName}.`);
logger.info(`Updated ${params.restaurantName}.`);
} catch (err) {
if (err instanceof z.ZodError) {
console.error(err.issues);
Expand Down
Loading

0 comments on commit f877947

Please sign in to comment.