From dec1536c6ca70556d312bc0822f7150fb498c4a6 Mon Sep 17 00:00:00 2001 From: Cryptkeeper Date: Sun, 1 Nov 2015 23:46:24 -0600 Subject: [PATCH] Implement Winston powered logger --- app.js | 16 +++++----------- assets/js/site.js | 8 ++++++++ lib/logger.js | 17 +++++++++++++++++ lib/server.js | 6 ++++-- package.json | 3 ++- 5 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 lib/logger.js diff --git a/app.js b/app.js index cb357cc..98cd0e6 100644 --- a/app.js +++ b/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; }); }); diff --git a/assets/js/site.js b/assets/js/site.js index b35d6e0..f07fee8 100644 --- a/assets/js/site.js +++ b/assets/js/site.js @@ -4,4 +4,12 @@ $(document).ready(function() { socket.on('connect', function() { }); + + socket.on('add', function(servers) { + + }); + + socket.on('update', function(server) { + + }); }); \ No newline at end of file diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..0c01c20 --- /dev/null +++ b/lib/logger.js @@ -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; \ No newline at end of file diff --git a/lib/server.js b/lib/server.js index 1f11f2c..3e2434d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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(); }; \ No newline at end of file diff --git a/package.json b/package.json index bcfcd85..56363bd 100644 --- a/package.json +++ b/package.json @@ -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",