[Tutor] python logger

Timo timomlists at gmail.com
Wed Dec 28 19:03:45 CET 2011


Op 28-12-11 18:13, rail shafigulin schreef:
> 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
Without reading the code, you are passing too many arguments for a 
format string.

 >>> msg = 'one arg %s and a second %s'
 >>> msg % ('foo', 'bar', 'baz') # Pass 3 arguments
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

Cheers,
Timo

>
> 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 <http://mylogadapter.info>('this is a log message', 
> testinfo)
>
> if __name__ == "__main__":
>   main()
>
> any help on how to add extra output parameters is appreciated.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list