Class: Ramaze::Logger::RotatingInformer

### Home Classes Methods

### In Files

- lib/ramaze/log/rotatinginformer.rb

### Parent

Object

### Methods

- ::new
- #base_dir=
- #closed?
- #log
- #log_interpolate
- #shutdown
- #timestamp
- #write

### Included Modules

- Innate::Traited
- Ramaze::Logging

### Files

- Gemfile
- Rakefile

### Class/Module Index

Quicksearch

- Ramaze
- Ramaze::App
- Ramaze::Bin
- Ramaze::Bin::Create
- Ramaze::Bin::Runner
- Ramaze::Cache
- Ramaze::Cache::LRU
- Ramaze::Cache::LocalMemCache
- Ramaze::Cache::MemCache
- Ramaze::Cache::Redis
- Ramaze::Cache::Sequel
- Ramaze::Controller
- Ramaze::CoreExtensions
- Ramaze::CoreExtensions::Object
- Ramaze::CoreExtensions::String
- Ramaze::Current
- Ramaze::DefaultController
- Ramaze::Files
- Ramaze::Gestalt
- Ramaze::Helper
- Ramaze::Helper::Auth
- Ramaze::Helper::Bench
- Ramaze::Helper::BlueForm
- Ramaze::Helper::BlueForm::Form
- Ramaze::Helper::CSRF
- Ramaze::Helper::Cache
- Ramaze::Helper::Cache::SingletonMethods
- Ramaze::Helper::Email
- Ramaze::Helper::Erector
- Ramaze::Helper::Flash
- Ramaze::Helper::Formatting
- Ramaze::Helper::Gestalt
- Ramaze::Helper::Gravatar
- Ramaze::Helper::Identity
- Ramaze::Helper::Layout
- Ramaze::Helper::Layout::SingletonMethods
- Ramaze::Helper::Link
- Ramaze::Helper::Localize
- Ramaze::Helper::Localize::Dictionary
- Ramaze::Helper::Localize::Parser
- Ramaze::Helper::Markaby
- Ramaze::Helper::Maruku
- Ramaze::Helper::Paginate
- Ramaze::Helper::Paginate::Paginator
- Ramaze::Helper::Paginate::Paginator::ArrayPager
- Ramaze::Helper::Paginate::Paginator::DataMapperPager
- Ramaze::Helper::Remarkably
- Ramaze::Helper::RequestAccessor
- Ramaze::Helper::SendFile
- Ramaze::Helper::SimpleCaptcha
- Ramaze::Helper::Stack
- Ramaze::Helper::Thread
- Ramaze::Helper::Upload
- Ramaze::Helper::Upload::ClassMethods
- Ramaze::Helper::Upload::UploadedFile
- Ramaze::Helper::UserHelper
- Ramaze::Helper::UserHelper::Wrapper
- Ramaze::Helper::XHTML
- Ramaze::LRUHash
- Ramaze::Logger
- Ramaze::Logger::Informer
- Ramaze::Logger::LogHub
- Ramaze::Logger::Logger
- Ramaze::Logger::RotatingInformer
- Ramaze::Logger::Syslog
- Ramaze::Logging
- Ramaze::Reloader
- Ramaze::Reloader::Hooks
- Ramaze::Reloader::WatchInotify
- Ramaze::Reloader::WatchStat
- Ramaze::Request
- Ramaze::Response
- Ramaze::SpecLogger
- Ramaze::View
- Ramaze::View::Erector
- Ramaze::View::Erubis
- Ramaze::View::Ezamar
- Ramaze::View::Gestalt
- Ramaze::View::Haml
- Ramaze::View::Liquid
- Ramaze::View::Liquid::Tag
- Ramaze::View::Lokar
- Ramaze::View::Mustache
- Ramaze::View::Mustache::RamazeContext
- Ramaze::View::Nagoro
- Ramaze::View::Remarkably
- Ramaze::View::Sass
- Ramaze::View::Slippers
- Ramaze::View::Tagz
- Ramaze::View::Tagz::Methods
- Ramaze::View::Tenjin
- Innate
- Innate::View
- Innate::View::Slim
- Nagoro
- Nagoro::Pipe
- Nagoro::Pipe::RenderPartial
- Bacon
- Bacon::PrettyOutput
- Erector
- Erector::Widget
- Rack
- Rack::RouteExceptions
- Tenjin
- Tenjin::Template
- BlankSlate
- Controller
- MainController
- Object

No matching classes.

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

- "%time": will be replaced by the current time.

- "%prefix": the log level such as "ERROR" or "INFO".

- "%text": the actual log message.

### Attributes

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

### 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
    def initialize(base_dir, time_format = '%Y-%m-%d.log',
    log_levels = [:debug, :error, :info, :warn])
      # Verify and set base directory
      send :base_dir=, base_dir, true

      @time_format = time_format
      @log_levels  = log_levels

      # Keep track of log shutdown (to prevent StackErrors due to recursion)
      @in_shutdown = false
    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
    def base_dir=(directory, raise_exception = false)
      # Expand directory path
      base_dir = File.expand_path(directory)
      # Verify that directory path exists
      if File.exist?(base_dir)
        # Verify that directory path is a directory
        if File.directory?(base_dir)
          # Verify that directory path is writable
          if File.writable?(base_dir)
            @base_dir = base_dir
          else
            raise Exception.new("#{base_dir} is not writable") if raise_exception
          end
        else
          raise Exception.new("#{base_dir} is not a directory") if raise_exception
        end
      else
        raise Exception.new("#{base_dir} does not exist.") if raise_exception
      end
    end

closed?() click to toggle source

Is "@out" closed?

@return [TrueClass|FalseClass]

    # File lib/ramaze/log/rotatinginformer.rb, line 204
    def closed?
      @out.respond_to?(:closed?) && @out.closed?
    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
    def log(tag, *messages)
      return unless @log_levels.include?(tag)

      # Update current log
      update_current_log

      messages.flatten!

      prefix = tag.to_s.upcase.ljust(5)

      messages.each do |message|
        @out.puts(log_interpolate(prefix, message))
      end

      @out.flush if @out.respond_to?(:flush)
    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
    def log_interpolate(prefix, text, time = timestamp)
      message = class_trait[:format].dup

      vars = { '%time' => time, '%prefix' => prefix, '%text' => text }
      vars.each{|from, to| message.gsub!(from, to) }

      message
    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
    def shutdown
      if @out.respond_to?(:close)
        unless @in_shutdown
          @in_shutdown = true
          Log.debug("close, #{@out.inspect}")
          @in_shutdown = false
        end
        @out.close
      end
    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
    def timestamp
      mask = class_trait[:timestamp]
      Time.now.strftime(mask || "%Y-%m-%d %H:%M:%S")
    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
    def write(message)
      log(:info, message)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.