Class: QuickBase::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/QuickBaseClient.rb

Overview

To log QuickBase requests and responses to a file, make an instance of this class and call Client.setLogger( loggerInstance ). Call Client.setLogger( nil ) to turn logging off. The log file is written in CSV format to make it importable by QuickBase.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, append = true) ⇒ Logger

Returns a new instance of Logger.



4741
4742
4743
4744
# File 'lib/QuickBaseClient.rb', line 4741

def initialize( filename, append = true )
   @requestEntryNum = @responseEntryNum = 0
   changeLogFile( filename, append )
end

Instance Attribute Details

#appendObject (readonly)

Returns the value of attribute append.



4739
4740
4741
# File 'lib/QuickBaseClient.rb', line 4739

def append
  @append
end

#fileObject (readonly)

Returns the value of attribute file.



4739
4740
4741
# File 'lib/QuickBaseClient.rb', line 4739

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



4739
4740
4741
# File 'lib/QuickBaseClient.rb', line 4739

def filename
  @filename
end

Instance Method Details

#changeLogFile(filename, append = true) ⇒ Object



4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
# File 'lib/QuickBaseClient.rb', line 4756

def changeLogFile( filename, append = true )
   if @file and @filename and @filename != filename
      closeLogFile()
   end
   begin
      @append = append
      skipHeader = (@append == true and FileTest.exist?( filename ))
      @file = File.open( filename, @append ? "a" : "w" )
      @filename = filename
      @file.print( "entry,request time,dbid,api,request,response time, response" ) unless skipHeader
   rescue StandardError => e
      closeLogFile()
      puts "Logger error: #{e}"
   end
end

#closeLogFileObject



4746
4747
4748
4749
4750
4751
4752
4753
4754
# File 'lib/QuickBaseClient.rb', line 4746

def closeLogFile()
   if @file
      @file.close
      @file = nil
      @filename = nil
      @append = true
      @requestEntryNum = @responseEntryNum = 0
   end
end

#getTimeStringObject



4810
4811
4812
4813
# File 'lib/QuickBaseClient.rb', line 4810

def getTimeString()
   t = Time.now
   t.strftime( "%Y-%m-%d-%H-%M-%S" )
end

#logRequest(dbidForRequestURL, api_Request, requestXML) ⇒ Object



4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
# File 'lib/QuickBaseClient.rb', line 4772

def logRequest( dbidForRequestURL, api_Request, requestXML )
   if @file
      @requestEntryNum += 1
      entry =  "\n#{@requestEntryNum},"
      entry << "#{getTimeString()},"
      entry << "#{dbidForRequestURL},"
      entry << "#{api_Request},"

      maxChars = requestXML.length > 256 ? 256 : requestXML.length

      request = requestXML[0,maxChars]
      request.gsub!( ",", "_" )
      entry << "#{request}"

      @file.print( entry )
   end
end

#logResponse(error, responseXML) ⇒ Object



4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
# File 'lib/QuickBaseClient.rb', line 4790

def logResponse( error, responseXML )
   if @file
      @responseEntryNum += 1

      if @responseEntryNum != @requestEntryNum
         entry =  "\n#{@responseEntryNum},,,,#{getTimeString()},"
      else
         entry =  ",#{getTimeString()},"
      end

      maxChars = responseXML.length > 256 ? 256 : responseXML.length

      response = responseXML[0,maxChars]
      response.gsub!( ",", "_" )
      entry << "#{response}"

      @file.print( entry )
   end
end