Configuring loggers

There are the following two configuration details that we need to provide in order to see the output in our logs:

  • The logger we're using needs to be associated with at least one handler that produces conspicuous output.
  • The handler needs a logging level that will pass our logging messages.

The logging package has a variety of configuration methods. We'll show you logging.basicConfig() here. We'll take a look at logging.config.dictConfig() separately.

The logging.basicConfig() method permits a few parameters to create a single logging.handlers.StreamHandler for logging the output. In many cases, this is all we need:

>>> import logging 
>>> import sys 
>>> logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

This will configure a StreamHandler instance that will write to sys.stderr. It will pass messages that have a level that is greater than or equal to the given level. By using logging.DEBUG, we're assured of seeing all the messages. The default level is logging.WARN.

After performing the basic configuration, we'll see our debugging messages when we create the class, as follows:

>>> pc3 = Player_3("Bet3", "Strategy3", 3)
DEBUG:Player_3:init bet Bet3, strategy Strategy3, stake 3

The default log format shows us the level (DEBUG), the name of the logger (Player_3), and the string that we produced. There are more attributes in LogRecord that can also be added to the output. Often, this default format is acceptable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset