Class | Extlib::Logger |
In: |
lib/extlib/logger.rb
|
Parent: | Object |
Levels | = | { :fatal => 7, :error => 6, :warn => 4, :info => 3, :debug => 0 | NotesRuby (standard) logger levels:
|
auto_flush | [RW] | |
buffer | [R] | |
delimiter | [RW] | |
init_args | [R] | |
level | [RW] | |
log | [R] |
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.
string<String>: | The message to be logged. Defaults to nil. |
String: | The resulting message added to the log file. |
# File lib/extlib/logger.rb, line 144 144: def <<(string = nil) 145: message = "" 146: message << delimiter 147: message << string if string 148: message << "\n" unless message[-1] == ?\n 149: @buffer << message 150: flush if @auto_flush 151: 152: message 153: end
Close and remove the current log object.
# File lib/extlib/logger.rb, line 130 130: def close 131: flush 132: @log.close if @log.respond_to?(:close) && !@log.tty? 133: @log = nil 134: end
Flush the entire buffer to the log object.
# File lib/extlib/logger.rb, line 124 124: def flush 125: return unless @buffer.size > 0 126: @log.write(@buffer.slice!(0..-1).join) 127: end
Replaces an existing logger with a new one.
log<IO, String>: | Either an IO object or a name of a logfile. |
log_level<~to_sym>: | The log level from, e.g. :fatal or :info. Defaults to :error in the production environment and :debug otherwise. |
delimiter<String>: | Delimiter to use between message sections. Defaults to " ~ ". |
auto_flush<Boolean>: | Whether the log should automatically flush after new messages are added. Defaults to false. |
# File lib/extlib/logger.rb, line 110 110: def set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false) 111: if log_level && Levels[log_level.to_sym] 112: @level = Levels[log_level.to_sym] 113: else 114: @level = Levels[:debug] 115: end 116: @buffer = [] 117: @delimiter = delimiter 118: @auto_flush = auto_flush 119: 120: initialize_log(log) 121: end
Readies a log for writing.
log<IO, String>: | Either an IO object or a name of a logfile. |
# File lib/extlib/logger.rb, line 71 71: def initialize_log(log) 72: close if @log # be sure that we don't leave open files laying around. 73: 74: if log.respond_to?(:write) 75: @log = log 76: elsif File.exist?(log) 77: @log = open(log, (File::WRONLY | File::APPEND)) 78: @log.sync = true 79: else 80: FileUtils.mkdir_p(File.dirname(log)) unless File.directory?(File.dirname(log)) 81: @log = open(log, (File::WRONLY | File::APPEND | File::CREAT)) 82: @log.sync = true 83: @log.write("#{Time.now.httpdate} #{delimiter} info #{delimiter} Logfile created\n") 84: end 85: end