title: Python logging.Formatter(): is there any way to fix the width of a field and justify it left/right?
body: <p>Here's a sample of log records from the logging tutorial:</p>
  
  <pre><code>2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message
  2005-03-19 15:38:55,979 - simpleExample - INFO - info message
  2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message
  2005-03-19 15:38:56,055 - simpleExample - ERROR - error message
  2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message
  </code></pre>
  
  <p>This trailing jaggedness annoys me to no end.</p>
  
  <p>I really want to be able to format like this:</p>
  
  <pre><code>2005-03-19 15:38:55,977 - simpleExample -    DEBUG - debug message
  2005-03-19 15:38:55,979 - simpleExample -     INFO - info message
  2005-03-19 15:38:56,054 - simpleExample -  WARNING - warn message
  2005-03-19 15:38:56,055 - simpleExample -    ERROR - error message
  2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message
  </code></pre>
  
  <p>I've attempted the following for my logger, which doesn't work (abbreviated code):</p>
  
  <pre><code>fmt = "{0:&gt;8}"
  formatter = logging.Formatter("%(asctime)s %(filename)s: " + fmt.format("%(levelname)s") + " %(message)s", "%Y/%m/%d %H:%M:%S")
  </code></pre>
  
  <p>This executes fine and prints the level name as always, but it doesn't implement the width format.</p>
  
  <p>Ex.</p>
  
  <pre><code>logger.debug("testing debug message")
  logger.info("some random info")
  logger.critical("oh crap!")
  </code></pre>
  
  <p><strong>Actual result:</strong></p>
  
  <pre><code>2013/12/16 13:43:10 logtester: DEBUG testing debug message
  2013/12/16 13:43:10 logtester: INFO some random info
  2013/12/16 13:43:10 logtester: CRITICAL oh crap!
  </code></pre>
  
  <p><strong>Desired result:</strong></p>
  
  <pre><code>2013/12/16 13:43:10 logtester:    DEBUG testing debug message
  2013/12/16 13:43:10 logtester:     INFO some random info
  2013/12/16 13:43:10 logtester: CRITICAL oh crap!
  </code></pre>
  
  <p>Any hints to implement a fixed width of a field in a logging.Formatter()?</p>
  
  <p>Using Python 2.6.9.</p>
  
  <p><strong>EDIT</strong>: second attempt using another method:</p>
  
  <pre><code>formatter = logging.Formatter("%(asctime)s %(filename)s: " + "%(foo)5s" % {"foo" : "(%(levelname)s)"} + " %(message)s", "%Y/%m/%d %H:%M:%S")
  </code></pre>
  
  <p>Still results in:</p>
  
  <pre><code>2013/12/16 13:43:10 logtester: DEBUG testing debug message
  2013/12/16 13:43:10 logtester: INFO some random info
  2013/12/16 13:43:10 logtester: CRITICAL oh crap!
  </code></pre>
  
  <p>Maybe I'm just doing something boneheaded.</p>