简体   繁体   中英

ValueError: Unsupported character ':' in date/time format string for logging

So I have a python program that I am using the logging module in and when I am setting up the format for the logging string, it is giving this error:

ValueError: unsupported format character ':' (0x3a) at index 24
Logged from file snippets-convert.py, line 36

and it is a real pain!

Here is the code that I am using:

logging.basicConfig(
    format='%(asctime)s:%(levelname):%(message)', 
    datefmt='%m/%d/%Y %I:%M:%S %p',
    filename='./data/'+time.strftime("%d-%m-%Y")+ '.log', 
    level=logging.DEBUG)

and the full traceback:

Traceback (most recent call last):
  File "/usr/lib/python3.3/logging/__init__.py", line 937, in emit
    msg = self.format(record)
  File "/usr/lib/python3.3/logging/__init__.py", line 808, in format
    return fmt.format(record)
  File "/usr/lib/python3.3/logging/__init__.py", line 549, in format
    s = self.formatMessage(record)
  File "/usr/lib/python3.3/logging/__init__.py", line 518, in formatMessage
    return self._style.format(record)
  File "/usr/lib/python3.3/logging/__init__.py", line 364, in format
    return self._fmt % record.__dict__
ValueError: unsupported format character ':' (0x3a) at index 24
Logged from file snippets-convert.py, line 36

but I can't find the error.

I believe that the code causing the issue is the datefmt='%m/%d/%Y %I:%M:%S %p' part of the logging config, but I copied that directly from the Python 3.3 documentation here: http://docs.python.org/3.3/howto/logging.html#displaying-the-date-time-in-messages (direct link to the code I copied), so I don't know why it is being such an annoying little jerk!

I can include more code if needed, I just wanted to keep my post as succinct and to-the-point as possible.

If anyone knows that cause of this error and how to fix it, I would be most pleased!

Thanks!

The problem has nothing to do with your date format, but with your format string. You can test this very easily:

logging.basicConfig(
    format='%(asctime)s %(levelname) %(message)', 
    datefmt='%m/%d/%Y %I:%M:%S %p',
    filename='./data/'+time.strftime("%d-%m-%Y")+ '.log', 
    level=logging.DEBUG)

No error; everything works.


However, I think this is just a symptom of a more serious problem.

%(levelname) is not a format pattern. You probably wanted %(levelname)s . Because you left the s off, the % -format operation tries to look up the format type for format character : , and there is no such format character, hence the error.

Using a space instead just means you will successfully get the literal string %(levelname) instead of an error. That's not what you wanted.

So, do this:

logging.basicConfig(
    format='%(asctime)s:%(levelname)s:%(message)s', 
    datefmt='%m/%d/%Y %I:%M:%S %p',
    filename='./data/'+time.strftime("%d-%m-%Y")+ '.log', 
    level=logging.DEBUG)

Now there's no error, and you get what you presumably were looking for too.

By the way, this is one of the advantages of using {} -formatting instead of % -formatting—there are a lot fewer errors possible, and those that do show up are a lot less inscrutable to debug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM