title: How can I log current line, and stack info with Python?
body: <p>I have logging function as follows.</p>
  
  <pre><code>logging.basicConfig(
      filename = fileName,
      format = "%(levelname) -10s %(asctime)s %(message)s",
      level = logging.DEBUG
  )
  
  def printinfo(string):
      if DEBUG:
          logging.info(string)
  
  def printerror(string):
      if DEBUG:
          logging.error(string)
      print string
  </code></pre>
  
  <p>I need to login the line number, stack information. For example:</p>
  
  <pre><code>1: def hello():
  2:    goodbye()
  3:
  4: def goodbye():
  5:    printinfo()
  
  ---&gt; Line 5: goodbye()/hello()
  </code></pre>
  
  <p>How can I do this with Python?</p>
  
  <h2>SOLVED</h2>
  
  <pre><code>def printinfo(string):
      if DEBUG:
          frame = inspect.currentframe()
          stack_trace = traceback.format_stack(frame)
          logging.debug(stack_trace[:-1])
      if LOG:
          logging.info(string)
  </code></pre>
  
  <p>gives me this info which is exactly what I need.</p>
  
  <pre><code>DEBUG      2011-02-23 10:09:13,500 [
    '  File "/abc.py", line 553, in &lt;module&gt;\n    runUnitTest(COVERAGE, PROFILE)\n', 
    '  File "/abc.py", line 411, in runUnitTest\n    printinfo(string)\n']
  </code></pre>