Printf Debugging
Printf debugging is essential, so since we’re going to do it anyways, let’s do it fast. In this post we’ll focus specifically on how to write printf debugging log lines to make them fast to read and write programs against. There’s two important features: a machine parsable format like json or DSV and explicitly labeled fields.
JSON
As we saw last week, JSON is the ideal way to structure machine parsable data. Here are some sample json logs from the field:
{"level": "DEBUG", "data": [[0, "type", "text", 0, null, 0], [1, "name", "text", 0, null, 0], [2, "tbl_name", "text", 0, null, 0], [3, "rootpage", "int", 0, null, 0], [4, "sql", "text", 0, null, 0]], "query": "pragma table_info(sqlite_master);"}
{"level": "DEBUG", "data": [], "query": "select * from sqlite_master;"}
Notice that each field is labeled explicitly. If we’re logging a query, we might be tempted to log it as:
print(json.dumps(query))
If we’re whipping up a log line that isn’t pushed to production, we can cut some corners to go…