Implement Winston powered logger
This commit is contained in:
parent
eb75acf754
commit
dec1536c6c
16
app.js
16
app.js
@ -1,5 +1,7 @@
|
||||
var server = require('./lib/server');
|
||||
var ping = require('./lib/ping');
|
||||
var logger = require('./lib/logger');
|
||||
|
||||
var config = require('./config.json');
|
||||
|
||||
var networkHistory = [];
|
||||
@ -15,9 +17,7 @@ setInterval(function() {
|
||||
ping.ping(network.ip, network.port || 25565, network.type, 2500, function(err, result) {
|
||||
// Handle our ping results, if it succeeded.
|
||||
if (err) {
|
||||
console.log('Failed to ping ' + network.ip + ': ' + err);
|
||||
} else {
|
||||
console.log(network.ip + ' reply: ' + result.players.online + '/' + result.players.max);
|
||||
logger.log('error', 'Failed to ping ' + network.ip + ': ' + err);
|
||||
}
|
||||
|
||||
server.io.sockets.emit('update', result);
|
||||
@ -48,18 +48,14 @@ setInterval(function() {
|
||||
}
|
||||
}, 2500);
|
||||
|
||||
setInterval(function() {
|
||||
console.log('Connected clients: %d', connectedClients);
|
||||
}, 1000);
|
||||
|
||||
server.start(function() {
|
||||
// Track how many people are currently connected.
|
||||
server.io.on('connect', function(client) {
|
||||
console.log('Incoming connection: %s', client.request.connection.remoteAddress);
|
||||
|
||||
// We're good to connect them!
|
||||
connectedClients += 1;
|
||||
|
||||
logger.log('info', 'Accepted connection: %s, total clients: %d', client.request.connection.remoteAddress, connectedClients);
|
||||
|
||||
// Remap our associative array into just an array.
|
||||
var networkHistoryList = [];
|
||||
var networkHistoryKeys = Object.keys(networkHistory);
|
||||
@ -73,8 +69,6 @@ server.start(function() {
|
||||
|
||||
// Attach our listeners.
|
||||
client.on('disconnect', function(client) {
|
||||
console.log('Dropped connection: %s', client.request.connection.remoteAddress);
|
||||
|
||||
connectedClients -= 1;
|
||||
});
|
||||
});
|
||||
|
@ -4,4 +4,12 @@ $(document).ready(function() {
|
||||
socket.on('connect', function() {
|
||||
|
||||
});
|
||||
|
||||
socket.on('add', function(servers) {
|
||||
|
||||
});
|
||||
|
||||
socket.on('update', function(server) {
|
||||
|
||||
});
|
||||
});
|
17
lib/logger.js
Normal file
17
lib/logger.js
Normal file
@ -0,0 +1,17 @@
|
||||
var winston = require('winston');
|
||||
|
||||
winston.remove(winston.transports.Console);
|
||||
|
||||
winston.add(winston.transports.File, {
|
||||
filename: 'minetrack.log'
|
||||
});
|
||||
winston.add(winston.transports.Console, {
|
||||
'timestamp': function() {
|
||||
var date = new Date();
|
||||
|
||||
return date.toLocaleTimeString() + " " + date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear().toString().substring(2, 4);
|
||||
},
|
||||
'colorize': true
|
||||
});
|
||||
|
||||
module.exports = winston;
|
@ -4,6 +4,8 @@ var url = require('url');
|
||||
var mime = require('mime');
|
||||
var io = require('socket.io');
|
||||
|
||||
var logger = require('./logger');
|
||||
|
||||
var config = require('../config.json');
|
||||
|
||||
exports.start = function(callback) {
|
||||
@ -15,7 +17,7 @@ exports.start = function(callback) {
|
||||
urlMapping[routeKeys[i]] = config.routes[routeKeys[i]];
|
||||
}
|
||||
|
||||
console.log(Object.keys(config.routes));
|
||||
logger.log('info', Object.keys(config.routes));
|
||||
|
||||
// Create our tiny little HTTP server.
|
||||
var server = http.createServer(function(req, res) {
|
||||
@ -41,7 +43,7 @@ exports.start = function(callback) {
|
||||
exports.io = (io = io.listen(server));
|
||||
|
||||
// Since everything is loaded, do some final prep work.
|
||||
console.log('Started on %s:%d', config.site.ip, config.site.port);
|
||||
logger.log('info', 'Started on %s:%d', config.site.ip, config.site.port);
|
||||
|
||||
callback();
|
||||
};
|
@ -4,8 +4,9 @@
|
||||
"description": "A Minecraft server tracker that lets you focus on the basics.",
|
||||
"main": "app.js",
|
||||
"dependencies": {
|
||||
"mime": "^1.3.4",
|
||||
"socket.io": "^1.3.7",
|
||||
"mime": "^1.3.4"
|
||||
"winston": "^2.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
Loading…
Reference in New Issue
Block a user