FotisK
FotisK

Reputation: 1177

Create log file for interactive browser display

I am using to log the execution on my python project the logging module and for the file handler I have configured to log with DEBUG and with the below formatter:

fileformatter = logging.Formatter('[%(asctime)s] %(name)s '
                                  '%(funcName)s():%(lineno)d '
                                  '[%(levelname)s] - %(message)s')

Now, I am at a point where I would like to view the log file in a browser and been able to select how much I would like to view! So maybe only INFO messages or only ERROR msgs or been able to collaps/expand lines!

I assume that I have some how to modify the log file into another format maybe XML or is this also not the best format!?

Upvotes: 0

Views: 802

Answers (2)

Anthon
Anthon

Reputation: 76598

Every format has its pros and cons of which you need to be aware. If e.g. you write things out as XML or HTML, you will have more trouble updating the file because the closing element of those formats (e.g. </BODY></HTML>) prevents you from simple appending new entries to the end of the file. If you would have to parse the complete file to remove the element, that would be significant overhead (but since you know how the logger writes things you can probably just set the file pointer at 14 or so characters before the end).

The major drawback IMO is that the tagged format promoted by SGML, HTML and XML, while humanly parsable, is not really readable (to quickly spot things) and is inefficient through its enormously bloat.

I suggest you leave the format as is and have you browsers build in javascript engine parse and format it on the fly to make it into something you can read in your browser, including some buttons in the interface to suppress certain message classes.

Upvotes: 1

Finwood
Finwood

Reputation: 3981

Displaying logs interactively in a browser differs much from the static log file approach. You usually just want to display the log file as it is, without further processing.

The neat way

Your idea to save logs in a different format is quite right, but XML does not work that well. Every XML document must have only one root-node, so you would have to rewrite your log file every time you add a log record.

Instead, I'd recommend one of the following approaches:

  • If you already have a database of any kind running, write your logs into it. From there you can access, search and filter your logs easily.
  • Use a different markup style for saving your logs; YAML, for example, does not need closing tags like XML.
  • Fall back to a plain text format and use regular expressions to grab information for filtering

In any case you'll need some Javascript to take care of the interactivity (show/hide records, fold tracebacks or pre-defined blocks, etc.).

A very simple, yet not very nice solution

If you want to avoid all of the above mentioned difficulties, creating multiple log files from the start would work. Add multiple FileHandlers to your logger object and configure different file names and logging levels for each of them.

This way you can specify the logging level to be displayed in the browser by selecting the right file.

Upvotes: 1

Related Questions