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.



5022
5023
5024
5025
# File 'lib/QuickBaseClient.rb', line 5022

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

Instance Attribute Details

#appendObject (readonly)

Returns the value of attribute append.



5020
5021
5022
# File 'lib/QuickBaseClient.rb', line 5020

def append
  @append
end

#fileObject (readonly)

Returns the value of attribute file.



5020
5021
5022
# File 'lib/QuickBaseClient.rb', line 5020

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



5020
5021
5022
# File 'lib/QuickBaseClient.rb', line 5020

def filename
  @filename
end

Instance Method Details

#changeLogFile(filename, append = true) ⇒ Object



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

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



5027
5028
5029
5030
5031
5032
5033
5034
5035
# File 'lib/QuickBaseClient.rb', line 5027

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

#getTimeStringObject



5091
5092
5093
5094
# File 'lib/QuickBaseClient.rb', line 5091

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

#logRequest(dbidForRequestURL, api_Request, requestXML) ⇒ Object



5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
# File 'lib/QuickBaseClient.rb', line 5053

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



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

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