Created
January 31, 2014 17:49
-
-
Save glennpratt/8738532 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
require 'syslog' | |
module Aq | |
# Clone the ::Syslog module so we can extend it. | |
Syslog = ::Syslog.dup | |
# Syslog for fields. No need to {::Syslog#open} it, we already did. | |
module Syslog | |
DEFAULT_OPTIONS = LOG_PID | ($stderr.isatty ? LOG_PERROR : 0) | |
DEFAULT_FACILITY = LOG_DAEMON | |
FORMAT_SPECIFIERS_MATCH = /[^%](%(?:\d+\$)?[+-]?(?:[ 0]|'.{1})?-?\d*(?:\.\d+)?[a-zA-Z])/ | |
# Open it with defaults now. Because we can. | |
self.open(File.basename($0), DEFAULT_OPTIONS, DEFAULT_FACILITY) | |
class << self | |
# Alias log so we can override it. | |
alias :orig_log :log | |
# Override ::Syslog#log to escape format_string when no format_args are | |
# passed. | |
def log(priority, format_string, *format_args) | |
escape_format_string!(format_string) if format_args.empty? | |
orig_log(priority, format_string, *format_args) | |
end | |
# Escape printf style format specifiers. | |
def escape_format_string!(format_string) | |
format_string.gsub!(FORMAT_SPECIFIERS_MATCH, '%\1') | |
end | |
# Override convenience methods here because they ::Syslog versions | |
# won't call our #log method. | |
def emerg(str, *args); log(::Syslog::LOG_EMERG, str, *args); end | |
def alert(str, *args); log(::Syslog::LOG_ALERT, str, *args); end | |
def crit(str, *args); log(::Syslog::LOG_CRIT, str, *args); end | |
def err(str, *args); log(::Syslog::LOG_ERR, str, *args); end | |
def warning(str, *args); log(::Syslog::LOG_WARNING, str, *args); end | |
def notice(str, *args); log(::Syslog::LOG_NOTICE, str, *args); end | |
def info(str, *args); log(::Syslog::LOG_INFO, str, *args); end | |
def debug(str, *args); log(::Syslog::LOG_DEBUG, str, *args); end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment