Java 如何通过dropwizard上的API流出日志?
我想通过api endpoint/logs for dropwizard流出日志 这比我想象的要难。Dropwizard将通过config.yml读取配置,并将这些信息作为私有信息。我不知道在哪里可以找到记录一切的记录器? 我错过什么了吗Java 如何通过dropwizard上的API流出日志?,java,stream,jersey,dropwizard,Java,Stream,Jersey,Dropwizard,我想通过api endpoint/logs for dropwizard流出日志 这比我想象的要难。Dropwizard将通过config.yml读取配置,并将这些信息作为私有信息。我不知道在哪里可以找到记录一切的记录器? 我错过什么了吗 还有别的办法吗 这是一个流媒体示例,您也可以在此处阅读: 及 守则: public class StreamingTest extends io.dropwizard.Application<Configuration> { @Ove
还有别的办法吗 这是一个流媒体示例,您也可以在此处阅读: 及 守则:
public class StreamingTest extends io.dropwizard.Application<Configuration> {
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
environment.jersey().property(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 0);
environment.jersey().register(Streamer.class);
}
public static void main(String[] args) throws Exception {
new StreamingTest().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
}
@Path("/log")
public static class Streamer {
@GET
@Produces("application/octet-stream")
public Response test() {
return Response.ok(new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
while (true) {
output.write("Test \n".getBytes());
output.flush();
try {
Thread.sleep(1000); // simulate waiting for stream
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).build();
}
}
}
公共类StreamingTest扩展io.dropwizard.Application{
@凌驾
公共void运行(配置、环境)引发异常{
environment.jersey().property(ServerProperties.OUTBOUND\u CONTENT\u LENGTH\u BUFFER,0);
environment.jersey()寄存器(Streamer.class);
}
公共静态void main(字符串[]args)引发异常{
新的StreamingTest().run(“server”,“/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml”);
}
@路径(“/log”)
公共静态类拖缆{
@得到
@产生(“应用程序/八位字节流”)
公众反应测试(){
返回Response.ok(新的StreamingOutput(){
@凌驾
public void write(OutputStream output)引发IOException、WebApplicationException{
while(true){
output.write(“Test\n”.getBytes());
output.flush();
试一试{
Thread.sleep(1000);//模拟等待流
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}).build();
}
}
}
这正是你想要的
几点线evironment.jersey().property(ServerProperties.OUTBOUND\u CONTENT\u LENGTH\u BUFFER,0)代码>很重要。它告诉服务器在将输出返回给调用方之前不要缓冲输出
此外,您需要通过调用output.flush()显式清除输出代码>
然而,这似乎是装运日志的错误方式。你有没有调查过i.e.logstash?还是基于网络的附加器,将日志直接流式传输到您需要的位置
希望有帮助
--artur不清楚你到底想做什么?你能给出一个输出示例吗?您是否希望通过http保持连接的打开状态,并在不停止的情况下继续输出日志?你的客户是如何知道他什么时候完成的?假设我正在记录:line1 line2 line3 line4在我访问/记录的那一刻,我希望看到一个连续的流:line5 line6 line7…这个流真的不是问题,但我想知道你是否使用它来做正确的事情?你能详细说明你最终需要实现什么吗?