简体   繁体   English

如何在stringBuilder中存储日志

[英]how to store logs in stringBuilder

I want to show the frontend user all the logs.我想向前端用户显示所有日志。 How can I transport all the log statements to a frontend?如何将所有日志语句传输到前端? For example:例如:

void process(){
..
// currently this is shown in a file and in a console
log.info("process called..");
}

How can I transport this log message to the frontend in an efficient manner?如何以有效的方式将此日志消息传输到前端? Should I append the logs into a StringBuilder ?我应该将日志 append 写入StringBuilder吗? How can I do this with Log4j2 ?如何使用Log4j2做到这一点?

Currently, I have a no jdbc store.目前,我没有jdbc 商店。 But I can store all my logs to a no sql database.但我可以将所有日志存储到无 sql 数据库中。 I cannot use JDBCAppender (or CassandraAppender ).我不能使用JDBCAppender (或CassandraAppender )。 Should I avoid a Logger and do it myself:我是否应该避免使用 Logger 并自己做:

Instead of代替

log.info("process called..");

I could use我可以使用

user.addLog("process called..");

Would it be better to get the string value of log.info() .获取log.info()的字符串值会更好吗? If so, how?如果是这样,怎么做?

The best idea would would be to store your logs in a database with a JDBCAppender .最好的办法是使用JDBCAppender将日志存储在数据库中。 When the user requests the logs, you can decide how many of the logs to load and return in your response.当用户请求日志时,您可以决定在响应中加载和返回多少日志。

If you would hold all your logs in memory eg in a StringBuffer, you could run out of memory and kill your application.如果您将所有日志保存在 memory 中,例如在 StringBuffer 中,您可能会用完 memory 并终止您的应用程序。 Also on a server restart, all your logs would be lost.同样在服务器重新启动时,您的所有日志都会丢失。 Both is prevented by storing the logs into a database and access it on demand.通过将日志存储到数据库并按需访问它来防止两者。

If you really need a StringAppender for custom integration, you have to write it yourself extending on AbstractOutputStreamAppender .如果您真的需要一个 StringAppender 来进行自定义集成,您必须自己编写它并在AbstractOutputStreamAppender上进行扩展。 Here is a blog post with code about it. 这是一篇博客文章,其中包含有关它的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM