Feature: log to syslog #362

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+41 −0
Diff settings

Always

Just for now

View
@@ -4,6 +4,7 @@
require 'rubygems'
require 'fileutils'
require 'tempfile'
+require 'syslog'
require 'yaml'
require 'etc'
View
@@ -11,6 +11,7 @@ class << self
def message(string)
to_console loggify(string, :message, :green)
to_file loggify(string, :message)
+ to_syslog loggify(string, :message)
end
##
@@ -19,6 +20,7 @@ def message(string)
def error(string)
to_console loggify(string, :error, :red), true
to_file loggify(string, :error)
+ to_syslog loggify(string, :error), :err
end
##
@@ -28,19 +30,22 @@ def warn(string)
@has_warnings = true
to_console loggify(string, :warning, :yellow), true
to_file loggify(string, :warning)
+ to_syslog loggify(string, :warning), :warn
end
# Outputs the data as if it were a regular 'puts' command,
# but also logs it to the backup.log
def normal(string)
to_console loggify(string)
to_file loggify(string)
+ to_syslog loggify(string)
end
##
# Silently logs data to the log file
def silent(string)
to_file loggify(string, :silent)
+ to_syslog loggify(string, :silent)
end
##
@@ -119,6 +124,15 @@ def to_file(lines)
messages.push(*lines)
end
+ ##
+ # Receives an Array of Strings to be written to syslog.
+ def to_syslog(lines, level = :info)
+ Syslog.open('backup', Syslog::LOG_PID, Syslog::LOG_LOCAL0) do |s|
+ lines.each {|line| s.send(level,line) }
+ end
+ messages.push(*lines)
+ end
+
##
# Invokes the #colorize method with the provided string
# and the color code "32" (for green)
View
@@ -33,6 +33,11 @@
returns(:uncolored_regular_message)
subject.expects(:to_file).in_sequence(s).
with(:uncolored_regular_message)
+ subject.expects(:loggify).in_sequence(s).
+ with('regular message', :message).
+ returns(:uncolored_regular_message)
+ subject.expects(:to_syslog).in_sequence(s).
+ with(:uncolored_regular_message)
subject.message('regular message')
end
@@ -50,6 +55,11 @@
returns(:uncolored_error_message)
subject.expects(:to_file).in_sequence(s).
with(:uncolored_error_message)
+ subject.expects(:loggify).in_sequence(s).
+ with('error message', :error).
+ returns(:uncolored_error_message)
+ subject.expects(:to_syslog).in_sequence(s).
+ with(:uncolored_error_message, :err)
subject.error('error message')
end
@@ -67,13 +77,19 @@
returns(:uncolored_warning_message)
subject.expects(:to_file).in_sequence(s).
with(:uncolored_warning_message)
+ subject.expects(:loggify).in_sequence(s).
+ with('warning message', :warning).
+ returns(:uncolored_warning_message)
+ subject.expects(:to_syslog).in_sequence(s).
+ with(:uncolored_warning_message, :warn)
subject.warn('warning message')
end
it 'sets has_warnings? to true' do
subject.stubs(:to_console)
subject.stubs(:to_file)
+ subject.stubs(:to_syslog)
expect { subject.warn('warning') }.
to change{ subject.has_warnings? }.from(false).to(true)
end
@@ -91,6 +107,11 @@
returns(:unformatted_message)
subject.expects(:to_file).in_sequence(s).
with(:unformatted_message)
+ subject.expects(:loggify).in_sequence(s).
+ with('normal message').
+ returns(:unformatted_message)
+ subject.expects(:to_syslog).in_sequence(s).
+ with(:unformatted_message)
subject.normal('normal message')
end
@@ -104,6 +125,11 @@
returns(:silent_message)
subject.expects(:to_file).in_sequence(s).
with(:silent_message)
+ subject.expects(:loggify).in_sequence(s).
+ with('silent message', :silent).
+ returns(:silent_message)
+ subject.expects(:to_syslog).in_sequence(s).
+ with(:silent_message)
subject.silent('silent message')
end