[develop | DONE | CHANGED] update server.ts methods for logs

This commit is contained in:
argirok 2023-12-13 17:42:28 +02:00
parent 143f1c0ba1
commit a6a9278cd6
1 changed files with 55 additions and 13 deletions

View File

@ -38,10 +38,7 @@ export function app(): express.Express {
server.get('/log/:id', (req, res) => { server.get('/log/:id', (req, res) => {
res.status(200).json(getLogs(req.params['id'])); res.status(200).json(getLogs(req.params['id']));
}); });
function getLogs(id){
const file = readFileSync(properties.logFilesPath + 'actions.log' + (id?"."+id:""), 'utf-8');
return JSON.parse("[" + file.substring(0,file.length - 2) + "]");
}
server.get('/logFiles', (req, res) => { server.get('/logFiles', (req, res) => {
fs.readdir(properties.logFilesPath, (err, files) => { fs.readdir(properties.logFilesPath, (err, files) => {
res.status(200).json(files); res.status(200).json(files);
@ -49,20 +46,29 @@ export function app(): express.Express {
}); });
server.get('/prettyLog', (req, res) => { server.get('/prettyLog', (req, res) => {
const file = readFileSync('/var/log/irish-log/foo.txt', 'utf-8'); res.status(200).send( getPrettyLogs(null));
let logs = JSON.parse("[" + file.substring(0,file.length - 2) + "]"); });
let prettyLog = ""; server.get('/prettyLog/:id', (req, res) => {
for(let i = logs.length -1; i>=0; i--){ res.status(200).send( getPrettyLogs(req.params['id']));
prettyLog += "On " + (logs[i].date?logs[i].date:"") + " " /*+ logs[i].action + " "*/ + logs[i].message+ "<br>";
}
res.status(200).send( prettyLog);
}); });
server.post('/logAction', jsonParser,(req, res) => { server.post('/logAction', jsonParser,(req, res) => {
let log = req.body; let log = req.body;
log.date = new Date(); log.date = new Date();
fs.appendFile('/var/log/irish-log/foo.txt', JSON.stringify(log)+ ",\n", 'utf8', function(err) { fs.appendFile(properties.logFilesPath + 'actions.log', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
if (err) {
return console.error(err);
}
});
res.status(200).send({
code: 200,
message: 'action received!'
});
});
server.get('/testLogAction', jsonParser,(req, res) => {
let log = {message:"log", date: new Date()};
fs.appendFile(properties.logFilesPath + 'actions.log', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
if (err) { if (err) {
return console.error(err); return console.error(err);
} }
@ -108,7 +114,43 @@ function run(): void {
console.log(`Node Express server listening on http://localhost:${port}`); console.log(`Node Express server listening on http://localhost:${port}`);
}); });
} }
function getLogs(id){
const file = readFileSync(properties.logFilesPath + 'actions.log' + (id?"."+id:""), 'utf-8');
return JSON.parse("[" + file.substring(0,file.length - 2) + "]");
}
function getPrettyLogs(id){
const file = readFileSync(properties.logFilesPath + 'actions.log'+ (id?"."+id:""), 'utf-8');
let logs = JSON.parse("[" + file.substring(0,file.length - 2) + "]");
let prettyLog = "";
for(let i = logs.length -1; i>=0; i--) {
prettyLog += "On " + (logs[i].date ? formatDateAndTime(logs[i].date) : "") + " " /*+ logs[i].action + " "*/ + logs[i].message + "<br>";
}
return prettyLog + getPrevNextLog(id?id:0);
}
function formatDateAndTime(dateStr){
let date = new Date(dateStr);
const formatter = new Intl.DateTimeFormat('en-GB', {
timeStyle: 'short',
timeZone: 'GMT',
});
return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear() + " "
+ formatter.format(date) + "";
}
function getPrevNextLog(id= 0){
const files = fs.readdirSync(properties.logFilesPath);
let id_next:number = +id+1;
let id_prev:number = id-1;
let prev = (id>1 && files.indexOf("actions.log."+id_prev )!=-1)? '<a style="margin-left: 5px;" href="'+properties.logServiceUrl +
'prettyLog/' + id_prev + '">Prev</a>':"";
prev = (id ==1 && files.indexOf("actions.log")!=-1)? '<a style="margin-left: 5px;" href="'+properties.logServiceUrl +
'prettyLog">Prev</a>':prev;
let next = (id>=0 && files.indexOf("actions.log."+ id_next )!=-1)? ('<a style="margin-left: 5px;" href="'+properties.logServiceUrl +'prettyLog/' +id_next + '">Next</a>'):"";
return "<br>" + prev + next;
}
// Webpack will replace 'require' with '__webpack_require__' // Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require' // '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle. // The below code is to ensure that the server is run only when not requiring the bundle.