Let VizTracer be the dashcam of your python program!

Tian Gao
3 min readSep 1, 2020

--

I’ve shared the package VizTracer that I’ve been developing recently. I presented it as a profiler. However, it was more than a profiler. There are more aspects of it.

So today, I’ll take another angle and talk about it as a logger.

First of all I want to introduce a little bit about VizTracer to those who have never heard of it. It’s a python tool that can record data while your python program is running, and generate an HTML report of your execution like this:

The most important data VizTracer records is function entry/exit(or, FEE). It records every single call in your program and display them in timeline, which is very helpful for you to understand what your program is doing.

Now back to logging. Many people debug their code by logging. Some are using the builtin logging module, some are using other libraries. Normally the log you get is in text format, no matter it’s in a file or just print to your screen. But what if you get something much more intuitive and detailed? You can see your call stack through time, examine every function call, and log arbitrary data at any time.

Of course, with more information, comes more burden. VizTracer can’t keep record of everything ever happened because that’ll just cost too much memory and disk space. However, VizTracer is using circular buffer now and will record the most recent events.

Just like a dashcam, you normally do not check its contents, unless you are in some incident. You always check for the latest videos which is the most relevant to the incident.

You can dump the log out whenever your program is doing unexpected behavior, like when you catch an exception, or you find some errors, or even when your program exits. VizTracer will dump a beautiful and detailed report of what happened just before your problem, which probably has the reason that caused your issue.

You can run your program with VizTracer for a long time, and VizTracer will only record in a circular buffer, waiting to dump the most recent events. Yes it will have overhead, but so does any logging mechanism. VizTracer provides filters for you to minimize overhead and keep the most important data you are interested in.

Unlike traditional text based loggers, VizTracer can show you when your data is recorded on a timeline. You can have multiple data signals(threads, processes, variables, objects) displayed together on the same timeline.

If you are using builtin logging module, you can just add the handler VizTracer provided to it, and have your logs display on VizTracer.

Come and check it if you are interested :) Please tell me if you have anything to say about VizTracer. Also if you want to do it more formal, create an issue for me :)

https://github.com/gaogaotiantian/viztracer

--

--

No responses yet