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() {
|
||||
Log(req, true);
|
||||
});
|
||||
stat.Inc(req.url);
|
||||
stat.LogStat(req.url);
|
||||
next();
|
||||
});
|
||||
app.use(express.static('public'));
|
||||
|
|
38
stat.js
38
stat.js
|
@ -19,7 +19,7 @@
|
|||
------------------------------------------------------------------------- */
|
||||
|
||||
module.exports = {
|
||||
Inc: Inc,
|
||||
LogStat: LogStat,
|
||||
Load: Load
|
||||
};
|
||||
|
||||
|
@ -27,9 +27,11 @@ var utils = require('./utils.js');
|
|||
var logger = require('./logger.js');
|
||||
|
||||
const statFile = "stats/stats";
|
||||
const vStatFile = "stats/vstats";
|
||||
const writeInterval = 10;
|
||||
|
||||
var data = {};
|
||||
var vData = [];
|
||||
var writes = 0;
|
||||
|
||||
function Load() {
|
||||
|
@ -40,6 +42,19 @@ function Load() {
|
|||
logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg"));
|
||||
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) {
|
||||
|
@ -51,16 +66,35 @@ function Inc(value) {
|
|||
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() {
|
||||
writes++;
|
||||
if (writes == writeInterval) {
|
||||
try {
|
||||
utils.WriteFile(JSON.stringify(data), statFile);
|
||||
writes = 0;
|
||||
// logger.Log("[STAT] Stats wrote.");
|
||||
} catch (e) {
|
||||
logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg"));
|
||||
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
|
||||
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