Pregunta

Text based logging has the disadvantage that they are hard to read and understand. Is there a library that would allow me to generate structured and visually aesthetic log file?

For example, on issue following log statement:

logger.log('Request received from gateway', request)

The output could be something like this:

<p>Request received from gateway. <a href='detail.html#163'>Details</a></p>

So that the log file log.html shows:

Thursday 14th March 04:8:13 - Request received from gateway. Details

Here details could be an anchored hyperlink into another html file with the String dump of the request object.

The advantage of this approach would be that the verbosity would be reduced. Instead of all the details being dumped onto the screen at once, we'd only see the overview. Details would appear in a popup window if and when we need to see it.

How could I create more readable, structured logs in Java?

¿Fue útil?

Solución

I did not find anything to suit my needs. Hence I created a small open source library called log4j-weblayout.

There is a org.apache.log4j.HTMLLayout in log4j but I did not find it useful. All it does is dump your statements in a HTML table. While this is better than text dump it doesn't utilize HTML features such as font colors and popup windows.

For introduction and usage information take a look at the project website.

List of features (will be updated as more are added):

  1. Better timestamp handling. Major source of verbosity in log files is the repeating timestamp. Text based files have complete timestamp for each log statement like: Sat Jul 23 2005 02:16:57 150ms. This increases the verbosity. If you print a shorter form like 02:16:57 you loose essential details. Weblayout hits the sweet spot by printing the concise readable form and showing full date in a mouse over popup.
  2. Distinguishing levels by color coding. The statements from different levels stand out from each other.
  3. Switching log level at runtime. With text files you set the log level before running the application. You cannot change the level while viewing the log. Weblayout provides a small toolbox at the top of the screen that toggles the levels by using Javascript.
  4. Better exception handling. Combines multiple exceptions together to display a single coherent description of the problem. For example it would merge ConnectException, IllegalArgumentException and ParseException into something like: Did not receive response from server, because The request is not valid, because Date cannot be in the past.
  5. Better stack trace handling. By default logger.info(exception) will log only message and not stacktrace. If you manually extract stacktrace as a string and pass it to log4j, it would not be very readable. log4j-weblayout will do this automatically and pretty print it in the same way that your IDE does.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top