Appends a message to the log. The methods yield to an optional block and the output of this block will be appended to the message.
The message to be logged. Defaults to nil.
The resulting message added to the log file.
# File lib/extlib/logger.rb, line 144 def <<(string = nil) message = "" message << delimiter message << string if string message << "\n" unless message[-1] == \n @buffer << message flush if @auto_flush message end
Close and remove the current log object.
# File lib/extlib/logger.rb, line 130 def close flush @log.close if @log.respond_to?(:close) && !@log.tty? @log = nil end
Flush the entire buffer to the log object.
# File lib/extlib/logger.rb, line 124 def flush return unless @buffer.size > 0 @log.write(@buffer.slice!(0..-1).join) end
Replaces an existing logger with a new one.
Either an IO object or a name of a logfile.
The log level from, e.g. :fatal or :info. Defaults to :error in the production environment and :debug otherwise.
Delimiter to use between message sections. Defaults to “ ~ ”.
Whether the log should automatically flush after new messages are added. Defaults to false.
# File lib/extlib/logger.rb, line 110 def set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false) if log_level && Levels[log_level.to_sym] @level = Levels[log_level.to_sym] else @level = Levels[:debug] end @buffer = [] @delimiter = delimiter @auto_flush = auto_flush initialize_log(log) end
Readies a log for writing.
Either an IO object or a name of a logfile.
# File lib/extlib/logger.rb, line 71 def initialize_log(log) close if @log # be sure that we don't leave open files laying around. if log.respond_to?(:write) @log = log elsif File.exist?(log) @log = open(log, (File::WRONLY | File::APPEND)) @log.sync = true else FileUtils.mkdir_p(File.dirname(log)) unless File.directory?(File.dirname(log)) @log = open(log, (File::WRONLY | File::APPEND | File::CREAT)) @log.sync = true @log.write("#{Time.now.httpdate} #{delimiter} info #{delimiter} Logfile created\n") end end