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.



4987
4988
4989
4990
# File 'lib/QuickBaseClient.rb', line 4987

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

Instance Attribute Details

#appendObject (readonly)

Returns the value of attribute append.



4985
4986
4987
# File 'lib/QuickBaseClient.rb', line 4985

def append
  @append
end

#fileObject (readonly)

Returns the value of attribute file.



4985
4986
4987
# File 'lib/QuickBaseClient.rb', line 4985

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



4985
4986
4987
# File 'lib/QuickBaseClient.rb', line 4985

def filename
  @filename
end

Instance Method Details

#changeLogFile(filename, append = true) ⇒ Object



5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
# File 'lib/QuickBaseClient.rb', line 5002

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



4992
4993
4994
4995
4996
4997
4998
4999
5000
# File 'lib/QuickBaseClient.rb', line 4992

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

#getTimeStringObject



5056
5057
5058
5059
# File 'lib/QuickBaseClient.rb', line 5056

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

#logRequest(dbidForRequestURL, api_Request, requestXML) ⇒ Object



5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
# File 'lib/QuickBaseClient.rb', line 5018

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



5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
# File 'lib/QuickBaseClient.rb', line 5036

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