improve logging
This commit is contained in:
parent
9f2cbf2bce
commit
ce5f5380e1
2 changed files with 36 additions and 22 deletions
|
@ -9,6 +9,8 @@ const packageJson = require('../package.json');
|
||||||
class WingbaseServer extends ServerInterface {
|
class WingbaseServer extends ServerInterface {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
process.on('SIGINT', this.stop.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
|
@ -18,12 +20,13 @@ class WingbaseServer extends ServerInterface {
|
||||||
this.webServer.start();
|
this.webServer.start();
|
||||||
this.gameServer.start();
|
this.gameServer.start();
|
||||||
|
|
||||||
this.log(`Wingbase version ${packageJson.version} running.`);
|
this.log(`Wingbase version ${packageJson.version} running.`, 'bold');
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
this.log('Server stopping.');
|
this.log('Server stopping.', 'bold');
|
||||||
process.exit();
|
this.capLogfile();
|
||||||
|
setTimeout(process.exit, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,13 @@
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const pad = (str, len, right) => {
|
||||||
|
str = '' + str;
|
||||||
|
return (right ? str : '') +
|
||||||
|
Array(len > str.length ? 1 + len - str.length : 0)
|
||||||
|
.join('0') + (right ? '' : str);
|
||||||
|
};
|
||||||
|
|
||||||
require('colors');
|
require('colors');
|
||||||
|
|
||||||
class ServerInterface {
|
class ServerInterface {
|
||||||
|
@ -10,30 +17,13 @@ class ServerInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
log(msg) {
|
log(msg) {
|
||||||
let pad = (str, len, right) => {
|
let timestamp = this.timestamp;
|
||||||
str = '' + str;
|
|
||||||
return (right ? str : '') +
|
|
||||||
Array(len > str.length ? 1 + len - str.length : 0)
|
|
||||||
.join('0') + (right ? '' : str);
|
|
||||||
}
|
|
||||||
|
|
||||||
let d = new Date();
|
|
||||||
let timestamp =
|
|
||||||
`<${pad(d.getUTCHours(), 2)}:` +
|
|
||||||
`${pad(d.getUTCMinutes(), 2)}:` +
|
|
||||||
`${pad(d.getUTCSeconds(), 2)}.` +
|
|
||||||
`${pad(('' + d.getUTCMilliseconds()).slice(0, 2), 2, true)}> `;
|
|
||||||
let output = msg;
|
let output = msg;
|
||||||
Array.from(arguments).splice(1).forEach(a => output = output[a]);
|
Array.from(arguments).splice(1).forEach(a => output = output[a]);
|
||||||
output = timestamp.gray + output;
|
output = timestamp.gray + output;
|
||||||
// Clear and go to start of line.
|
// Clear and go to start of line.
|
||||||
console.log('\x1b[2K\x1b[999D' + output);
|
console.log('\x1b[2K\x1b[999D' + output);
|
||||||
|
fs.appendFile(this.logfile, timestamp + msg + '\n');
|
||||||
let date =
|
|
||||||
`${pad(d.getUTCFullYear(), 2)}-` +
|
|
||||||
`${pad(d.getUTCMonth(), 2)}-` +
|
|
||||||
`${pad(d.getUTCDate(), 2)}`;
|
|
||||||
fs.appendFile('log/' + date + '.log', timestamp + msg + '\n');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(msg) {
|
debug(msg) {
|
||||||
|
@ -43,6 +33,27 @@ class ServerInterface {
|
||||||
error(msg) {
|
error(msg) {
|
||||||
this.log(msg, 'red');
|
this.log(msg, 'red');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
capLogfile() {
|
||||||
|
fs.appendFile(this.logfile, '-----------\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
get timestamp() {
|
||||||
|
let d = new Date();
|
||||||
|
return `<${pad(d.getUTCHours(), 2)}:` +
|
||||||
|
`${pad(d.getUTCMinutes(), 2)}:` +
|
||||||
|
`${pad(d.getUTCSeconds(), 2)}.` +
|
||||||
|
`${pad(('' + d.getUTCMilliseconds()).slice(0, 2), 2, true)}> `;
|
||||||
|
}
|
||||||
|
|
||||||
|
get logfile() {
|
||||||
|
let d = new Date();
|
||||||
|
let date =
|
||||||
|
`${pad(d.getUTCFullYear(), 2)}-` +
|
||||||
|
`${pad(d.getUTCMonth(), 2)}-` +
|
||||||
|
`${pad(d.getUTCDate(), 2)}`;
|
||||||
|
return 'log/' + date + '.log';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ServerInterface;
|
module.exports = ServerInterface;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue