Skip to content

Instantly share code, notes, and snippets.

@Najaf

Najaf/basic-logging.rb

Last active Aug 29, 2015
Embed
What would you like to do?
#! /usr/bin/env ruby
require 'socket'
def log(source, message)
time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
puts "[%s] %s => %s" % [time, source, message]
end
server = TCPServer.new ARGV.first.to_i
loop do
client = server.accept
info = client.remote_address
source = "%s:%s" % [info.ip_address, info.ip_port]
log source, "Connection established"
data = client.readline
log source, "Client transmitted %d bytes" % data.bytes.count
client.close
log source, "Connection terminated"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment