I want to redirect the print()
statements of some arbitrary code to a proper logging functionality.
Unfortunately, the following code works only when print()
gets a single argument.
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")
This results in the following logfile:
03/30/18 14:06:11,881 root DEBUG hallo
And the following error in the console:
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
How do I have to change the code to make it work for an arbitrary number of arguments of print()
?