add log cache, routes, and controller
This commit is contained in:
1 parent
34485a0cae
commit
0b6febbb82
4 files changed
+93
-10
No files matched your search
@@ -0,0 +1,23 @@
|
|||||||
|
import { handleError } from "./controllerUtils.js";
|
||||||
|
|
||||||
|
const SERVICE_NAME = "JobQueueController";
|
||||||
|
|
||||||
|
class LogController {
|
||||||
|
constructor(logger) {
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
getLogs = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const logs = await this.logger.getLogs();
|
||||||
|
res.success({
|
||||||
|
msg: "Logs fetched successfully",
|
||||||
|
data: logs,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(handleError(error, SERVICE_NAME, "getMetrics"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export default LogController;
|
||||||
@@ -36,6 +36,9 @@ import StatusPageController from "./controllers/statusPageController.js";
|
|||||||
import QueueRoutes from "./routes/queueRoute.js";
|
import QueueRoutes from "./routes/queueRoute.js";
|
||||||
import QueueController from "./controllers/queueController.js";
|
import QueueController from "./controllers/queueController.js";
|
||||||
|
|
||||||
|
import LogRoutes from "./routes/logRoutes.js";
|
||||||
|
import LogController from "./controllers/logController.js";
|
||||||
|
|
||||||
import NotificationRoutes from "./routes/notificationRoute.js";
|
import NotificationRoutes from "./routes/notificationRoute.js";
|
||||||
import NotificationController from "./controllers/notificationController.js";
|
import NotificationController from "./controllers/notificationController.js";
|
||||||
|
|
||||||
@@ -294,6 +297,8 @@ const startApp = async () => {
|
|||||||
ServiceRegistry.get(StringService.SERVICE_NAME)
|
ServiceRegistry.get(StringService.SERVICE_NAME)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const logController = new LogController(logger);
|
||||||
|
|
||||||
const statusPageController = new StatusPageController(
|
const statusPageController = new StatusPageController(
|
||||||
ServiceRegistry.get(MongoDB.SERVICE_NAME),
|
ServiceRegistry.get(MongoDB.SERVICE_NAME),
|
||||||
ServiceRegistry.get(StringService.SERVICE_NAME)
|
ServiceRegistry.get(StringService.SERVICE_NAME)
|
||||||
@@ -320,6 +325,7 @@ const startApp = async () => {
|
|||||||
maintenanceWindowController
|
maintenanceWindowController
|
||||||
);
|
);
|
||||||
const queueRoutes = new QueueRoutes(queueController);
|
const queueRoutes = new QueueRoutes(queueController);
|
||||||
|
const logRoutes = new LogRoutes(logController);
|
||||||
const statusPageRoutes = new StatusPageRoutes(statusPageController);
|
const statusPageRoutes = new StatusPageRoutes(statusPageController);
|
||||||
|
|
||||||
const notificationRoutes = new NotificationRoutes(notificationController);
|
const notificationRoutes = new NotificationRoutes(notificationController);
|
||||||
@@ -372,6 +378,7 @@ const startApp = async () => {
|
|||||||
app.use("/api/v1/checks", verifyJWT, checkRoutes.getRouter());
|
app.use("/api/v1/checks", verifyJWT, checkRoutes.getRouter());
|
||||||
app.use("/api/v1/maintenance-window", verifyJWT, maintenanceWindowRoutes.getRouter());
|
app.use("/api/v1/maintenance-window", verifyJWT, maintenanceWindowRoutes.getRouter());
|
||||||
app.use("/api/v1/queue", verifyJWT, queueRoutes.getRouter());
|
app.use("/api/v1/queue", verifyJWT, queueRoutes.getRouter());
|
||||||
|
app.use("/api/v1/logs", verifyJWT, logRoutes.getRouter());
|
||||||
app.use("/api/v1/status-page", statusPageRoutes.getRouter());
|
app.use("/api/v1/status-page", statusPageRoutes.getRouter());
|
||||||
app.use("/api/v1/notifications", verifyJWT, notificationRoutes.getRouter());
|
app.use("/api/v1/notifications", verifyJWT, notificationRoutes.getRouter());
|
||||||
app.use("/api/v1/diagnostic", verifyJWT, diagnosticRoutes.getRouter());
|
app.use("/api/v1/diagnostic", verifyJWT, diagnosticRoutes.getRouter());
|
||||||
|
|||||||
Executable
+18
@@ -0,0 +1,18 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
import { isAllowed } from "../middleware/isAllowed.js";
|
||||||
|
class LogRoutes {
|
||||||
|
constructor(logController) {
|
||||||
|
this.router = Router();
|
||||||
|
this.logController = logController;
|
||||||
|
this.initRoutes();
|
||||||
|
}
|
||||||
|
initRoutes() {
|
||||||
|
this.router.get("/", isAllowed(["admin", "superadmin"]), this.logController.getLogs);
|
||||||
|
}
|
||||||
|
|
||||||
|
getRouter() {
|
||||||
|
return this.router;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default LogRoutes;
|
||||||
+45
-10
@@ -4,6 +4,8 @@ dotenv.config();
|
|||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.logCache = [];
|
||||||
|
this.maxCacheSize = 500;
|
||||||
const consoleFormat = format.printf(
|
const consoleFormat = format.printf(
|
||||||
({ level, message, service, method, details, timestamp, stack }) => {
|
({ level, message, service, method, details, timestamp, stack }) => {
|
||||||
if (message instanceof Object) {
|
if (message instanceof Object) {
|
||||||
@@ -72,11 +74,17 @@ class Logger {
|
|||||||
* @param {Object} config.details - Additional details.
|
* @param {Object} config.details - Additional details.
|
||||||
*/
|
*/
|
||||||
info(config) {
|
info(config) {
|
||||||
this.logger.info(config.message, {
|
const logEntry = {
|
||||||
|
level: "info",
|
||||||
|
message: config.message,
|
||||||
service: config.service,
|
service: config.service,
|
||||||
method: config.method,
|
method: config.method,
|
||||||
details: config.details,
|
details: config.details,
|
||||||
});
|
timestamp: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
this.cacheLog(logEntry);
|
||||||
|
this.logger.info(config.message, logEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,11 +96,17 @@ class Logger {
|
|||||||
* @param {Object} config.details - Additional details.
|
* @param {Object} config.details - Additional details.
|
||||||
*/
|
*/
|
||||||
warn(config) {
|
warn(config) {
|
||||||
this.logger.warn(config.message, {
|
const logEntry = {
|
||||||
|
level: "warn",
|
||||||
|
message: config.message,
|
||||||
service: config.service,
|
service: config.service,
|
||||||
method: config.method,
|
method: config.method,
|
||||||
details: config.details,
|
details: config.details,
|
||||||
});
|
timestamp: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
this.cacheLog(logEntry);
|
||||||
|
this.logger.warn(config.message, logEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,12 +118,17 @@ class Logger {
|
|||||||
* @param {Object} config.details - Additional details.
|
* @param {Object} config.details - Additional details.
|
||||||
*/
|
*/
|
||||||
error(config) {
|
error(config) {
|
||||||
this.logger.error(config.message, {
|
const logEntry = {
|
||||||
|
level: "error",
|
||||||
|
message: config.message,
|
||||||
service: config.service,
|
service: config.service,
|
||||||
method: config.method,
|
method: config.method,
|
||||||
details: config.details,
|
details: config.details,
|
||||||
stack: config.stack,
|
timestamp: new Date().toISOString(),
|
||||||
});
|
};
|
||||||
|
|
||||||
|
this.cacheLog(logEntry);
|
||||||
|
this.logger.error(config.message, logEntry);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Logs a debug message.
|
* Logs a debug message.
|
||||||
@@ -120,12 +139,28 @@ class Logger {
|
|||||||
* @param {Object} config.details - Additional details.
|
* @param {Object} config.details - Additional details.
|
||||||
*/
|
*/
|
||||||
debug(config) {
|
debug(config) {
|
||||||
this.logger.debug(config.message, {
|
const logEntry = {
|
||||||
|
level: "debug",
|
||||||
|
message: config.message,
|
||||||
service: config.service,
|
service: config.service,
|
||||||
method: config.method,
|
method: config.method,
|
||||||
details: config.details,
|
details: config.details,
|
||||||
stack: config.stack,
|
timestamp: new Date().toISOString(),
|
||||||
});
|
};
|
||||||
|
|
||||||
|
this.cacheLog(logEntry);
|
||||||
|
this.logger.debug(config.message, logEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheLog(entry) {
|
||||||
|
this.logCache.push(entry);
|
||||||
|
if (this.logCache.length > this.maxCacheSize) {
|
||||||
|
this.logCache.shift();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getLogs() {
|
||||||
|
return this.logCache;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user