Very basic example of logging
Playing about with ruby
This script checks if it is being run as root. If it is not then the script exits and logs a FATAL error...
#!/usr/bin/ruby
# This library is standard in ruby 1.8.x require 'logger' def do_at_exit(message,exit_code,log) at_exit { log.fatal(message) if exit_code == 1 } Process.exit(exit_code) end log = Logger.new(STDOUT) log.level = Logger::WARN do_at_exit("This script must be run as root.\n",1,log) if Process.uid != 0
Here I'm logging to standard out however this can easily be changed to a file by doing something like:
log = Logger.new('./test.log')
Example output when logging to STDOUT: F, [2006-06-13T15:12:48.124511 #25802] FATAL -- : This script must be run as root.
Example output when logging to a file: # Logfile created on Tue Jun 13 15:14:07 JST 2006 by logger.rb/1.5.2.4 F, [2006-06-13T15:14:07.936568 #25826] FATAL -- : This script must be run as root.
For more info on the Logger class (logging utility) go here
Regards, Bawdo.
tags: Ruby
Tue 13 Jun 2006, 15:03
0 comments