feat(logger): setup logger with discord and db logging
This commit is contained in:
@@ -7,13 +7,22 @@ import path, { dirname, join } from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { db } from "./pkg/db/db.js";
|
||||
import { settings } from "./pkg/db/schema/settings.js";
|
||||
import { env } from "./pkg/utils/envValidator.js";
|
||||
import { createLogger } from "./pkg/logger/logger.js";
|
||||
|
||||
const PORT = Number(process.env.VITE_PORT) || 4200;
|
||||
const PORT = Number(env.VITE_PORT) || 4200;
|
||||
|
||||
const main = async () => {
|
||||
//create the logger
|
||||
const log = createLogger({ module: "system", subModule: "main start" });
|
||||
|
||||
// base path
|
||||
let basePath: string = "";
|
||||
if (process.env.NODE_ENV?.trim() !== "production") {
|
||||
|
||||
if (
|
||||
process.env.NODE_ENV?.trim() !== "production" &&
|
||||
!env.RUNNING_IN_DOCKER
|
||||
) {
|
||||
basePath = "/lst";
|
||||
}
|
||||
|
||||
@@ -24,7 +33,11 @@ const main = async () => {
|
||||
try {
|
||||
const set = await db.select().from(settings);
|
||||
|
||||
console.log(set);
|
||||
if (set.length === 0) {
|
||||
return log.fatal(
|
||||
"Seems like the DB is not setup yet the app will close now"
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error getting settings", error);
|
||||
}
|
||||
@@ -63,13 +76,16 @@ const main = async () => {
|
||||
|
||||
// start the server up
|
||||
server.listen(PORT, "0.0.0.0", () =>
|
||||
console.log(
|
||||
`Server running in ${process.env.NODE_ENV}, on http://0.0.0.0:${PORT}${basePath}`
|
||||
log.info(
|
||||
`Server running in ${
|
||||
process.env.NODE_ENV ? process.env.NODE_ENV : "dev"
|
||||
}, on http://0.0.0.0:${PORT}${basePath}`
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
main().catch((err) => {
|
||||
console.error("Startup error:", err);
|
||||
const log = createLogger({ module: "system", subModule: "main start" });
|
||||
log.fatal("Startup error:", err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
21
app/src/pkg/db/schema/logs.ts
Normal file
21
app/src/pkg/db/schema/logs.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { boolean, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
|
||||
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
|
||||
export const logs = pgTable("logs", {
|
||||
log_id: uuid("log_id").defaultRandom().primaryKey(),
|
||||
level: text("level"),
|
||||
module: text("module").notNull(),
|
||||
subModule: text("subModule"),
|
||||
message: text("message").notNull(),
|
||||
stack: text("stack"),
|
||||
checked: boolean("checked").default(false),
|
||||
hostname: text("hostname"),
|
||||
createdAt: timestamp("createdAt").defaultNow(),
|
||||
});
|
||||
|
||||
export const logSchema = createSelectSchema(logs);
|
||||
export const newSettingSchema = createInsertSchema(logs);
|
||||
|
||||
export type Log = z.infer<typeof logSchema>;
|
||||
export type NewLog = z.infer<typeof newSettingSchema>;
|
||||
34
app/src/pkg/logger/dbTransport.ts
Normal file
34
app/src/pkg/logger/dbTransport.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import build from "pino-abstract-transport";
|
||||
import { db } from "../db/db.js";
|
||||
import { logs, type Log } from "../db/schema/logs.js";
|
||||
|
||||
const pinoLogLevels: any = {
|
||||
10: "trace",
|
||||
20: "debug",
|
||||
30: "info",
|
||||
40: "warn",
|
||||
50: "error",
|
||||
60: "fatal",
|
||||
};
|
||||
// Create a custom transport function
|
||||
export default async function (log: Log) {
|
||||
//const {username, service, level, msg, ...extra} = log;
|
||||
try {
|
||||
return build(async function (source) {
|
||||
for await (let obj of source) {
|
||||
// convert to the name to make it more easy to find later :P
|
||||
const levelName = pinoLogLevels[obj.level] || "unknown";
|
||||
await db.insert(logs).values({
|
||||
level: levelName,
|
||||
module: obj?.module.toLowerCase(),
|
||||
subModule: obj?.subModule.toLowerCase(),
|
||||
hostname: obj?.hostname.toLowerCase(),
|
||||
message: obj.msg,
|
||||
stack: obj?.stack,
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Error inserting log into database:", err);
|
||||
}
|
||||
}
|
||||
53
app/src/pkg/logger/logger.ts
Normal file
53
app/src/pkg/logger/logger.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import pino, { type Logger } from "pino";
|
||||
import { env } from "../utils/envValidator.js";
|
||||
|
||||
export let logLevel = process.env.LOG_LEVEL || "info";
|
||||
|
||||
interface CustomLogger extends pino.Logger {
|
||||
specialMonitor: pino.LogFn;
|
||||
}
|
||||
|
||||
const transport = pino.transport({
|
||||
targets: [
|
||||
{
|
||||
target: "pino-pretty",
|
||||
options: {
|
||||
colorize: true,
|
||||
singleLine: true,
|
||||
// customPrettifiers: {
|
||||
// time: (time) => `🕰 ${time}`,
|
||||
// },
|
||||
destination: process.stdout.fd,
|
||||
},
|
||||
},
|
||||
{
|
||||
target: "./dbTransport.js",
|
||||
},
|
||||
{
|
||||
target: "./notification.js",
|
||||
},
|
||||
// Only log to Go if LST_USE_GO=true
|
||||
...(process.env.LST_USE_GO === "true"
|
||||
? [
|
||||
{
|
||||
target: "./goTransport.js", // New transport for Go
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
});
|
||||
|
||||
export const rootLogger: Logger = pino(
|
||||
{
|
||||
level: logLevel,
|
||||
redact: { paths: ["email", "password"], remove: true },
|
||||
},
|
||||
transport
|
||||
);
|
||||
|
||||
/**
|
||||
* factory to create child to log things for us
|
||||
*/
|
||||
export function createLogger(bindings: Record<string, unknown>): Logger {
|
||||
return rootLogger.child(bindings);
|
||||
}
|
||||
87
app/src/pkg/logger/notification.ts
Normal file
87
app/src/pkg/logger/notification.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import build from "pino-abstract-transport";
|
||||
import { db } from "../db/db.js";
|
||||
import { logs, type Log } from "../db/schema/logs.js";
|
||||
import { env } from "../utils/envValidator.js";
|
||||
|
||||
const pinoLogLevels: any = {
|
||||
10: "trace",
|
||||
20: "debug",
|
||||
30: "info",
|
||||
40: "warn",
|
||||
50: "error",
|
||||
60: "fatal",
|
||||
};
|
||||
// discord function
|
||||
async function sendFatal(log: Log) {
|
||||
const webhookUrl = process.env.WEBHOOK_URL!;
|
||||
|
||||
const payload = {
|
||||
embeds: [
|
||||
{
|
||||
title: `🚨 ${env.PROD_PLANT_TOKEN}: encounter a critical error `,
|
||||
description: `Where was the error: ${log.module}${
|
||||
log.subModule ? `-${log.subModule}` : null
|
||||
}`,
|
||||
color: 0xff0000, // red
|
||||
fields: [
|
||||
{
|
||||
name: "Message",
|
||||
value: log.message,
|
||||
inline: false,
|
||||
},
|
||||
{
|
||||
name: "Hostname",
|
||||
value: log.hostname,
|
||||
inline: false,
|
||||
},
|
||||
{
|
||||
name: "Stack",
|
||||
value:
|
||||
"```" +
|
||||
(log.stack?.slice(0, 1000) ?? "no stack") +
|
||||
"```",
|
||||
},
|
||||
],
|
||||
footer: {
|
||||
text: "LST Logger 💀",
|
||||
},
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
await fetch(webhookUrl, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
|
||||
export default async function (log: Log) {
|
||||
//const {username, service, level, msg, ...extra} = log;
|
||||
try {
|
||||
return build(async function (source) {
|
||||
for await (let obj of source) {
|
||||
// convert to the name to make it more easy to find later :P
|
||||
const levelName = pinoLogLevels[obj.level] || "unknown";
|
||||
|
||||
const newlog = {
|
||||
level: levelName,
|
||||
module: obj?.module.toLowerCase(),
|
||||
subModule: obj?.subModule.toLowerCase(),
|
||||
hostname: obj?.hostname.toLowerCase(),
|
||||
message: obj.msg,
|
||||
};
|
||||
if (!process.env.WEBHOOK_URL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj.level >= 60) {
|
||||
sendFatal(newlog as Log);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Error inserting log into database:", err);
|
||||
}
|
||||
}
|
||||
60
app/src/pkg/utils/checkHostNamePort.ts
Normal file
60
app/src/pkg/utils/checkHostNamePort.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import dns from "dns";
|
||||
import net from "net";
|
||||
|
||||
export async function checkHostnamePort(
|
||||
hostnamePort: string
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
// Split the input into hostname and port
|
||||
const [hostname, port] = hostnamePort.split(":");
|
||||
if (!hostname || !port) {
|
||||
return false; // Invalid format
|
||||
}
|
||||
|
||||
// Resolve the hostname to an IP address
|
||||
const ip = (await resolveHostname(hostname)) as string;
|
||||
|
||||
// Check if the port is open
|
||||
const portCheck = await checkPort(ip, parseInt(port, 10));
|
||||
|
||||
return true; // Hostname:port is reachable
|
||||
} catch (err) {
|
||||
return false; // Any error means the hostname:port is not reachable
|
||||
}
|
||||
}
|
||||
|
||||
function checkPort(ip: string, port: number): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = new net.Socket();
|
||||
|
||||
socket.setTimeout(2000); // Set a timeout for the connection attempt
|
||||
|
||||
socket.on("connect", () => {
|
||||
socket.destroy(); // Close the connection
|
||||
resolve(true); // Port is open
|
||||
});
|
||||
|
||||
socket.on("timeout", () => {
|
||||
socket.destroy(); // Close the connection
|
||||
reject(new Error("Connection timed out")); // Port is not reachable
|
||||
});
|
||||
|
||||
socket.on("error", (err: any) => {
|
||||
reject(new Error(`Unknown error: ${err}`)); // Handle non-Error types
|
||||
});
|
||||
|
||||
socket.connect(port, ip);
|
||||
});
|
||||
}
|
||||
|
||||
function resolveHostname(hostname: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
dns.lookup(hostname, (err, address) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(address);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
53
app/src/pkg/utils/envValidator.ts
Normal file
53
app/src/pkg/utils/envValidator.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { z } from "zod";
|
||||
import { createLogger } from "../logger/logger.js";
|
||||
/**
|
||||
* This is where we will validate the required ENV parapmeters.
|
||||
*
|
||||
*/
|
||||
const envSchema = z.object({
|
||||
//Server stuff
|
||||
VITE_PORT: z.string().default("4200"),
|
||||
LOG_LEVEL: z.string().default("info"),
|
||||
// app db stuff
|
||||
DATABASE_HOST: z.string(),
|
||||
DATABASE_PORT: z.string(),
|
||||
DATABASE_USER: z.string(),
|
||||
DATABASE_PASSWORD: z.string(),
|
||||
DATABASE_DB: z.string().default("lst"),
|
||||
// prod server checks
|
||||
PROD_SERVER: z.string(),
|
||||
PROD_PLANT_TOKEN: z.string(),
|
||||
PROD_USER: z.string(),
|
||||
PROD_PASSWORD: z.string(),
|
||||
//docker specifics
|
||||
RUNNING_IN_DOCKER: z.boolean().default(false),
|
||||
});
|
||||
|
||||
// use safeParse instead of parse
|
||||
const parsed = envSchema.safeParse(process.env);
|
||||
|
||||
const log = createLogger({ module: "envValidation" });
|
||||
|
||||
if (!parsed.success) {
|
||||
log.fatal(
|
||||
`Environment validation failed: Missing: ${parsed.error.issues
|
||||
.map((e) => {
|
||||
return e.path[0];
|
||||
})
|
||||
.join(", ")}`
|
||||
);
|
||||
// 🔔 Send a notification (e.g., email, webhook, Slack)
|
||||
// sendNotification(parsed.error.format());
|
||||
|
||||
// gracefully exit if in production
|
||||
//process.exit(1);
|
||||
throw Error(
|
||||
`Environment validation failed: Missing: ${parsed.error.issues
|
||||
.map((e) => {
|
||||
return e.path[0];
|
||||
})
|
||||
.join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
export const env = parsed.data;
|
||||
Reference in New Issue
Block a user