Configuring JNI
Java Native Interface (JNI) is a standard programming interface for writing Java native methods and embedding the Java virtual machine into native applications.
The connector leverages the JNI for improved performance on Mac and Linux.
Configure the Config INI File
The Linux and Mac editions of the Salesforce python connector are configured with an ini file. This file is used to set several parameters, including JNI behavior. This file is to be located in:
{path_to_distribution_site-packages}/cdata/config.ini
Ensure that any configuration properties you set in the ini file fall under the following section name (adjust the 311 number if you are using an different python version from 3.11):
- For Linux:
[salesforce.cpython-311-x86_64-linux-gnu.so]
- For Mac:
[salesforce.cpython-311-darwin.so]
Configure the JNI connector's behavior by editing the properties in the connector's config.ini file. The connector can be configured as follows:
- LOGFILE: Set this the same way as the CDATA_LOGFILE envrionment variable below.
- JAVA_HOME: Configure the path to the JVM library location used to launch the JVM.
- CLASS_PATH: Use a colon-separated list to configure the paths to the third-party jar libraries.
Configure Environment Variables
Additionally, set the following environment variables:
- CDATA_JAVA_HOME: Configure the path to the JVM library location used to launch the JVM.
- CDATA_JVM_OPTIONS: Place JVM options here.
- CDATA_LOGFILE: Set this in the following scheme: <SCHEME>://<TAG>[|<LEVEL>]
- SCHEME: The options are STDOUT, FILE.
- STDOUT: Both the native wrapper and odbc core log into stdout. The Logfile and Verbosity properties can override the behavior of ODBC core.
- FILE: The native wrapper logs into <FILENAME> while the odbc core logs into <FILENAME>.driver.log. The Logfile and Verbosity properties can override the behavior of ODBC core.
- TAG
- For STDOUT, set this to 1. For FILE, set this to the filename.
- LEVEL
- Set to one of: FATAL | ERROR | WARNING | INFO | DEBUG
- SCHEME: The options are STDOUT, FILE.
The following are some examples of this syntax:
- STDOUT://1|DEBUG
- FILE:///tmp/my_py.log|DEBUG
Custom Logger
The Python connector supports a custom logging mechanism for redirecting log output to any destination, such as a cloud storage service or logging framework. Use setCustomLoggerFactory() to register a factory function that creates a logger instance for each connection.
The factory function receives the context string from the Logfile connection property (the portion after CUSTOM://) and must return an object with a writeLog(verbosity, message) method.
To enable custom logging:
- Call setCustomLoggerFactory() with your factory function before opening connections.
- Set Logfile to CUSTOM:// followed by a context string to identify the connection.
- Set Verbosity to the desired log level.
The following example demonstrates a custom logger factory that creates a separate logger instance per connection:
import cdata.salesforce as mod
import time
class MyLogger:
def __init__(self, loggerId):
self.loggerId = loggerId
def writeLog(self, verbosity, message):
print("[MyLogger " + self.loggerId + "] " + message)
def createLogger(context):
return MyLogger(context[len("MyLoggerId="):])
mod.setCustomLoggerFactory(createLogger)
conn1 = mod.connect("...;Logfile=CUSTOM://MyLoggerId=1;Verbosity=2;")
# do something with conn1
time.sleep(1) # Wait for logs to flush from conn1
conn2 = mod.connect("...;Logfile=CUSTOM://MyLoggerId=2;Verbosity=2;")
# do something with conn2
time.sleep(1) # Wait for logs to flush from conn2