[Tutor] python logger

rail shafigulin rail.shafigulin at gmail.com
Wed Dec 28 18:13:23 CET 2011


has anyone used python logger before? i'm trying to adapt it for my
workplace. right now it is pretty simplistic for me. i'm trying to generate
extra output by the LoggerAdapter.  however i'm getting errors.
specifically i get the following message:

Traceback (most recent call last):
  File "/usr/lib/python3.1/logging/__init__.py", line 770, in emit
    msg = self.format(record)
  File "/usr/lib/python3.1/logging/__init__.py", line 650, in format
    return fmt.format(record)
  File "/usr/lib/python3.1/logging/__init__.py", line 438, in format
    record.message = record.getMessage()
  File "/usr/lib/python3.1/logging/__init__.py", line 308, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting

i'm using this<http://docs.python.org/release/3.1.3/library/logging.html#using-loggeradapters-to-impart-contextual-information>documentation.
any

here is my code

import logging
import logging.handlers

class TestInfo(object):
  def __init__(self, test_name = None,
               description = '',
               notes = '',
               expected = None,
               actual = None,
               status = 'Fail',
               timestamp = None):
    self.__test_name = test_name
    self.__description = description
    self.__notes = notes
    self.__expected = expected
    self.__actual = actual
    self.__status = status
    self.__timestamp = timestamp

  def __getitem__(self, name):
    """
    To allow this instance to look like a dict.
    """
    result = None
    if 'test_name' == name:
      result = self.__test_name
    elif 'description' == name:
      result = self.__description
    elif 'notes' == name:
      result = self.__notes
    elif 'expected' == name:
      result = self.__expected
    elif 'actual' == name:
      result = self.__actual
    elif 'status' == name:
      status = self.__status
    elif 'timestamp' == name:
      timestamp = self.__timestamp
    else:
      result = self.__dict__.get(name, "?")
    return result

  def __iter__(self):
    """
    To allow iteration over keys, which will be merged into
    the LogRecord dict before formatting and output.
    """
    names = ['test_name', 'description', 'notes', 'expected', 'actual',
'status', 'timestamp']
    names.extend(self.__dict__.keys())
    return names.__iter__()

def main():
  testinfo = TestInfo(
    test_name = 'myname',
    description = 'mydescription',
    notes = 'mynotes',
    expected = 'myexpected',
    actual = 'myactual',
    status = 'Fail',
    timestamp = 'mystamp')

  mylogger = logging.getLogger('mylogger')
  mylogger.setLevel(logging.DEBUG)

  filehandler = logging.FileHandler('test.log')
  filehandler.setLevel(logging.DEBUG)

  mylogger.addHandler(filehandler)
  mylogadapter = logger.LoggerAdapter(mylogger, testinfo)
  mylogadapter.info('this is a log message', testinfo)

if __name__ == "__main__":
  main()

any help on how to add extra output parameters is appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111228/36587afa/attachment.html>


More information about the Tutor mailing list