summaryrefslogtreecommitdiff
path: root/lib/byteback/log.rb
diff options
context:
space:
mode:
authorMatthew Bloch <matthew@bytemark.co.uk>2014-10-31 02:43:35 +0000
committerMatthew Bloch <matthew@bytemark.co.uk>2014-10-31 02:43:35 +0000
commit1238c74fa01c009d7f76327f3beb30fee4b9f98f (patch)
tree2e0e0abde1b35f03ef515acc9ebd57f638af1c25 /lib/byteback/log.rb
parentf23c6c91e2e2a8eb4154ec545199a5ecbe5136a1 (diff)
Refactored to improve logging and reduce cut & paste code, bumped Debian version number.
Diffstat (limited to 'lib/byteback/log.rb')
-rwxr-xr-xlib/byteback/log.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/byteback/log.rb b/lib/byteback/log.rb
new file mode 100755
index 0000000..66b86ef
--- /dev/null
+++ b/lib/byteback/log.rb
@@ -0,0 +1,46 @@
+require 'logger'
+require 'syslog'
+
+module Byteback
+ # Translates Ruby's Logger calls to similar calls to Syslog
+ # (implemented in Ruby 2.0 as Syslog::Logger)
+ #
+ class SyslogProxy
+ class << self
+ def debug(*a); Syslog.log(Syslog::LOG_DEBUG, *a); end
+ def info(*a); Syslog.log(Syslog::LOG_INFO, *a); end
+ def warn(*a); Syslog.log(Syslog::LOG_WARN, *a); end
+ def error(*a); Syslog.log(Syslog::LOG_ERR, *a); end
+ def fatal(*a); Syslog.log(Syslog::LOG_EMERG, *a); end
+ end
+ end
+
+ # Log proxy class that we can include in our scripts for some simple
+ # logging defaults.
+ #
+ module Log
+ @@me = File.expand_path($0).split("/").last
+
+ @@logger = if STDIN.tty? && !ENV['BYTEBACK_TO_SYSLOG']
+ logger = Logger.new(STDERR)
+ logger.level = Logger::DEBUG
+ logger.formatter = proc { |severity, datetime, progname, msg|
+ if severity == "FATAL" || severity == "ERROR"
+ "*** #{msg}\n"
+ else
+ "#{msg}\n"
+ end
+ }
+ logger
+ else
+ Syslog.open(@@me)
+ SyslogProxy
+ end
+
+ def debug(*a); @@logger.__send__(:debug, *a); end
+ def info(*a); @@logger.__send__(:info, *a); end
+ def warn(*a); @@logger.__send__(:warn, *a); end
+ def fatal(*a); @@logger.__send__(:fatal, *a); exit 1; end
+ def error(*a); @@logger.__send__(:error, *a); end
+ end
+end