config & Logging
This commit is contained in:
4041
res/LegoLog Logo.ai
Normal file
4041
res/LegoLog Logo.ai
Normal file
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,11 @@ const dotenv = require('dotenv');
|
||||
function load() {
|
||||
Logger.info('Loading config...');
|
||||
const res = dotenv.config();
|
||||
Logger.debug('Config: result.parsed');
|
||||
Logger.debug(`Config: ${JSON.stringify(res.parsed)}`);
|
||||
Logger.debug(`Config: running in ${res.parsed.NODE_ENV}`);
|
||||
|
||||
// If NODE_ENV is dev, every config item that is dev is made into the actual one so that the
|
||||
// individual modules do not need to care about hte mode of operation
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -4,13 +4,24 @@ const clc = require('cli-color');
|
||||
const moment = require('moment');
|
||||
const fs = require('fs');
|
||||
|
||||
let LogLevel = 1;
|
||||
let LogLevel = 0;
|
||||
let logPath = 'logs.log';
|
||||
let dateFormat = 'DD-MM-YY HH:mm:ss'
|
||||
|
||||
// Must be ran after the config is initalised
|
||||
// TODO: network logs
|
||||
module.exports.init = async function(path) {
|
||||
if (path) logPath = path;
|
||||
|
||||
// ALWAYS logs to console, others are aditionals
|
||||
switch (process.env.LOG_TARGET)
|
||||
{
|
||||
case 'console':
|
||||
case 'file':
|
||||
case 'network':
|
||||
default:
|
||||
}
|
||||
|
||||
if (!fs.existsSync(logPath)) {
|
||||
fs.writeFileSync(logPath, '');
|
||||
}
|
||||
@@ -30,10 +41,6 @@ module.exports.DEBUG_LOGS = 1;
|
||||
module.exports.INFO_LOGS = 2;
|
||||
module.exports.WARN_LOGS = 3;
|
||||
|
||||
module.exports.welcome = function() {
|
||||
// Unused
|
||||
}
|
||||
|
||||
module.exports.middleware = function(origin, message) {
|
||||
let d = moment().format(dateFormat);
|
||||
fs.appendFileSync(logPath, `[${d.toLocaleString()}] [MIDDLEWARE: ${origin}] ${message} \n`);
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
const Logger = require('../logger.js');
|
||||
|
||||
const express = require('express');
|
||||
|
||||
function load() {
|
||||
Logger.info('Loading config...');
|
||||
dotenv.config();
|
||||
|
||||
}
|
||||
|
||||
function logRequest(req, res, next)
|
||||
{
|
||||
Logger.middleware('REQUEST', `${req.originalUrl} [${req.method}: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }]`);
|
||||
next();
|
||||
}
|
||||
|
||||
function listen(port) {
|
||||
const app = express();
|
||||
app.listen(port);
|
||||
Logger.info(`Listening on port ${port}...`);
|
||||
|
||||
Logger.info(`Setting up basic middleware...`);
|
||||
app.use(logRequest);
|
||||
|
||||
app.get('/', (req, res) => { res.end('lol') })
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user