Skip to content

Commit

Permalink
feat add logger loglevel and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
berkingurcan committed Jun 10, 2024
1 parent 5d60dca commit 89d4b76
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"discord.js": "^14.14.1",
"dotenv": "^16.4.5",
"eslint-import-resolver-typescript": "^3.6.1",
"loglevel": "^1.9.1",
"loglevel-plugin-prefix": "^0.8.4",
"mocha": "^10.4.0",
"openai": "^4.31.0",
"path": "^0.12.7",
Expand Down
18 changes: 10 additions & 8 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import log from './logger';

import { setIntervalAsync } from "set-interval-async/dynamic";
import { startAutoPosting } from "./lib/startAutoPosting.js";
import {
Expand Down Expand Up @@ -31,11 +33,11 @@ import { REST } from "@discordjs/rest";
import { Routes } from "discord-api-types/v10";

process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
log.error("Unhandled promise rejection:", error);
});

process.on("uncaughtException", (error) => {
console.error("Unhandled exception:", error);
log.error("Unhandled exception:", error);
});

(async () => {
Expand All @@ -54,13 +56,13 @@ process.on("uncaughtException", (error) => {
),
{ body: [command.toJSON()] },
);
console.log("Successfully registered commands.");
log.info("Successfully registered commands.");
} catch (error) {
console.error("Error registering commands", error);
log.error("Error registering commands", error);
}

client.once("ready", () => {
console.log("Ready as ", client.user.username);
log.info("Ready as ", client.user.username);
startAutoPosting(client, redisClient);
startSurveyStatusChecker(redisClient);
});
Expand Down Expand Up @@ -109,7 +111,7 @@ process.on("uncaughtException", (error) => {
await handleInfo(interaction, version);
break;
default:
console.error("unknown subcommand");
log.error("unknown subcommand");
}
} else if (interaction.isButton()) {
if (interaction.customId.startsWith("respondButton")) {
Expand Down Expand Up @@ -206,12 +208,12 @@ const checkAndUpdateSurveyStatus = async (redisClient: any) => {

if (endTime && isActive === "true" && currentTime >= parseInt(endTime)) {
updateMulti.set(`survey:${surveys[i]}:is-active`, "false");
console.log(`Survey ${surveys[i]} is now inactive.`);
log.info(`Survey ${surveys[i]} is now inactive.`);
}
}

await updateMulti.exec();
} catch (error) {
console.error("Error checking and updating survey status:", error);
log.error("Error checking and updating survey status:", error);
}
};
16 changes: 16 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';

prefix.reg(log);
prefix.apply(log, {
format(level, name, timestamp) {
return `${timestamp} ${level.toUpperCase()}:`;
},
timestampFormatter(date) {
return date.toISOString();
},
});

log.setLevel(log.levels.TRACE); // Set the desired log level

export default log;
15 changes: 8 additions & 7 deletions src/summarizer.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import log from "./logger";
import { createClient } from "redis";
import { checkUpdateSurveys } from "./lib/checkUpdateSurveys.js";
import { redisConfig } from "./config.js";

const subscribeRedisClient = createClient(redisConfig);
subscribeRedisClient.on("error", (err) =>
console.log("Redis Client Error", err),
log.error("Redis Client Error", err),
);

const redisClient = createClient(redisConfig);
redisClient.on("error", (err) => console.log("Redis Client Error", err));
redisClient.on("error", (err) => log.error("Redis Client Error", err));

process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
log.error("Unhandled promise rejection:", error);
});

process.on("uncaughtException", (error) => {
console.error("Unhandled exception:", error);
log.error("Unhandled exception:", error);
});

const main = async () => {
await subscribeRedisClient.connect();
await redisClient.connect();

while (true) {
console.log("checking surveys for updates...");
log.info("checking surveys for updates...");
try {
await checkUpdateSurveys(redisClient);
} catch (e) {
console.error("error while processing surveys:", e);
log.error("error while processing surveys:", e);
}
console.log("done checking surveys for updates.");
log.info("done checking surveys for updates.");
await new Promise((r) => setTimeout(r, 1 * 1000));
}

Expand Down

0 comments on commit 89d4b76

Please sign in to comment.