mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Logging changes
This commit is contained in:
parent
11a6150d3a
commit
c2f3f24e51
3 changed files with 41 additions and 3 deletions
|
@ -69,7 +69,7 @@ app.use(function(req, res, next) {
|
||||||
res.on('finish', function() {
|
res.on('finish', function() {
|
||||||
Log(req, true);
|
Log(req, true);
|
||||||
});
|
});
|
||||||
stat.Inc(req.url);
|
stat.LogStat(req.url);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
app.use(express.static('public'));
|
app.use(express.static('public'));
|
||||||
|
|
38
stat.js
38
stat.js
|
@ -19,7 +19,7 @@
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Inc: Inc,
|
LogStat: LogStat,
|
||||||
Load: Load
|
Load: Load
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -27,9 +27,11 @@ var utils = require('./utils.js');
|
||||||
var logger = require('./logger.js');
|
var logger = require('./logger.js');
|
||||||
|
|
||||||
const statFile = "stats/stats";
|
const statFile = "stats/stats";
|
||||||
|
const vStatFile = "stats/vstats";
|
||||||
const writeInterval = 10;
|
const writeInterval = 10;
|
||||||
|
|
||||||
var data = {};
|
var data = {};
|
||||||
|
var vData = [];
|
||||||
var writes = 0;
|
var writes = 0;
|
||||||
|
|
||||||
function Load() {
|
function Load() {
|
||||||
|
@ -40,6 +42,19 @@ function Load() {
|
||||||
logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg"));
|
logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg"));
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
var prevVData = utils.ReadFile(vStatFile);
|
||||||
|
vData = JSON.parse(prevVData);
|
||||||
|
} catch (e) {
|
||||||
|
logger.Log("[STAT]: Error at loading visit logs!", logger.GetColor("redbg"));
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function LogStat(url) {
|
||||||
|
Inc(url);
|
||||||
|
AddVisitStat(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
function Inc(value) {
|
function Inc(value) {
|
||||||
|
@ -51,16 +66,35 @@ function Inc(value) {
|
||||||
Save();
|
Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function AddVisitStat(name) {
|
||||||
|
var m = new Date();
|
||||||
|
const now = m.getFullYear() + "/" +
|
||||||
|
("0" + (m.getMonth() + 1)).slice(-2) + "/" +
|
||||||
|
("0" + m.getDate()).slice(-2);
|
||||||
|
if (vData[now] == undefined)
|
||||||
|
vData[now] = [];
|
||||||
|
if (vData[now][name] == undefined)
|
||||||
|
vData[now][name] = 0;
|
||||||
|
vData[now][name]++;
|
||||||
|
}
|
||||||
|
|
||||||
function Save() {
|
function Save() {
|
||||||
writes++;
|
writes++;
|
||||||
if (writes == writeInterval) {
|
if (writes == writeInterval) {
|
||||||
try {
|
try {
|
||||||
utils.WriteFile(JSON.stringify(data), statFile);
|
utils.WriteFile(JSON.stringify(data), statFile);
|
||||||
writes = 0;
|
|
||||||
// logger.Log("[STAT] Stats wrote.");
|
// logger.Log("[STAT] Stats wrote.");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg"));
|
logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg"));
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
utils.WriteFile(JSON.stringify(vData), vStatFile);
|
||||||
|
// logger.Log("[STAT] Stats wrote.");
|
||||||
|
} catch (e) {
|
||||||
|
logger.Log("[STAT]: Error at writing visit logs!", logger.GetColor("redbg"));
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
writes = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
todos
4
todos
|
@ -1,2 +1,6 @@
|
||||||
make init function that
|
make init function that
|
||||||
creates directory and file structure
|
creates directory and file structure
|
||||||
|
redirects, for more stats (ex. to greasyfork: /script
|
||||||
|
like - dislike button
|
||||||
|
static public links above textboxes
|
||||||
|
public questions allow question deletion
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue