Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Logging TypeError: not all arguments converted during string formatting

Here is what I am doing

>>> import logging >>> logging.getLogger().setLevel(logging.INFO) >>> from datetime import date >>> date = date.today() >>> logging.info('date={}', date) Traceback (most recent call last):   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 846, in emit     msg = self.format(record)   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 723, in format     return fmt.format(record)   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 464, in format     record.message = record.getMessage()   File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 328, in getMessage     msg = msg % self.args TypeError: not all arguments converted during string formatting Logged from file <stdin>, line 1 >>>  

My python version is

$ python --version Python 2.7.3 

How do I make it work?

like image 500
daydreamer Avatar asked Oct 11 '12 15:10

daydreamer


People also ask

How do I fix TypeError is not all arguments converted during string formatting in Python?

The Python "TypeError: not all arguments converted during string formatting" occurs when we use incorrect syntax to format a string or use the % operator with a string and a number. To solve the error, call the format() method on the string and provide values for all placeholders.

How do you fix not all arguments converted during string formatting?

Such a common error is TypeError: not all arguments converted during string formatting. This error is caused when there is a mismatch in data types and strings are not properly formatted. The solution to this error is to use proper string formatting functions such as int() or str() to obtain the desired data type.

What does TypeError not all arguments converted during string formatting mean?

In Python, “typeerror: not all arguments converted during string formatting” primarily occurs when: There is no format specifier. The format specifiers and the number of values are different. Two format specifiers are mixed.

How do you check a string format in Python?

Use time. strptime to parse from string to time struct. If the string doesn't match the format it raises ValueError . Show activity on this post.


2 Answers

You cannot use new-style formatting when using the logging module; use %s instead of {}.

logging.info('date=%s', date) 

The logging module uses the old-style % operator to format the log string. See the debug method for more detail.

If you really want to use str.format() string formatting, consider using custom objects that apply the formatting 'late', when actually converted to a string:

class BraceMessage(object):     def __init__(self, fmt, *args, **kwargs):         self.fmt = fmt         self.args = args         self.kwargs = kwargs      def __str__(self):         return self.fmt.format(*self.args, **self.kwargs)  __ = BraceMessage  logging.info(__('date={}', date)) 

This is an approach the Python 3 logging module documentation proposes, and it happens to work on Python 2 too.

like image 115
Martijn Pieters Avatar answered Sep 30 '22 03:09

Martijn Pieters


You could do the formatting yourself:

logging.info('date={}'.format(date)) 

As was pointed out by Martijn Pieters, this will always run the string formatting, while using the logging module would cause the formatting to only be performed if the message is actually logged.

like image 34
Matt Avatar answered Sep 30 '22 04:09

Matt