跳到主要内容

日志

Python

HTTP API

以下代码演示了自定义格式,使用stream进行输出

from webullsdkcore.client import ApiClient
from webullsdkcore.common.region import Region

client = ApiClient(app_key="<your_app_key>", app_secret="<your_app_secret>", region_id=Region.HK.value)
log_format='%(thread)d %(asctime)s %(name)s %(levelname)s %(message)s'
client.set_stream_logger(stream=sys.stdout, format_string=log_format)

以下代码演示了自定义格式,使用文件进行输出

from webullsdkcore.client import ApiClient
from webullsdkcore.common.region import Region

client = ApiClient(app_key="<your_app_key>", app_secret="<your_app_secret>", region_id=Region.HK.value)
log_format='%(thread)d %(asctime)s %(name)s %(levelname)s %(message)s'
log_file_path='<my_file_path>'
client.set_file_logger(path=log_file_path, format_string=log_format)

行情订阅

以下代码演示了通过回调函数on_log,使用print简单处理日志数据

from webullsdkmdata.quotes.subscribe.default_client import DefaultQuotesClient
from webullsdkcore.common.region import Region

quotes_client = DefaultQuotesClient("<your_app_key>", "<your_app_secret>", region_id=Region.HK.value)
def _on_log_func(client, userdata, level, log_data_buf):
print("level:%s, buf:%s" % (level, log_data_buf))
quotes_client.on_log = _on_log_func

交易事件订阅

以下代码演示了通过回调函数on_log,使用print简单处理日志数据

from webullsdktradeeventscore.events_client import EventsClient
from webullsdkcore.common.region import Region

events_client = EventsClient("<your_app_key>", "<your_app_secret>", region_id=Region.HK.value)
def _on_log_func(level, log_data_buf):
print("level:%s, buf:%s" % (level, log_data_buf))
enents_client.on_log = _on_log_func

行情API

以下代码演示了自定义格式,使用文件进行输出

from webullsdkquotescore.grpc.grpc_client import GrpcApiClient
from webullsdkcore.common.region import Region

client = GrpcApiClient(app_key="<your_app_key>", app_secret="<your_app_secret>", region_id=Region.HK.value)
log_format='%(thread)d %(asctime)s %(name)s %(levelname)s %(message)s'
log_file_path='<my_file_path>'
client.set_file_logger(path=log_file_path, format_string=log_format)

Java

使用日志框架

SDK通过寻找日志实现来支持使用多种日志框架。日志框架优先级:Slf4J > Log4J2 > Log4J > java.util.logging

例:使用Slf4J + Log4J2

Maven配置

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
</dependencies>

/resources目录下添加配置文件log4j2.properties,例如

status = info

appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.rolling.type = RollingFile
appender.rolling.name = LogToRollingFile
appender.rolling.fileName = logs/app.log
appender.rolling.filePattern = logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 10

logger.app.name = com.webull.openapi
logger.app.level = debug
logger.app.additivity = false
logger.app.appenderRef.rolling.ref = LogToRollingFile
logger.app.appenderRef.console.ref = LogToConsole

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole

自定义日志实现

如果需要自定义日志实现,用户需要实现com.webull.openapi.logger.Logger接口与继承com.webull.openapi.logger.LoggerFactory类,例如

实现Logger接口

import com.webull.openapi.logger.Logger;

public class ExampleLogger implements Logger {

private final String name;

public ExampleLogger(String name) {
this.name = name;
}

@Override
public String name() {
return name;
}

@Override
public void trace(String msg) {
System.out.println(msg);
}

@Override
public void debug(String msg) {
System.out.println(msg);
}

@Override
public void info(String msg) {
System.out.println(msg);
}

@Override
public void warn(String msg) {
System.out.println(msg);
}

@Override
public void error(String msg) {
System.out.println(msg);
}

// 省略部分代码...
}

继承LoggerFactory

import com.webull.openapi.logger.Logger;
import com.webull.openapi.logger.LoggerFactory;

public class ExampleLoggerFactory extends LoggerFactory {

@Override
protected Logger newLogger(String name) {
return new ExampleLogger(name);
}
}

设置默认日志工厂

public class LoggerTest {

static {
// 设置默认日志工厂
LoggerFactory.setDefault(new ExampleLoggerFactory());
}

private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

public static void main(String[] args) {
logger.error("Print error log...");
logger.warn("Print warn log...");
logger.info("Print info log...");
logger.debug("Print debug log...");
logger.trace("Print trace log...");
}
}