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.



5042
5043
5044
5045
# File 'lib/QuickBaseClient.rb', line 5042

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

Instance Attribute Details

#appendObject (readonly)

Returns the value of attribute append.



5040
5041
5042
# File 'lib/QuickBaseClient.rb', line 5040

def append
  @append
end

#fileObject (readonly)

Returns the value of attribute file.



5040
5041
5042
# File 'lib/QuickBaseClient.rb', line 5040

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



5040
5041
5042
# File 'lib/QuickBaseClient.rb', line 5040

def filename
  @filename
end

Instance Method Details

#changeLogFile(filename, append = true) ⇒ Object



5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
# File 'lib/QuickBaseClient.rb', line 5057

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



5047
5048
5049
5050
5051
5052
5053
5054
5055
# File 'lib/QuickBaseClient.rb', line 5047

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

#getTimeStringObject



5111
5112
5113
5114
# File 'lib/QuickBaseClient.rb', line 5111

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

#logRequest(dbidForRequestURL, api_Request, requestXML) ⇒ Object



5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
# File 'lib/QuickBaseClient.rb', line 5073

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



5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
# File 'lib/QuickBaseClient.rb', line 5091

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