added error handling system in logger

This commit is contained in:
2025-12-31 13:14:47 -05:00
parent cf8f0fbc34
commit 6139f12e13
2 changed files with 30 additions and 12 deletions

View File

@@ -11,23 +11,37 @@ import { assignUserToStatus } from '../services/db/statusService';
import { Request, response, Response } from 'express';
import { getUserRoles } from '../services/db/rolesService';
import { requireLogin, requireRole } from '../middleware/auth';
import { logger } from '../services/logging/logger';
//get CoC
router.get('/coc', async (req: Request, res: Response) => {
const output = await fetch(`${process.env.DOC_HOST}/api/pages/714`, {
headers: {
Authorization: `Token ${process.env.DOC_TOKEN_ID}:${process.env.DOC_TOKEN_SECRET}`,
}
})
try {
const response = await fetch(`${process.env.DOC_HOST}/api/pages/714`, {
headers: {
Authorization: `Token ${process.env.DOC_TOKEN_ID}:${process.env.DOC_TOKEN_SECRET}`,
},
});
if (output.ok) {
const out = await output.json();
if (!response.ok) {
const text = await response.text();
logger.error('app', 'Failed to fetch LOA policy from Bookstack', {
status: response.status,
statusText: response.statusText,
body: text,
});
return res.sendStatus(500);
}
const out = await response.json();
res.status(200).json(out.html);
} else {
console.error("Failed to fetch LOA policy from bookstack");
} catch (error) {
logger.error('app', 'Error fetching LOA policy from Bookstack', {
error: error instanceof Error ? error.message : String(error),
});
res.sendStatus(500);
}
})
});
// POST /application

View File

@@ -29,7 +29,11 @@ function emitLog(header: LogHeader, payload: LogPayload = {}) {
if (!shouldLog(header.depth)) return;
const logLine = { ...header, ...payload };
console.log(JSON.stringify(logLine));
if (header.level === 'error')
console.error(JSON.stringify(logLine))
else
console.log(JSON.stringify(logLine));
}
export const logger = {