Last active
December 16, 2015 11:09
-
-
Save takaheraw/5425789 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> | |
<script> | |
var host = 'localhost'; | |
var port = 3030; | |
var url = 'ws://' + host + ':' + port + '/'; | |
var WebSocket = window.WebSocket || window.MozWebSocket; | |
var ws = new WebSocket(url); | |
$(function() { | |
var logs = $('#logs'); | |
var log = $('.log'); | |
ws.onopen = function () { | |
logs.prepend(url + 'へ接続しました'); | |
} | |
ws.onmessage = function (msg) { | |
var data = JSON.parse(msg.data); | |
logs.prepend('<div class="log">' + data + '</div'); | |
$('.log', logs).fadeIn(); | |
} | |
ws.onclose = function () { | |
logs.prepend('切断しました'); | |
} | |
}); | |
</script> | |
<h1>Apache-Logs Watcher</h1> | |
<div id="logs"></div> |
This file contains hidden or 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
var fs = require('fs'); | |
var exec = require('child_process').exec; | |
var WsServer = require('ws').Server; | |
var ws = new WsServer({port: 3030}); | |
var file = '/usr/local/Cellar/nginx/1.2.7/logs/access.log'; | |
ws.on('connection', function(socket) { | |
fs.watch(file, function(eventName, fileName) { | |
readAndSend(socket); | |
}); | |
}); | |
function readAndSend(socket) { | |
exec('tail -1 ' + file, function(err, stdout, stderr) { | |
if (err) throw err; | |
sendLog(socket, stdout); | |
}); | |
} | |
function sendLog(socket, data) { | |
if (socket.readyState === 1) | |
socket.send(JSON.stringify(data)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment