(continued from previous page) The result should be 6 separate files, each with part of the log history for the application: The most current file is always$_{logging_rotatingfile_example.out}$ , and each time it reaches the size limit it is renamed with the suffix .1 . Each of the existing backup files is renamed to increment the suffix ($_{.1}$ becomes$_{.2}$ , etc.) and the$_{.6}$ file is erased. Obviously this example sets the log length much too small as an extreme example. You would want to set maxBytes to an appropriate value. When logging was added to the Python standard library, the only way of formatting messages with variable content was to use the %-formatting method. Since then, Python has gained two new formatting approaches: string. $_{Template}$(added in Python 2.4) and$_{str.format()}$ (added in Python 2.6). Logging (as of 3.2) provides improved support for these two additional formatting styles. The$_{Formatter}$ class been enhanced to take an additional, optional keyword parameter named$_{style}$. This defaults to$_{'%'}$ , but other possible values are$_{'{'}$and$_{'$'}$ , which correspond to the other two formatting styles. Backwards compatibility is maintained by default (as you would expect), but by explicitly specifying a style parameter, you get the ability to specify format strings which work with$_{str.format()}$ or$_{string.Template}$ . Here’s an example console session to show the possibilities: 18 (continues on next page) >>> import logging >>> root =logging.getLogger() >>> root.setLevel(logging .DEBUG) >>> handler =logging.StreamHandler() >>> bf =logging.Formatter(' {asctime} {name} {levelname:8s} {message} ', ... style='{ ') >>> handler.setFormatter(bf) >>> root.addHandler(handler) >>> logger=logging.getLogger('foo.bar') >>> logger.debug( 'This is a DEBUG message ') 2010-10-28 15:11:55,341 foo.bar DEBUG This is a DEBUG message >>> logger.critical( 'This is a CRITICAL message ') 2010-10-28 15:12:11,526 foo.bar CRITICAL This is a CRITICAL message >>> df =logging.Formatter('$asctime $name $ {levelname} $message ', ... style='$') logging_rotatingfile_example.out logging_rotatingfile_example.out.1 logging_rotatingfile_example.out.2 logging_rotatingfile_example.out.3 logging_rotatingfile_example.out.4 logging_rotatingfile_example.out.5 handler =logging.handlers.RotatingFileHandler( LOG_FILENAME, maxBytes =20 , backupCount =5) my_logger .addHandler(handler) # Log some messages for i in range(20 ): my_logger .debug( 'i = %d '%i) # See what files are created logfiles=glob.glob( ' %s *'%LOG_FILENAME) for filename in logfiles: print(filename) 11 Use of alternative formatting styles