0

I have a script using several imported submodules of mine, each of them using the module logging. In 1 of them i wrote a bad code line, but i can figure out where it is. Indeed, the synthax for a typical logging output with an argument var is:

logging.info("my var=%s", var)

If the number of arguments does not correspond to the number of %s in the string, there is an error message from logging as follows :

Traceback (most recent call last):
File "/usr/lib/python2.6/logging/__init__.py", line 776, in emit
  msg = self.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 654, in format
  return fmt.format(record)
File "/usr/lib/python2.6/logging/__init__.py", line 436, in format
  record.message = record.getMessage()
File "/usr/lib/python2.6/logging/__init__.py", line 306, in getMessage
  msg = msg % self.args
ValueError: incomplete format

The problem is that this error message is completely generic: it does not show the name of the module where the probleme appear, nor the code line, nor the arguments... which makes impossible for me to find out where I wrote a bad call to logging.info()

1
  • In Python 2.7 and later, you should get a message indicating where the line was logged from (Issue #7869, fixed in February 2010). Commented Apr 1, 2015 at 22:29

2 Answers 2

1

Q&D braindead solution : edit /usr/lib/python2.6/logging/init.py and wrap line 776 in a try/except clause:

try:
    msg = self.format(record)
except ValueError:
    import pdb; pdb.set_trace()

then re-run your code from a shell and do whatever needed to re-raise the exception. You'll be sent in the step debugger, from which you can inspect the whole call stack.

Once the problem solved, rollback your edits. And eventually contribute a patch to the logging module so it emits a more useful error message...

Oh and BTW : automated tests are a good way to catch this kind of errors as soon as possible, when you remember what part of the code you just touched.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Editing logging/init.py was actually my first idea... but i do not have root permissions on my machine :(
How can one not have root permissions on one's own machine ??? I don't know where you're working but be sure I'll never work there. But anyway: nothing prevents you from getting the logging package's sources (it's pure Python AFAICT) and copy it locally so it's in your sys.path.
in any university for example, it is probably the case :)
In Python 2.7 and later, you should get a message indicating where the line was logged from (Issue #7869, fixed in February 2010).
0

try this:

logging.info("my var=%s" % var)

5 Comments

The comma will work just fine since the logging function will do replacements
my problem is that i have already 1000's of code lines in several modules, using the synthax logging.info("my var=%s", var), which works pretty well, except that at one (unknown) place, the number of arguments does not fit the number of %s... And i would like another solution than checking each lline of code...
what is inside variable var ?
it could be anything, sometimes a INT sometimes a str. In this precise case, i don't know since i don't know where is the bug in the code. MY question is how can i bypass the fact that the error message of the module logging are unsuable.
@Peter: this kind of defeat part of the point of using the logging module, as it will perform the string formatting regardless of the current logging level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.