Application code should not directly instantiate and use instances of$_{Handler}$. Instead, the$_{Handler}$class is a base class that defines the interface that all handlers should have and establishes some default behavior that child classes can use (or override). 

## 2.4 Formatters 

Formatter objects configure the final order, structure, and contents of the log message. Unlike the base$_{logging.}$ Handler class, application code may instantiate formatter classes, although you could likely subclass the formatter if your application needs special behavior. The constructor takes three optional arguments – a message format string, a date format string and a style indicator. 

logging.Formatter. __init__ ( fmt=None , datefmt=None , style=’%’ ) 

If there is no message format string, the default is to use the raw message. If there is no date format string, the default date format is: 

%Y-%m-%d %H:%M:%S 

with the milliseconds tacked on at the end. The style is one of % , ‘{‘ or ‘$’. If one of these is not specified, then ‘%’ will be used. 

If the style is ‘%’, the message format string uses %(<dictionary key>)s styled string substitution; the possible keys are documented in logrecord-attributes. If the style is ‘{‘, the message format string is assumed to be compatible with$_{str.format()}$ (using keyword arguments), while if the style is ‘$’ then the message format string should conform to what is expected by$_{string.Template.substitute()}$ . 

Changed in version 3.2: Added the$_{style}$parameter. 

The following message format string will log the time in a human-readable format, the severity of the message, and the contents of the message, in that order: 

' %(asctime)s - %(levelname)s - %(message)s ' 

Formatters use a user-configurable function to convert the creation time of a record to a tuple. By default,$_{time.}$ localtime() is used; to change this for a particular formatter instance, set the$_{converter}$attribute of the instance to a function with the same signature as$_{time.localtime()}$ or$_{time.gmtime()}$ . To change it for all formatters, for example if you want all logging times to be shown in GMT, set the$_{converter}$attribute in the Formatter class (to$_{time.gmtime}$ for GMT display). 

## 2.5 Configuring Logging 

Programmers can configure logging in three ways: 

1. Creating loggers, handlers, and formatters explicitly using Python code that calls the configuration methods listed above. 

2. Creating a logging config file and reading it using the$_{fileConfig()}$ function. 

3. Creating a dictionary of configuration information and passing it to the$_{dictConfig()}$ function. 

For the reference documentation on the last two options, see logging-config-api. The following example configures a very simple logger, a console handler, and a simple formatter using Python code: 

import logging # create logger logger=logging.getLogger('simple_example ') logger.setLevel(logging .DEBUG) # create console handler and set level to debug ch =logging.StreamHandler() 

(continues on next page) 

