Implement Winston powered logger

This commit is contained in:
Cryptkeeper 2015-11-01 23:46:24 -06:00
parent eb75acf754
commit dec1536c6c
5 changed files with 36 additions and 14 deletions

16
app.js

@ -1,5 +1,7 @@
var server = require('./lib/server'); var server = require('./lib/server');
var ping = require('./lib/ping'); var ping = require('./lib/ping');
var logger = require('./lib/logger');
var config = require('./config.json'); var config = require('./config.json');
var networkHistory = []; var networkHistory = [];
@ -15,9 +17,7 @@ setInterval(function() {
ping.ping(network.ip, network.port || 25565, network.type, 2500, function(err, result) { ping.ping(network.ip, network.port || 25565, network.type, 2500, function(err, result) {
// Handle our ping results, if it succeeded. // Handle our ping results, if it succeeded.
if (err) { if (err) {
console.log('Failed to ping ' + network.ip + ': ' + err); logger.log('error', 'Failed to ping ' + network.ip + ': ' + err);
} else {
console.log(network.ip + ' reply: ' + result.players.online + '/' + result.players.max);
} }
server.io.sockets.emit('update', result); server.io.sockets.emit('update', result);
@ -48,18 +48,14 @@ setInterval(function() {
} }
}, 2500); }, 2500);
setInterval(function() {
console.log('Connected clients: %d', connectedClients);
}, 1000);
server.start(function() { server.start(function() {
// Track how many people are currently connected. // Track how many people are currently connected.
server.io.on('connect', function(client) { server.io.on('connect', function(client) {
console.log('Incoming connection: %s', client.request.connection.remoteAddress);
// We're good to connect them! // We're good to connect them!
connectedClients += 1; connectedClients += 1;
logger.log('info', 'Accepted connection: %s, total clients: %d', client.request.connection.remoteAddress, connectedClients);
// Remap our associative array into just an array. // Remap our associative array into just an array.
var networkHistoryList = []; var networkHistoryList = [];
var networkHistoryKeys = Object.keys(networkHistory); var networkHistoryKeys = Object.keys(networkHistory);
@ -73,8 +69,6 @@ server.start(function() {
// Attach our listeners. // Attach our listeners.
client.on('disconnect', function(client) { client.on('disconnect', function(client) {
console.log('Dropped connection: %s', client.request.connection.remoteAddress);
connectedClients -= 1; connectedClients -= 1;
}); });
}); });

@ -4,4 +4,12 @@ $(document).ready(function() {
socket.on('connect', function() { socket.on('connect', function() {
}); });
socket.on('add', function(servers) {
});
socket.on('update', function(server) {
});
}); });

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 mime = require('mime');
var io = require('socket.io'); var io = require('socket.io');
var logger = require('./logger');
var config = require('../config.json'); var config = require('../config.json');
exports.start = function(callback) { exports.start = function(callback) {
@ -15,7 +17,7 @@ exports.start = function(callback) {
urlMapping[routeKeys[i]] = config.routes[routeKeys[i]]; 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. // Create our tiny little HTTP server.
var server = http.createServer(function(req, res) { var server = http.createServer(function(req, res) {
@ -41,7 +43,7 @@ exports.start = function(callback) {
exports.io = (io = io.listen(server)); exports.io = (io = io.listen(server));
// Since everything is loaded, do some final prep work. // 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(); callback();
}; };

@ -4,8 +4,9 @@
"description": "A Minecraft server tracker that lets you focus on the basics.", "description": "A Minecraft server tracker that lets you focus on the basics.",
"main": "app.js", "main": "app.js",
"dependencies": { "dependencies": {
"mime": "^1.3.4",
"socket.io": "^1.3.7", "socket.io": "^1.3.7",
"mime": "^1.3.4" "winston": "^2.0.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",