feat(v1 logger): added in a logger to monitor the old app
This commit is contained in:
70
app/src/pkg/logger/v1Listener.ts
Normal file
70
app/src/pkg/logger/v1Listener.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import { Client } from "pg";
|
||||||
|
import { createLogger } from "./logger.js";
|
||||||
|
|
||||||
|
type NewLog = {
|
||||||
|
level: string;
|
||||||
|
username: string;
|
||||||
|
service: string;
|
||||||
|
message: string;
|
||||||
|
checked: boolean;
|
||||||
|
add_Date: Date;
|
||||||
|
};
|
||||||
|
export const v1Listener = async () => {
|
||||||
|
const log = createLogger({ module: "logger", subModule: "Old logging app" });
|
||||||
|
const client = new Client({
|
||||||
|
connectionString: process.env.DATABASE_URL_V1,
|
||||||
|
});
|
||||||
|
|
||||||
|
await client.connect();
|
||||||
|
|
||||||
|
// the notify channel to listen for logs on
|
||||||
|
const channels = ["logs_channel", "users_channel", "orders_channel"];
|
||||||
|
|
||||||
|
for (const ch of channels) {
|
||||||
|
await client.query(`LISTEN ${ch}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Listening for:", channels.join(", "));
|
||||||
|
|
||||||
|
// create the log function to be able to mimic what is coming over
|
||||||
|
const logEvent = (newLog: string) => {
|
||||||
|
const newLogEvent: NewLog = JSON.parse(newLog);
|
||||||
|
|
||||||
|
switch (newLogEvent.level) {
|
||||||
|
case "info":
|
||||||
|
log.info(
|
||||||
|
{ username: newLogEvent.username, service: newLogEvent.service },
|
||||||
|
newLogEvent.message,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "error":
|
||||||
|
log.error(
|
||||||
|
{ username: newLogEvent.username, service: newLogEvent.service },
|
||||||
|
newLogEvent.message,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
log.info(
|
||||||
|
{ username: newLogEvent.username, service: newLogEvent.service },
|
||||||
|
newLogEvent.message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
client.on("notification", (msg) => {
|
||||||
|
// msg.channel tells which channel it came from
|
||||||
|
// msg.payload is whatever message you sent from the trigger
|
||||||
|
switch (msg.channel) {
|
||||||
|
case "logs_channel":
|
||||||
|
logEvent(msg.payload || "");
|
||||||
|
break;
|
||||||
|
case "users_channel":
|
||||||
|
console.log("👤 User event:", msg.payload);
|
||||||
|
break;
|
||||||
|
case "orders_channel":
|
||||||
|
console.log("🛒 Order event:", msg.payload);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log("Other event:", msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user