From 070defecb68b72ae4bb22b37b1a50718ad3486c4 Mon Sep 17 00:00:00 2001 From: mrfry Date: Mon, 22 Mar 2021 13:17:03 +0100 Subject: [PATCH] Logrotate redo --- src/server.ts | 33 ++++++++++++++++++++++++++++++--- src/utils/logger.ts | 36 +++++++++++++++++------------------- submodules/qmining-page | 2 +- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/server.ts b/src/server.ts index 4f51115..3ded398 100755 --- a/src/server.ts +++ b/src/server.ts @@ -237,10 +237,10 @@ function setLogTimer() { now.getDate() + 1, 0, 0, - 0 + 1 ) logger.DebugLog(`Next daily action: ${night}`, 'daily', 1) - const msToMidnight = night.getTime() - now.getTime() + const msToMidnight = night.getTime() - now.getTime() + 1000 logger.DebugLog(`msToMidnight: ${msToMidnight}`, 'daily', 1) logger.DebugLog(`Seconds To Midnight: ${msToMidnight / 1000}`, 'daily', 1) @@ -254,10 +254,38 @@ function setLogTimer() { setTimeout(function() { LogTimerAction() + rotateLog() setLogTimer() }, msToMidnight) } +function rotateLog() { + const date = new Date() + date.setDate(date.getDate() - 1) + const fname = + date.getFullYear() + + '-' + + ('0' + (date.getMonth() + 1)).slice(-2) + + '-' + + ('0' + date.getDate()).slice(-2) + + ' ' + + ('0' + date.getHours()).slice(-2) + + ':' + + ('0' + date.getMinutes()).slice(-2) + + ':' + + ('0' + date.getSeconds()).slice(-2) + + const logFile = logger.logDir + logger.logFileName + const vlogFile = logger.vlogDir + logger.logFileName + + if (utils.FileExists(logFile)) { + utils.CopyFile(logFile, logger.logDir + fname) + } + if (utils.FileExists(vlogFile)) { + utils.CopyFile(vlogFile, logger.vlogDir + fname) + } +} + function LogTimerAction() { logger.DebugLog(`Running Log Timer Action`, 'daily', 1) Object.keys(modules).forEach((key) => { @@ -280,7 +308,6 @@ function LogTimerAction() { const line = '===================================================================================================================================================' logger.Log(line) - logger.setNewLogfileName() } logger.Log('Node version: ' + process.version) diff --git a/src/utils/logger.ts b/src/utils/logger.ts index d51675c..888be56 100755 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -21,20 +21,6 @@ const hr = '---------------------------------------------------------------------------------' -export default { - getColoredDateString: getColoredDateString, - Log: Log, - DebugLog: DebugLog, - GetColor: GetColor, - LogReq: LogReq, - LogStat: LogStat, - Load: Load, - logHashed: logHashed, - hr: hr, - C: C, - setNewLogfileName: setNewLogfileName, -} - const DELIM = C('green') + '|' + C() // import express from 'express' @@ -50,7 +36,7 @@ const uvStatsFile = 'stats/uvstats' const nologFile = './nolog' const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan'] -let logFileName = utils.GetDateString(true) +const logFileName = 'log' const writeInterval = 10 const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0 @@ -68,10 +54,6 @@ let writes = 0 let noLogips = [] -function setNewLogfileName(): void { - logFileName = utils.GetDateString(true) -} - function getColoredDateString(): string { const date = new Date() const dateString = utils.GetDateString() @@ -418,3 +400,19 @@ function C(color?: string): string { } return '\x1b[0m' } + +export default { + getColoredDateString: getColoredDateString, + Log: Log, + DebugLog: DebugLog, + GetColor: GetColor, + LogReq: LogReq, + LogStat: LogStat, + Load: Load, + logHashed: logHashed, + hr: hr, + C: C, + logFileName: logFileName, + logDir: logDir, + vlogDir: vlogDir, +} diff --git a/submodules/qmining-page b/submodules/qmining-page index 4c4fb1d..e311b88 160000 --- a/submodules/qmining-page +++ b/submodules/qmining-page @@ -1 +1 @@ -Subproject commit 4c4fb1dacce3306de27407b501000851822394a4 +Subproject commit e311b88508f0c77d6b97a4273e57aa5d656a1a64