Other Features

Forced Termination of Processes

The Session class provides a static method enableJobCancellation() to enable forced termination of processes. It is disabled by default. With this feature enabled, when the Python API process is terminated (e.g. by Ctrl+C), any currently running jobs from any session will be cancelled.

Note: This feature is only available on Linux.

Log Redirection

Starting from DolphinDB Python API version 3.0.2.2, Python API has offered the Logger object. It is used to handle the log information generated during the API execution and the output of print() function in Session.run scripts.

Logger

The Logger provides only two types of loggers: default_logger and msg_logger.

  • default_logger: Logs information generated during API execution, including serialization errors, network connection errors, etc.
import dolphindb as ddb
ddb.logger.default_logger
  • msg_logger: Controls the print output from scripts executed via Session.run. Each Session corresponds to a msg_logger.
import dolphindb as ddb
s = ddb.Session()
s.msg_logger

Log Level

Set the log level by modifying the min_level attribute. The log values from low to high are: DEBUG, INFO (default), WARNING, and ERROR.

import dolphindb as ddb
ddb.logger.default_logger.min_level = ddb.logger.Level.ERROR

Standard Output

The Logger offers the following interfaces to enable or disable standard output (stdout). The stdout is enabled by default.

enable_stdout_sink

def enable_stdout_sink(self) -> None: ...

Enable stdout.

disable_stdout_sink

def disable_stdout_sink(self) -> None: ...

Disable stdout.

File Log Output

File log output is disabled by default. Enable or disable it using the following interfaces.

enable_file_sink

def enable_file_sink(self, path: str) -> None: ...
  • path: The path of the file log.

Enable file log output. This will overwrite the previously specified path.

disable_file_sink

def disable_file_sink(self) -> None: ...

Disable file log output.

Custom Log Output

Sink

The Logger offers the Sink class. Users can inherit it to implement custom log output. Users must specific a name when initializing a new instance of the Sink class.

add_sink

def add_sink(self, sink: Sink) -> None: ...

Add a custom Sink instance to a Logger object.

remove_sink

def remove_sink(self, name: str) -> None: ...

Remove a Sink instance from a Logger object based on the name.

list_links

def list_sinks(self) -> List[Sink]: ...

List all custom Sink instances currently in a Logger object.

Usage Examples

The following example of modifying msg_logger shows how to redirect log output through a custom Sink.

import dolphindb as ddb
from dolphindb.logger import Sink

msg_list = []


class mySink(Sink):
    def handle(self, msg):
        msg_list.append(msg.log)


s = ddb.Session()
s.msg_logger.add_sink(mySink("test1"))

s.connect("localhost", 8848, "admin", "123456")
s.run("print(123)")

print("msg: ", msg_list)

""" output:

123
msg: ['123']
"""

Explanation:

  • Inherit the Sink class to initialize a subclass mySink instance, and override handle method to save the received messages into msg_list.
  • Add a customs Sink instance named "test1" to msg_logger.
  • Execute print(123). With stdout enabled, the console displays two lines of output: the first line is default standard output; the second line is the contents of msg_list.