feat(log): using tagslog

This commit is contained in:
Alexey Kasyanchuk 2021-08-01 04:49:50 +03:00
parent f3e08b5aa4
commit fa112ffd9c
4 changed files with 31 additions and 63 deletions

26
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "rats-search", "name": "rats-search",
"version": "1.7.0", "version": "1.7.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "rats-search", "name": "rats-search",
"version": "1.7.0", "version": "1.7.1",
"hasInstallScript": true, "hasInstallScript": true,
"dependencies": { "dependencies": {
"ansi-256-colors": "^1.1.0", "ansi-256-colors": "^1.1.0",
@ -44,6 +44,7 @@
"react-minimal-pie-chart": "^8.2.0", "react-minimal-pie-chart": "^8.2.0",
"simple-encryptor": "github:DEgITx/node-simple-encryptor", "simple-encryptor": "github:DEgITx/node-simple-encryptor",
"stun": "1.1.0", "stun": "1.1.0",
"tagslog": "^1.1.2",
"webtorrent": "github:DEgITx/webtorrent" "webtorrent": "github:DEgITx/webtorrent"
}, },
"devDependencies": { "devDependencies": {
@ -14063,6 +14064,11 @@
"safer-buffer": "^2.0.2", "safer-buffer": "^2.0.2",
"tweetnacl": "~0.14.0" "tweetnacl": "~0.14.0"
}, },
"bin": {
"sshpk-conv": "bin/sshpk-conv",
"sshpk-sign": "bin/sshpk-sign",
"sshpk-verify": "bin/sshpk-verify"
},
"engines": { "engines": {
"node": ">=0.10.0" "node": ">=0.10.0"
} }
@ -18761,6 +18767,14 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/tagslog": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/tagslog/-/tagslog-1.1.2.tgz",
"integrity": "sha512-AstKis9gr4Ss0jpzJNhynPAXr85aevBsG0/FMRAwkedCYKATUhOjuSfP5Wm9Pwd58VocXWfBMYww/CEdKWnHgQ==",
"dependencies": {
"ansi-256-colors": "^1.1.0"
}
},
"node_modules/tapable": { "node_modules/tapable": {
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.0.tgz", "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.0.tgz",
@ -35763,6 +35777,14 @@
} }
} }
}, },
"tagslog": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/tagslog/-/tagslog-1.1.2.tgz",
"integrity": "sha512-AstKis9gr4Ss0jpzJNhynPAXr85aevBsG0/FMRAwkedCYKATUhOjuSfP5Wm9Pwd58VocXWfBMYww/CEdKWnHgQ==",
"requires": {
"ansi-256-colors": "^1.1.0"
}
},
"tapable": { "tapable": {
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.0.tgz", "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.0.tgz",

View File

@ -140,6 +140,7 @@
"react-minimal-pie-chart": "^8.2.0", "react-minimal-pie-chart": "^8.2.0",
"simple-encryptor": "github:DEgITx/node-simple-encryptor", "simple-encryptor": "github:DEgITx/node-simple-encryptor",
"stun": "1.1.0", "stun": "1.1.0",
"tagslog": "^1.1.2",
"webtorrent": "github:DEgITx/webtorrent" "webtorrent": "github:DEgITx/webtorrent"
}, },
"devDependencies": { "devDependencies": {

View File

@ -75,39 +75,12 @@ const util = require('util');
if (!fs.existsSync(app.getPath("userData"))){ if (!fs.existsSync(app.getPath("userData"))){
fs.mkdirSync(app.getPath("userData")); fs.mkdirSync(app.getPath("userData"));
} }
const logFile = fs.createWriteStream(app.getPath("userData") + '/rats.log', {flags : 'w'});
const logStdout = process.stdout;
const colors = require('ansi-256-colors'); require('tagslog')({
const stringHashCode = (str) => { logFile: app.getPath("userData") + '/rats.log',
let hash = 0, i, chr; stdout: (log) => process.stdout.write(log + '\n'),
if (str.length === 0) overrideConsole: true
return hash; });
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
console.log = (...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`[${date}] ` + util.format(...d) + '\n');
logStdout.write(util.format(...d) + '\n');
};
global.logT = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`[${date}] [${type}] ` + util.format(...d) + '\n');
logStdout.write(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + util.format(...d) + '\n');
}
global.logTE = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`\n[${date}] [ERROR] [${type}] ` + util.format(...d) + '\n\n');
logStdout.write(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + colors.fg.codes[9] + util.format(...d) + colors.reset + '\n');
}
// print os info // print os info
logT('system', 'Rats', app.getVersion()) logT('system', 'Rats', app.getVersion())

View File

@ -11,38 +11,10 @@ const io = require('socket.io')(server);
const fs = require('fs'); const fs = require('fs');
const path = require('path') const path = require('path')
const os = require('os') const os = require('os')
require('tagslog')({logFile: 'rats.log'});
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const util = require('util');
const colors = require('ansi-256-colors');
const stringHashCode = (str) => {
let hash = 0, i, chr;
if (str.length === 0)
return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
const logFile = fs.createWriteStream('rats.log', {flags : 'w'});
global.logT = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
console.log(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + util.format(...d));
logFile.write(`[${date}] ` + util.format(...d) + '\n');
}
global.logTE = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
console.log(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + colors.fg.codes[9] + util.format(...d) + colors.reset + '\n');
logFile.write(`[${date}] ` + util.format(...d) + '\n');
}
server.listen(appConfig.httpPort); server.listen(appConfig.httpPort);
logT('system', 'Rats v' + packageJson.version) logT('system', 'Rats v' + packageJson.version)
logT('system', 'Listening web server on', appConfig.httpPort, 'port') logT('system', 'Listening web server on', appConfig.httpPort, 'port')