0

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?

Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
  • looks like a good idea for me – Leo Apr 04 '14 at 11:29
  • @Leo I am thinking of developing an open source project along these lines. Want to know if something similar is already out there. – Kshitiz Sharma Apr 04 '14 at 11:30
  • just be careful not to have too many files in your file system. see http://stackoverflow.com/questions/466521/how-many-files-in-a-directory-is-too-many – Leo Apr 04 '14 at 11:30
  • @Leo You'll notice that I wrote `detail.html#467` not `detail467.html`. There would be one file with many sections not one file for each detail. – Kshitiz Sharma Apr 04 '14 at 11:34
  • Before going further on your project think about what you can offer that Splunk and other alternatives don't offer yet, and then if you still feel that you have something, then go ahead – morgano Apr 04 '14 at 11:34
  • yup, even contributing to existent OS options is a good idea too :-) – Leo Apr 04 '14 at 11:46
  • what about using [org.apache.log4j.HTMLLayout](http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/HTMLLayout.html) in your log4j configuration? – seph Apr 04 '14 at 11:53
  • @seph That could be a good starting point. Doesn't seem to support hyperlinking though. – Kshitiz Sharma Apr 04 '14 at 11:58
  • something like [this](http://logstash.net/) ? – seph Apr 04 '14 at 12:05
  • @seph I took some time to look at that. 1. The source code is in Ruby 2. The zipped download is 60 MB 3. It is a full fledged framework and not a lib 4. It locks you in. You need logstash to analyze log it generates. I'm thinking about standards compliant HTML which would open in any browser. – Kshitiz Sharma Apr 04 '14 at 12:32
  • @KshitizSharma Quick thought: If you persist in writing your own logger, **why not use an embedded relational database like SQLite or H2**? At least you'll be able to structure (and index) your logs, you'll get _indexing & querying for free_ and you'll only have a single file and nothing prevents you from rendering the content in html, if you must. – VH-NZZ Apr 23 '14 at 08:39

1 Answers1

0

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.
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
  • https://www.vogella.com/tutorials/Logging/article.html did you use the same implementation logic as this tutorial explains? – Renis1235 Mar 31 '21 at 08:58