ajit junghare ajit junghare - 4 years ago 216
Python Question

Flask Python Logging: TypeError while logging file path in File

I'm trying to add content of variable "log_location" to current logger.

log_location = log_folder_location + os.path.sep + log_file_name
logger.debug("log location", str(log_location))
print "log_location: ",log_location


this prints to console, but gives error in logging,

Traceback (most recent call last):
File "/usr/lib64/python2.6/logging/__init__.py", line 784, in emit
msg = self.format(record)
File "/usr/lib64/python2.6/logging/__init__.py", line 662, in format
return fmt.format(record)
File "/usr/lib64/python2.6/logging/__init__.py", line 444, in format
record.message = record.getMessage()
File "/usr/lib64/python2.6/logging/__init__.py", line 314, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting


it prints to,

log_location: /U01/Nova/Logs/DEV-INT/TEST/validation_20170203-164617-5.log


This error is not happening in when I try in normal python prompt
but facing same when using Flask

I've tried Logging Python stdout to File... with active stdout (backspacing/updating)
and TypeError: not all arguments converted during string formatting python
and this one too Python: Logging TypeError: not all arguments converted during string formatting

But did not understand what to do. Can someone explain this using simpler language, not like the documentation?

Answer Source

Use only one argument to logger.debug() method call. I.e.

logger.debug("log location: " + str(log_location))

About the autoformatting feature (I don't think you need this, but for the completeness sake) - let's look from the docs:

Logger.debug(msg, *args, **kwargs)

Logs a message with level DEBUG on this logger. The msg is the message format string, and the args are the arguments which are merged into msg using the string formatting operator. (Note that this means that you can use keywords in the format string, together with a single dictionary argument.)

So, multiple arguments are useful when you have formatting template in the first arg, for example

logger.debug('log location: %s', log_location)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download