Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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

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 如何将打印重定向到具有多个打印参数的日志记录?_Python_Logging_Arguments - Fatal编程技术网

Python 如何将打印重定向到具有多个打印参数的日志记录?

Python 如何将打印重定向到具有多个打印参数的日志记录?,python,logging,arguments,Python,Logging,Arguments,我想将一些任意代码的print()语句重定向到适当的日志功能 不幸的是,以下代码仅在print()获取单个参数时有效 import logging logging.basicConfig(filename="logname", filemode='a', format='%(asctime)s,%(msecs)03d %(name)s %(levelname)s %(message)s',

我想将一些任意代码的
print()
语句重定向到适当的日志功能

不幸的是,以下代码仅在
print()
获取单个参数时有效

import logging

logging.basicConfig(filename="logname",
                    filemode='a',
                    format='%(asctime)s,%(msecs)03d %(name)s %(levelname)s %(message)s',
                    datefmt='%D %H:%M:%S',
                    level=logging.DEBUG)

print = logging.debug
print("hallo")
print("makes", "error")
这将生成以下日志文件:

03/30/18 14:06:11,881 root DEBUG hallo
以及控制台中的以下错误:

Connected to pydev debugger (build 173.4674.37)
--- Logging error ---
Traceback (most recent call last):
  File "/home/user/anaconda3/lib/python3.6/logging/__init__.py", line 992, in emit
    msg = self.format(record)
  File "/home/user/anaconda3/lib/python3.6/logging/__init__.py", line 838, in format
    return fmt.format(record)
  File "/home/user/anaconda3/lib/python3.6/logging/__init__.py", line 575, in format
    record.message = record.getMessage()
  File "/home/user/anaconda3/lib/python3.6/logging/__init__.py", line 338, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
  File "/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/pydevd.py", line 1668, in <module>
    main()
  File "/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/user/PycharmProjects/my_project/attic.py", line 12, in <module>
    print("makes", "error")
Message: 'makes'
Arguments: ('error',)

Process finished with exit code 0
连接到pydev调试器(build 173.4674.37)
---记录错误---
回溯(最近一次呼叫最后一次):
文件“/home/user/anaconda3/lib/python3.6/logging/_init__.py”,第992行,在emit中
msg=self.format(记录)
文件“/home/user/anaconda3/lib/python3.6/logging/_init__.py”,第838行,格式为
返回格式(记录)
文件“/home/user/anaconda3/lib/python3.6/logging/_init__.py”,第575行,格式为
record.message=record.getMessage()
getMessage中的文件“/home/user/anaconda3/lib/python3.6/logging/_init__.py”,第338行
msg=msg%self.args
TypeError:在字符串格式化过程中并非所有参数都已转换
调用堆栈:
文件“/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/pydev.py”,第1668行,中
main()
文件“/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/pydev.py”,第1662行,主文件
globals=debugger.run(setup['file'],None,None,is_模块)
文件“/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/pydev.py”,第1072行,运行中
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“/home/user/install_dir/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/_-pydev_-imps/_-pydev_-execfile.py”,execfile中第18行
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“/home/user/PycharmProjects/my_project/attic.py”,第12行,在
打印(“生成”、“错误”)
信息:“制造”
参数:('error',)
进程已完成,退出代码为0

如何更改代码以使其适用于任意数量的
print()

print
不再是它自己,因为您将
print
重新定义为
logging.debug
debug
仅将第一个参数作为消息。但是,您可以以不同的方式定义
打印
,如下所示:

def print(*args, sep=" ", **kwargs):
    return logging.debug(sep.join(map(lambda x: str(x), args)), **kwargs)
这将失去使用
debug
*args
格式化字符串的能力

args
是使用字符串合并到
msg
中的参数 格式化运算符。(请注意,这意味着您可以使用关键字。) 格式字符串中,以及单个字典参数。)



与其重新定义
打印
,我建议您使用。

谢谢,这已经非常有用了。现在我谈到了下一个问题:使用您的解决方案,
dict
无法处理。你知道如何解决这个问题吗?哎呀,我忘记了一件重要的事情,我相信我通过连接所有参数的字符串表示法(这是原始的
打印
所做的),修复了它,你可以再试一次。