Sha256: aab635098b05c32bee02a2fd1ae819938ceba604dad8abce9bed41af20aff3b3

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

# :nodoc:
# Version:: $Id: fileoutputter.rb,v 1.3 2010/02/03 05:15:35 colbygk Exp $

require "log4r/outputter/iooutputter"
require "log4r/staticlogger"

module Log4r

  # Convenience wrapper for File. Additional hash arguments are:
  #
  # [<tt>:filename</tt>]   Name of the file to log to.
  # [<tt>:trunc</tt>]      Truncate the file?
  class FileOutputter < IOOutputter
    attr_reader :trunc, :filename

    def initialize(_name, hash={})
      super(_name, nil, hash)

      @trunc = Log4rTools.decode_bool(hash, :trunc, false)
      _filename = (hash[:filename] or hash['filename'])
      @create = Log4rTools.decode_bool(hash, :create, true)

      if _filename.class != String
        raise TypeError, "Argument 'filename' must be a String", caller
      end

      # file validation
      if FileTest.exist?( _filename )
        if not FileTest.file?( _filename )
          raise StandardError, "'#{_filename}' is not a regular file", caller
        elsif not FileTest.writable?( _filename )
          raise StandardError, "'#{_filename}' is not writable!", caller
        end
      else # ensure directory is writable
        dir = File.dirname( _filename )
        if not FileTest.writable?( dir )
          raise StandardError, "'#{dir}' is not writable!"
        end
      end

      @filename = _filename
      if ( @create == true ) then
	@out = File.new(@filename, (@trunc ? "w" : "a")) 
	Logger.log_internal {
	  "FileOutputter '#{@name}' writing to #{@filename}"
	}
      else
	Logger.log_internal {
	  "FileOutputter '#{@name}' called with :create == false, #{@filename}"
	}
      end
    end

  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
log4r-1.1.7 src/log4r/outputter/fileoutputter.rb
log4r-1.1.6 src/log4r/outputter/fileoutputter.rb
log4r-1.1.5 src/log4r/outputter/fileoutputter.rb