Created
February 22, 2012 13:25
-
-
Save dhoffend/1885169 to your computer and use it in GitHub Desktop.
Simple NodeJS Logfile Streamer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect('http://<server>:3000'); | |
socket.on('log', function (data) { | |
$('#log').prepend($('<li></li>').text(data.line)); | |
$('#log li:gt(50)').remove(); | |
}); | |
</script> | |
<style> | |
#log, #log li { | |
list-style-type: none; | |
font-size: 11px; | |
font-family: 'Courier New', monospace; | |
padding: 0; | |
margin: 0; | |
white-space: pre; | |
} | |
#header { | |
font-size: 11px; | |
font-family: 'Courier New', monospace; | |
font-weight: bold; | |
white-space: pre; | |
margin-bottom: 5px; | |
border-bottom: 1px solid black; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="header">INPUT HEADER HERE</div> | |
<ul id="log"> | |
</ul> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************* require ******************************/ | |
var app = require('http').createServer(handler) | |
, io = require('socket.io').listen(app) | |
, fs = require('fs') | |
, spawn = require('child_process').spawn; | |
/************************* logfile check ******************************/ | |
var filename = process.argv[2]; | |
if (!filename) | |
return console.log("Usage: node <server.js> <filename>"); | |
/************************* http server ******************************/ | |
app.listen(3000); | |
function handler (req, res) { | |
fs.readFile(__dirname + '/index.html', | |
function (err, data) { | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading index.html'); | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
} | |
/************************* socket.io ******************************/ | |
io.set('log level', 1); | |
/************************* tail -f stream ******************************/ | |
var tail = spawn("tail", ["-F", "-s 0.2", filename]); | |
tail.stdout.on("data", function (data) { | |
io.sockets.emit('log',{line: ''+data}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment