From 703fff9c138fde1492a91167bd57176466cc2be3 Mon Sep 17 00:00:00 2001 From: Susritha Date: Wed, 9 Oct 2024 17:11:23 +0530 Subject: [PATCH] Added application logs with morgan --- .gitignore | 3 +- app.js | 72 ++++++++++++++----------- package-lock.json | 130 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + 4 files changed, 175 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 1dcef2d..e14d388 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -.env \ No newline at end of file +.env +logs \ No newline at end of file diff --git a/app.js b/app.js index 8fe77ac..2e0a983 100644 --- a/app.js +++ b/app.js @@ -1,50 +1,60 @@ -require('dotenv').config(); +require("dotenv").config(); -const express = require('express'); -const expressLayout = require('express-ejs-layouts'); -const methodOverride = require('method-override'); -const cookieParser = require('cookie-parser'); -const session = require('express-session'); -const MongoStore = require('connect-mongo'); +const express = require("express"); +const expressLayout = require("express-ejs-layouts"); +const methodOverride = require("method-override"); +const cookieParser = require("cookie-parser"); +const session = require("express-session"); +const MongoStore = require("connect-mongo"); +const rfs = require("rotating-file-stream"); -const connectDB = require('./server/config/db'); -const { isActiveRoute } = require('./server/helpers/routeHelpers'); +const connectDB = require("./server/config/db"); +const { isActiveRoute } = require("./server/helpers/routeHelpers"); +const morgan = require("morgan"); const app = express(); const PORT = process.env.PORT || 5000; - + +const accessLogStream = rfs.createStream("application.log", { + interval: "1d", + path: "./logs", +}); + +app.use(morgan("combined", { stream: accessLogStream })); + // Connect to DB connectDB(); app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(cookieParser()); -app.use(methodOverride('_method')); - -app.use(session({ - secret: 'keyboard cat', - resave: false, - saveUninitialized: true, - store: MongoStore.create({ - mongoUrl: process.env.MONGODB_URI - }), - //cookie: { maxAge: new Date ( Date.now() + (3600000) ) } -})); - -app.use(express.static('public')); +app.use(morgan("dev")); +app.use(methodOverride("_method")); + +app.use( + session({ + secret: "keyboard cat", + resave: false, + saveUninitialized: true, + store: MongoStore.create({ + mongoUrl: process.env.MONGODB_URI, + }), + //cookie: { maxAge: new Date ( Date.now() + (3600000) ) } + }) +); + +app.use(express.static("public")); // Templating Engine app.use(expressLayout); -app.set('layout', './layouts/main'); -app.set('view engine', 'ejs'); - - -app.locals.isActiveRoute = isActiveRoute; +app.set("layout", "./layouts/main"); +app.set("view engine", "ejs"); +app.locals.isActiveRoute = isActiveRoute; -app.use('/', require('./server/routes/main')); -app.use('/', require('./server/routes/admin')); +app.use("/", require("./server/routes/main")); +app.use("/", require("./server/routes/admin")); -app.listen(PORT, ()=> { +app.listen(PORT, () => { console.log(`App listening on port ${PORT}`); }); diff --git a/package-lock.json b/package-lock.json index aeda0e6..802dce5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,8 @@ "method-override": "^3.0.0", "mongodb": "^6.6.2", "mongoose": "^7.0.3", + "morgan": "^1.10.0", + "rotating-file-stream": "^3.2.5", "zod": "^3.23.8" }, "devDependencies": { @@ -179,6 +181,24 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "node_modules/basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "5.1.2" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/basic-auth/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/bcrypt": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-5.1.0.tgz", @@ -1412,6 +1432,49 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" }, + "node_modules/morgan": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", + "integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==", + "license": "MIT", + "dependencies": { + "basic-auth": "~2.0.1", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-finished": "~2.3.0", + "on-headers": "~1.0.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/morgan/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/morgan/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, + "node_modules/morgan/node_modules/on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/mpath": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", @@ -1773,6 +1836,18 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/rotating-file-stream": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/rotating-file-stream/-/rotating-file-stream-3.2.5.tgz", + "integrity": "sha512-T8iBxUA4SookMTU97cIHUPck7beLOvN4g+y4db9E2eLn54OFsdp4qMnxuqmmJ05lcQHzueEVnPRykxfnPG948g==", + "license": "MIT", + "engines": { + "node": ">=14.0" + }, + "funding": { + "url": "https://www.blockchain.com/btc/address/12p1p5q7sK75tPyuesZmssiMYr4TKzpSCN" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -2322,6 +2397,21 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "requires": { + "safe-buffer": "5.1.2" + }, + "dependencies": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, "bcrypt": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-5.1.0.tgz", @@ -3243,6 +3333,41 @@ } } }, + "morgan": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", + "integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==", + "requires": { + "basic-auth": "~2.0.1", + "debug": "2.6.9", + "depd": "~2.0.0", + "on-finished": "~2.3.0", + "on-headers": "~1.0.2" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" + }, + "on-finished": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", + "integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==", + "requires": { + "ee-first": "1.1.1" + } + } + } + }, "mpath": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", @@ -3503,6 +3628,11 @@ "glob": "^7.1.3" } }, + "rotating-file-stream": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/rotating-file-stream/-/rotating-file-stream-3.2.5.tgz", + "integrity": "sha512-T8iBxUA4SookMTU97cIHUPck7beLOvN4g+y4db9E2eLn54OFsdp4qMnxuqmmJ05lcQHzueEVnPRykxfnPG948g==" + }, "safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", diff --git a/package.json b/package.json index d93ce72..09c3ff2 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,8 @@ "method-override": "^3.0.0", "mongodb": "^6.6.2", "mongoose": "^7.0.3", + "morgan": "^1.10.0", + "rotating-file-stream": "^3.2.5", "zod": "^3.23.8" }, "devDependencies": {