Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 日志记录中的格式类型_Python_Logging_Format - Fatal编程技术网

Python 日志记录中的格式类型

Python 日志记录中的格式类型,python,logging,format,Python,Logging,Format,我知道python中字符串格式的基本知识,但我不理解所使用的格式 在日志的基本配置方法中 FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s' logging.basicConfig(format=FORMAT) %(string)s类型格式的用途是什么以及何时使用它们?这是一个问题 %的右操作数可以是元组或字典(或其他映射) 使用元组时,%d,%s。。。。按顺序替换为相应的项目 使用词典时,%(键)d,%(键)s。。。

我知道python中字符串格式的基本知识,但我不理解所使用的格式 在日志的基本配置方法中

FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT) 

%(string)s
类型格式的用途是什么以及何时使用它们?

这是一个问题

%
的右操作数可以是元组或字典(或其他映射)

使用元组时,
%d
%s
。。。。按顺序替换为相应的项目

使用词典时,
%(键)d
%(键)s
。。。替换为字典中相应的
键的值

比如说,

>>> FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
>>> FORMAT % {'asctime': '2017-02-11', 'clientip': '1.2.3.4',
              'user': 'who', 'message': 'blah'}
'2017-02-11      1.2.3.4 who      blah'
使用
%(键)d
/字典有一些好处:

  • 您不需要按顺序指定格式说明符
  • 您可以重复同一个键多次:
    '%(num)d+%(num)d=%(double)d“{num':3,'double':3*2}