From 5e45b6acc7e443c27c9598205e4b2bf82797bcb2 Mon Sep 17 00:00:00 2001 From: galaxine-senpai Date: Mon, 29 Jan 2024 12:50:27 -0800 Subject: [PATCH] New update Changed how logging works Log now creates new file for each day it's started (still clears it if it exists) Should now get :remote-addr if nothing is in the CF header --- .gitignore | 4 +++- index.js | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d3bf09d..ce91d69 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ certs/* *.key private/* *.log -*.env \ No newline at end of file +*.env +robots.txt +sitemap.xml \ No newline at end of file diff --git a/index.js b/index.js index 36fc288..129f65d 100644 --- a/index.js +++ b/index.js @@ -12,26 +12,34 @@ const { red, blue, green, yellow, cyan, magenta, white, black, reset } = require // log all morgan logs to a file const { open, createWriteStream } = require('fs'); +let todays_date = moment().format('DD-MM-YYYY'); // create the file access.log in /logs/ -open(__dirname + '/logs/access.log', 'w', (err, file) => { +open(__dirname + `/logs/${todays_date}.log`, 'w', (err, file) => { if (err) { console.log(err); } + // IF file does exist, do nothing + if (file) { + console.log(`${yellow}Log file exists, skipping${reset}`); + } + console.log(`${green}Log file created${reset}`); }); morgan.token('date', (req, res, format) => { return moment().format('ddd, DD MMM YYYY HH:mm:ss ZZ'); // Set to the systems timezone (hopefully) }); +const getIP = ':req[CF-connecting-ip]' || ':remote-addr'; + // create a write stream (in append mode) -const accessLogStream = createWriteStream(__dirname + '/logs/access.log', { flags: 'a' }); +const accessLogStream = createWriteStream(__dirname + `/logs/${todays_date}.log`, { flags: 'a' }); // Yellow is info, green is religated to status codes -app.use(morgan(`:date | :method :url | :req[CF-connecting-ip] - :status - :response-time ms | :req[cf-ipcountry]`, { stream: accessLogStream })); +app.use(morgan(`:date | :method :url | ${getIP} - :status - :response-time ms | :req[cf-ipcountry]`, { stream: accessLogStream })); // Now print it to console w colors -app.use(morgan(`${blue}:date[web]${reset} | :method :url | :req[CF-connecting-ip] - ${yellow}(':status')} - :response-time ms${reset} | :req[cf-ipcountry]`)); +app.use(morgan(`${blue}:date[web]${reset} | :method :url | ${getIP} - ${yellow}(':status')} - :response-time ms${reset} | :req[cf-ipcountry]`)); // Paths to define for later use idfk const css = express.static('/src/css'); @@ -45,7 +53,7 @@ const port = const status = process.env.STATUS || 'DEVELOPMENT'; if (status == 'DEVELOPMENT') { - console.log('Running in development mode.'); + console.log(`${yellow}Running in development mode.${reset}`); } if (status == 'PRODUCTION') { // TODO: Add production settings & admin panel @@ -96,5 +104,5 @@ if (os.platform() === 'linux') { // start the server app.listen(port, () => { - console.log(`Server started on port ${port}`); + console.log(`${green}Server started on port ${yellow}${port}${reset}`); });