logger.rb
rbmud-0.2/lib/logger.rb · 846 B
From the archive RbMUD-0.2.tar.bz2
Download the archive « Back to the archive contents
#!/usr/bin/env ruby
# $Revision: 1.3 $
# $Date: 2003/12/04 22:18:34 $
# $Author: jefus $
class Logger
def Logger.log(line)
time = Time.new.strftime("[%H:%M:%S]")
puts("#{time} #{line}")
# the logger stores log files in logs/current-date/
date = Time.new.strftime("%m-%d-%y")
# ensure the directories exist
begin
Dir.mkdir("logs")
rescue
end
begin
Dir.mkdir("logs/#{date}")
rescue
end
# now we can log it
begin
log = File.open("logs/#{date}/mud.log", "w+") # append mode
# for simplicity, we also keep one big log file in the main dir
mainLog = File.open("mud.log", "a+")
log.puts("#{time} #{line}")
mainLog.puts("#{time} #{line}")
rescue
puts("Error writing to log file: #{$!}")
ensure
log.close if log
mainLog.close if mainLog
end
end
end
Logger.log("Log code initialized.")