Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:不支持的字符';:';日志记录的日期/时间格式字符串_Python_Logging_Format_Python 3.3 - Fatal编程技术网

Python ValueError:不支持的字符';:';日志记录的日期/时间格式字符串

Python ValueError:不支持的字符';:';日志记录的日期/时间格式字符串,python,logging,format,python-3.3,Python,Logging,Format,Python 3.3,因此,我有一个python程序,我正在使用中的日志记录模块,当我设置日志记录字符串的格式时,它给出了以下错误: ValueError: unsupported format character ':' (0x3a) at index 24 Logged from file snippets-convert.py, line 36 这真的很痛苦 以下是我正在使用的代码: logging.basicConfig( format='%(asctime)s:%(levelname):%(mes

因此,我有一个python程序,我正在使用中的
日志记录
模块,当我设置日志记录字符串的格式时,它给出了以下错误:

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

以下是我正在使用的代码:

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)
以及完整的回溯:

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
但是我找不到错误

我相信引起问题的代码是日志配置的
datefmt='%m/%d/%Y%I:%m:%S%p'
部分,但我直接从Python 3.3文档中复制了它:(直接链接到我复制的代码),所以我不知道为什么这是一个令人讨厌的小混蛋

如果需要的话,我可以包含更多的代码,我只是想让我的文章尽可能简明扼要

如果有人知道这个错误的原因以及如何修复它,我将非常高兴


谢谢

问题与日期格式无关,而是与格式字符串有关。您可以非常轻松地测试这一点:

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)
没有错误;一切正常


然而,我认为这只是一个更严重问题的征兆

%(levelname)
不是格式模式。您可能想要
%(levelname)s
。由于您关闭了
s
,因此
%
-format操作尝试查找格式字符的格式类型
,但没有此类格式字符,因此出现错误

使用空格只意味着您将成功获取文本字符串
%(levelname)
,而不是错误。那不是你想要的

因此,请这样做:

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)
现在没有错误了,你也得到了你想必想要的东西

顺便说一句,这是使用
{}
-格式而不是
%
-格式的优点之一-可能出现的错误要少得多,出现的错误要调试起来也不那么难懂