Class Extlib::Logger
In: lib/extlib/logger.rb
Parent: Object
Hash SimpleSet ::String ByteArray StandardError InvalidResourceError Logger Pool lib/extlib/logger.rb lib/extlib/byte_array.rb lib/extlib/simple_set.rb Inflection ClassMethods Hook Assertions lib/extlib/pooling.rb Pooling Extlib dot/m_31_0.png

Methods

<<   close   flush   initialize_log   new   push   set_log  

Constants

Levels = { :fatal => 7, :error => 6, :warn => 4, :info => 3, :debug => 0  

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

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

Public Class methods

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.

[Source]

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

Public Instance methods

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<String>:The message to be logged. Defaults to nil.

Returns

String:The resulting message added to the log file.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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)

Alias for #<<

Replaces an existing logger with a new one.

Parameters

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.

[Source]

     # 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

Readies a log for writing.

Parameters

log<IO, String>:Either an IO object or a name of a logfile.

[Source]

    # 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

[Validate]