30. Log with logging
The mismiy command used plain Python print calls to issue messages like
‘Loading metadata from META.yaml’. This is mostly fine, but clutters
the output of the unit tests. It would be nice to control it with
the traditional -v (or --verbose) command-line flags, but
not if it means passing an is_verbose parameter to every function
call that might print something.
Printing with logging
We can use Python’s built-in logging module instead.
It also seemed like it would be more consistent with the output of the
--server option.
In principle the logging system allows for fine-grained control over
how detailed the logs are on a module-by-module basis. But Mismiy
is not a production server and does not need that level of control.
Instead we will use the traditional -v and -q
flags to alter the minimum logging level. The default is INFO,
-v means DEBUG and -q means WARN
(so all messages other than errors are suppressed).
Server logs were logging with print
I was surprised to discover the HTTP server module does
not in fact use the logging module, but instead prints unconditionally
to stderr. So I added code to subclass SimpleHTTPREquestHandler
to make it log to a logger.
The http library uses a colorization module that is not officially
supported. So the result of my changes is that the log is now in
monochrome. For now I am leaving it that way, on less-code,
you-dont-really-need-it grounds.
Logging format
When not running as a server, messages are not prefixed with the date, time, and module name.
Posts on similar topics