Parent

Included Modules

Class Index [+]

Quicksearch

Ramaze::Logger::RotatingInformer

A logger that rotates log files based on the current date. Log files are named after the date on which they were created. If the date changes a new log file is used.

In order to use this logger you’ll have to specify a base directory for all log files. This directory will not be created for you so make sure it exists. Loading the class can be done as following:

    logger = Ramaze::Logger::RotatingInformer.new('./log')

This creates a new instance that uses the directory ``./log`` for all it’s log files.

The default log format is ``%Y-%m-%d.log``. If you want to change this you can specify an alternative format (including the extension) as the secondary parameter of the ``.new()`` method:

    logger = Ramaze::Logger::RotatingInformer.new('./log', '%d-%m-%Y.log')

In this case the instance will use the date format ``dd-mm-yyyy`` along with the ``.log`` extension.

Besides the date format you can also customize the timestamp format as well as the format of each logged messages. Both these are set in a trait. The timestamp format is located in the trait ``:timestamp`` while the message format is stored in the ``:format`` trait. These can be set as following:

    logger = Ramaze::Logger::RotatingInformer.new('./log')

    logger.trait[:timestamp] = '...'
    logger.trait[:format]    = '...'

When setting the ``:format`` trait you can use 3 tags that will be replaced by their corresponding values. These are the following tags:

Attributes

time_format[RW]
log_levels[RW]
base_dir[R]

Public Class Methods

new(base_dir, time_format = '%Y-%m-%d.log', log_levels = [:debug, :error, :info, :warn]) click to toggle source

Create a new instance of RotatingInformer.

base_dir is the directory where all log files will be stored

time_format is the time format used to name the log files. Possible formats are identical to those accepted by Time.strftime

log_levelse is an array describing what kind of messages that the log receives. The array may contain any or all of the symbols :debug, :error, :info and/or :warn

@example

  # Creates logs in directory called logs. The generated filenames
  # will be in the form YYYY-MM-DD.log
  RotatingInformer.new('logs')

  # Creates logs in directory called logs. The generated filenames
  # will be in the form YYYY-MM.txt
  RotatingInformer.new('logs', '%Y-%m.txt')

  # Creates logs in directory called logs. The generated filenames
  # will be in the form YYYY-MM.txt.
  # Only errors will be logged to the files.
  RotatingInformer.new('logs', '%Y-%m.txt', [:error])

@param [String] base_dir The base directory for all the log files. @param [String] time_format The time format for all log files. @param [Array] log_levels Array containing the type of messages to log.

    # File lib/ramaze/log/rotatinginformer.rb, line 87
87:       def initialize(base_dir, time_format = '%Y-%m-%d.log',
88:       log_levels = [:debug, :error, :info, :warn])
89:         # Verify and set base directory
90:         send :base_dir=, base_dir, true
91: 
92:         @time_format = time_format
93:         @log_levels  = log_levels
94: 
95:         # Keep track of log shutdown (to prevent StackErrors due to recursion)
96:         @in_shutdown = false
97:       end

Public Instance Methods

base_dir=(directory, raise_exception = false) click to toggle source

Set the base directory for log files

If this method is called with the raise_exception parameter set to true the method will raise an exception if the specified directory does not exist or is unwritable.

If raise_exception is set to false, the method will just silently fail if the specified directory does not exist or is unwritable.

@param [String] directory The base directory specified by the developer. @param [Bool] raise_exception Boolean that indicates if an exception

 should be raised if the base directory doesn't exist.
     # File lib/ramaze/log/rotatinginformer.rb, line 113
113:       def base_dir=(directory, raise_exception = false)
114:         # Expand directory path
115:         base_dir = File.expand_path(directory)
116:         # Verify that directory path exists
117:         if File.exist?(base_dir)
118:           # Verify that directory path is a directory
119:           if File.directory?(base_dir)
120:             # Verify that directory path is writable
121:             if File.writable?(base_dir)
122:               @base_dir = base_dir
123:             else
124:               raise Exception.new("#{base_dir} is not writable") if raise_exception
125:             end
126:           else
127:             raise Exception.new("#{base_dir} is not a directory") if raise_exception
128:           end
129:         else
130:           raise Exception.new("#{base_dir} does not exist.") if raise_exception
131:         end
132:       end
closed?() click to toggle source

Is ``@out`` closed?

@return [TrueClass|FalseClass]

     # File lib/ramaze/log/rotatinginformer.rb, line 204
204:       def closed?
205:         @out.respond_to?(:closed?) && @out.closed?
206:       end
log(tag, *messages) click to toggle source

Integration to Logging.

@param [String] tag The type of message we’re logging. @param [Array] messages An array of messages to log.

     # File lib/ramaze/log/rotatinginformer.rb, line 154
154:       def log(tag, *messages)
155:         return unless @log_levels.include?(tag)
156: 
157:         # Update current log
158:         update_current_log
159: 
160:         messages.flatten!
161: 
162:         prefix = tag.to_s.upcase.ljust(5)
163: 
164:         messages.each do |message|
165:           @out.puts(log_interpolate(prefix, message))
166:         end
167: 
168:         @out.flush if @out.respond_to?(:flush)
169:       end
log_interpolate(prefix, text, time = timestamp) click to toggle source

Takes the prefix (tag), text and timestamp and applies it to the :format trait.

@param [String] prefix @param [String] text @param [Integer] time

     # File lib/ramaze/log/rotatinginformer.rb, line 179
179:       def log_interpolate(prefix, text, time = timestamp)
180:         message = class_trait[:format].dup
181: 
182:         vars = { '%time' => time, '%prefix' => prefix, '%text' => text }
183:         vars.each{|from, to| message.gsub!(from, to) }
184: 
185:         message
186:       end
shutdown() click to toggle source

Close the file we log to if it isn’t closed already.

     # File lib/ramaze/log/rotatinginformer.rb, line 137
137:       def shutdown
138:         if @out.respond_to?(:close)
139:           unless @in_shutdown
140:             @in_shutdown = true
141:             Log.debug("close, #{@out.inspect}")
142:             @in_shutdown = false
143:           end
144:           @out.close
145:         end
146:       end
timestamp() click to toggle source

This uses timestamp trait or a date in the format of ``%Y-%m-%d %H:%M:%S``

@return [String]

     # File lib/ramaze/log/rotatinginformer.rb, line 194
194:       def timestamp
195:         mask = class_trait[:timestamp]
196:         Time.now.strftime(mask || "%Y-%m-%d %H:%M:%S")
197:       end
write(message) click to toggle source

Method that is called by Rack::CommonLogger when logging data to a file.

@author Yorick Peterse @param [String] message The data that has to be logged.

     # File lib/ramaze/log/rotatinginformer.rb, line 214
214:       def write(message)
215:         log(:info, message)
216:       end

Private Instance Methods

update_current_log() click to toggle source

Checks whether current filename is still valid. If not, update the current log to point at the new filename.

     # File lib/ramaze/log/rotatinginformer.rb, line 224
224:       def update_current_log
225:         out = File.join(@base_dir, Time.now.strftime(@time_format))
226: 
227:         if @out.nil? || @out.path != out
228:           # Close old log if necessary
229:           shutdown unless @out.nil? || closed?
230: 
231:           # Start new log
232:           @out = File.open(out, 'ab+')
233:         end
234:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.