Parent

Extlib::Logger

Constants

Levels

Notes

Ruby (standard) logger levels:

:fatal

An unhandleable error that results in a program crash

:error

A handleable error condition

:warn

A warning

:info

generic (useful) information about system operation

:debug

low-level information for developers

Attributes

level[RW]
delimiter[RW]
auto_flush[RW]
buffer[R]
log[R]
init_args[R]

Public Class Methods

new(*args) click to toggle source

To initialize the logger you create a new object, proxies to set_log.

Parameters

*args

Arguments to create the log from. See set_logs for specifics.

    # File lib/extlib/logger.rb, line 93
93:     def initialize(*args)
94:       @init_args = args
95:       set_log(*args)
96:     end

Public Instance Methods

<<(string = nil) click to toggle source

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.

Parameters

string

The message to be logged. Defaults to nil.

Returns

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
Also aliased as: push
close() click to toggle source

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() click to toggle source

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
push(string = nil) click to toggle source
Alias for: <<
set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false) click to toggle source

Replaces an existing logger with a new one.

Parameters

log

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

Delimiter to use between message sections. Defaults to “ ~ “.

auto_flush

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

Private Instance Methods

initialize_log(log) click to toggle source

Readies a log for writing.

Parameters

log

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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.