Additional Tools

Sending logs to Quickbase with QuickbaseLogHandler

It is often appropriate to send some logs to Quickbase, especially when writing scripts that interface with Quickbase. This package includes a log handler as part of Python standard logging, that will send logs in the background.

You supply it with a table client to the logs table, and the handler will send logs to that table in a non-blocking background thread.

By default, the class assumes the table has attributes for “when”, “level”, and “message”. You can supply a custom record factory via passing a function to with_record_factory, or by extending this class and overriding record_factory.

For example, say you have a QuickbaseTable class called MyLog. You can create a class like so:

 class InvocationLog(QuickbaseTable):
     __dbid__ = 'abcdef'
     __app__ = QuickbaseApp(app_id='aaappp', name='APP', realm_hostname='foo.quickbase.com')

     date_created = QuickbaseField(fid=1, field_type=Qb.DATETIME)
     date_modified = QuickbaseField(fid=2, field_type=Qb.DATETIME)
     recordid = QuickbaseField(fid=3, field_type=Qb.NUMERIC)
     record_owner = QuickbaseField(fid=4, field_type=Qb.USER)
     last_modified = QuickbaseField(fid=5, field_type=Qb.USER)

     log_message = QuickbaseField(fid=6, field_type=Qb.TEXT)
     logged_when = QuickbaseField(fid=7, field_type=Qb.DATETIME)


class MyLogHandler(QuickbaseLogHandler):
    def __init__(self):
        super().__init__(MyLog.client(my_user_token))

    def record_factory(self, record: logging.LogRecord):
        return MyLog(log_message=record.msg, logged_when=datetime.utcnow())


logger = logging.getLogger()
logger.addHandler(MyLogHandler())
logger.info('This message should show up in Quickbase!')

class quickbase_client.tools.QuickbaseLogHandler(logs_table_client: quickbase_client.client.table_client.QuickbaseTableClient)

Class for sending logs to a specific Quickbase table.

You supply it with a table client to the logs table, and the handler will send logs to that table in a non-blocking background thread.

This uses the higher-level QuickbaseTableClient APIs. So you will have to create a class for your table you want to send logs to.

emit(record)

Calls record_factory() and starts a thread to send it to Quickbase.

record_factory(record: logging.LogRecord)

Create a QuickbaseTable record object given a LogRecord. By default, this assumes the associated table, under the handlers table client, has properties when, level, and message.

Parameters

record – The logging.LogRecord.

Returns

A QuickbaseTable record object.

static with_record_factory(logs_table_client: quickbase_client.client.table_client.QuickbaseTableClient, record_factory)

Create a logger using a function to make records.

Parameters
  • logs_table_client – The QuickbaseTableClient for the logs to go to.

  • record_factory – A function which takes a logging.LogRecord and creates the relevant record (instance of a QuickbaseTable).