From affba14f3c91db26169473ad03664964b5a066d3 Mon Sep 17 00:00:00 2001 From: Klaas Date: Fri, 31 May 2024 22:21:44 +0200 Subject: [PATCH] logging --- .gitignore | 1 + app.js | 21 ++++++++++++++++++++- errorsLog.log | 0 outputLog.log | 0 package-lock.json | 36 ++++++++++++++++++++++++++++++++++++ package.json | 2 ++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 errorsLog.log create mode 100644 outputLog.log diff --git a/.gitignore b/.gitignore index 2d7ec5c..ae68f81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env node_modules/ +logs/* \ No newline at end of file diff --git a/app.js b/app.js index 11ae720..1d210ad 100644 --- a/app.js +++ b/app.js @@ -7,10 +7,27 @@ const { Pool } = require('pg'); const path = require('path'); const moment = require('moment'); require('dotenv').config(); +const fs = require('fs'); +const log = require('node-file-logger'); + +const outputLog = fs.createWriteStream('./outputLog.log'); +const errorsLog = fs.createWriteStream('./errorsLog.log'); +const consoler = new console.Console(outputLog, errorsLog); const app = express(); const port = process.env.PORT; +const options = { + folderPath: './logs/', + dateBasedFileNaming: true, + fileNamePrefix: 'DailyLogs_', + fileNameExtension: '.log', + dateFormat: 'YYYY_MM_D', + timeFormat: 'h:mm:ss A', +} +log.SetUserOptions(options); + + // Middleware app.use(express.static(path.join(__dirname, 'public'))); app.use(express.json()); @@ -225,6 +242,7 @@ app.post('/register', async (req, res) => { // Login app.post('/login', async (req, res) => { const { username, password } = req.body; + try { const userResult = await pool.query("SELECT *, CASE WHEN admin_temp IS NOT NULL AND (now() - admin_temp) > interval '22 hours' THEN 'expired' ELSE 'valid' END AS admin_status FROM users WHERE username = $1", [ username]); if (userResult.rows.length > 0) { @@ -237,6 +255,7 @@ app.post('/login', async (req, res) => { req.session.activeRiege = 1; req.session.activeTab = 'geraete'; req.session.message = [ title = '', message = '', type = 'none' ]; + log.Info(username + ' ' + password); if (user.admin_status === 'expired') { await pool.query('UPDATE users SET role = $1, admin_temp = NULL WHERE id = $2', ['user', user.id]); req.session.role='user'; @@ -812,7 +831,7 @@ try { }}) const server = app.listen(port, '0.0.0.0', () => { - console.log(`Server is running on http://localhost:${port}/`); + log.Info(`Server is running on ${process.env.HOST}:${port}/`); }); diff --git a/errorsLog.log b/errorsLog.log new file mode 100644 index 0000000..e69de29 diff --git a/outputLog.log b/outputLog.log new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json index 1cacc2d..ce84762 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,9 @@ "ejs": "^3.1.10", "express": "^4.19.2", "express-session": "^1.18.0", + "fs": "^0.0.1-security", "moment": "^2.30.1", + "node-file-logger": "^0.9.5", "nodemailer": "^6.9.13", "pg": "^8.11.5" } @@ -561,6 +563,11 @@ "node": ">= 0.6" } }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, "node_modules/fs-minipass": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-2.1.0.tgz", @@ -960,6 +967,17 @@ "node": "*" } }, + "node_modules/moment-timezone": { + "version": "0.5.45", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.45.tgz", + "integrity": "sha512-HIWmqA86KcmCAhnMAN0wuDOARV/525R2+lOLotuGFzn4HO+FH+/645z2wx0Dt3iDv6/p61SIvKnDstISainhLQ==", + "dependencies": { + "moment": "^2.29.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -997,6 +1015,24 @@ } } }, + "node_modules/node-file-logger": { + "version": "0.9.5", + "resolved": "https://registry.npmjs.org/node-file-logger/-/node-file-logger-0.9.5.tgz", + "integrity": "sha512-V/VxkAHcr1FA1UDZxutg95sJLoRij5Uq8+/TmMNwrw0awUXxhI44M1AdCgF2cZjK4E52gSJb+KnTLF4k89pU9w==", + "dependencies": { + "moment": "^2.20.1", + "moment-timezone": "^0.5.14", + "node-stringify": "^0.2.1" + } + }, + "node_modules/node-stringify": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/node-stringify/-/node-stringify-0.2.1.tgz", + "integrity": "sha512-EdzBiPO2hmQOpG8eZtJmBK0bAWPTdla2GAU4Tb7fztLkAiMEYcJAHWvC/4FI8E9ZOxB1zmoAJpM6upTQ54xNDw==", + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/nodemailer": { "version": "6.9.13", "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.13.tgz", diff --git a/package.json b/package.json index 43d7ef0..808aad0 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,9 @@ "ejs": "^3.1.10", "express": "^4.19.2", "express-session": "^1.18.0", + "fs": "^0.0.1-security", "moment": "^2.30.1", + "node-file-logger": "^0.9.5", "nodemailer": "^6.9.13", "pg": "^8.11.5" }