Background Image
Table of Contents Table of Contents
Previous Page  349 / 478 Next Page
Information
Show Menu
Previous Page 349 / 478 Next Page
Page Background

Use loggers named after the module you’re in

The root logger is a single global object, available to any library that’s loaded in your

Python process, so you’re never quite in control of it. Instead, follow the

log

ging.getLogger(__name__)

pattern to get one that’s unique to your module, but

that inherits from a top-level configuration you control.

Test important log messages

As we saw, log messages can be critical to debugging issues in production. If a log

message is important enough to keep in your codebase, it’s probably important

enough to test. We follow the rule of thumb that anything above

logging.INFO

definitely needs a test. Using

patch.object

on the logger for the module you’re

testing is one convenient way of unit testing it.

Wrap-Up

|

321