lib/ztk/logger.rb in ztk-0.2.4 vs lib/ztk/logger.rb in ztk-0.2.5
- old
+ new
@@ -54,11 +54,16 @@
def logdev
self.instance_variable_get(:@logdev).instance_variable_get(:@dev)
end
+ def shift(severity, shift=0, &block)
+ severity = ZTK::Logger.const_get(severity.to_s.upcase)
+ add(severity, nil, nil, shift, &block)
+ end
+
private
# Parses caller entries, extracting the file, line number and method.
#
# @param [String] at Entry from the caller Array.
@@ -79,20 +84,21 @@
# @param [Constant] severity Log level severity.
# @param [String] message Optional message to prefix the log entry with.
# @param [String] progname Optional name of the program to prefix the log
# entry with.
# @yieldreturn [String] The block should return the desired log message.
- def add(severity, message = nil, progname = nil, &block)
+ def add(severity, message=nil, progname=nil, shift=0, &block)
return if (@level > severity)
msg = (block && block.call)
(msg.nil? || msg.strip.empty?) and return
@hostname ||= %x(hostname -s).chomp.strip
- called_by = "#{@hostname}:#{parse_caller(caller[1])}"
+ called_by = "#{@hostname}:#{parse_caller(caller[1+shift])}"
message = [message, progname, msg].flatten.compact.join(": ")
message = "%19s.%06d|%05d|%5s|%60s%s\n" % [Time.now.utc.strftime("%Y-%m-%d|%H:%M:%S"), Time.now.utc.usec, Process.pid, SEVERITIES[severity], called_by, message]
@logdev.write(message)
+ @logdev.respond_to?(:flush) and @logdev.flush
true
end
# Sets the log level.