日誌
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.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.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...");
}
}