how to store logs in stringBuilder

Viewed 296

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? How can I do this with Log4j2?

Currently, I have a no jdbc store. But I can store all my logs to a no sql database. I cannot use JDBCAppender (or CassandraAppender). Should I avoid a Logger and do it myself:

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(). If so, how?

1 Answers

The best idea would would be to store your logs in a database with a 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 e.g. in a StringBuffer, you could run out of memory and kill your application. 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. Here is a blog post with code about it.

Related