I have a Python script that runs in an endless loop and can generate logs both in a logfile and on the console.
If I interrupt this script (in Pycharm), I only get some messages about my manual interruption in the console log, but not in the log file.
How can I get the same output in the logfile?
The script:
import logging
import os
my_log_file_name = os.path.basename(__file__) + ".my_log"
logging.basicConfig(filename=my_log_file_name,
filemode='a',
format='%(asctime)s,%(msecs)03d %(name)s %(levelname)s %(message)s',
datefmt='%D %H:%M:%S',
level=logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
# set a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s,%(msecs)03d %(name)s %(levelname)s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
logging.info("=================================================")
logging.info("starting execution")
while True:
pass
The output in the logfile:
03/31/18 19:27:34,335 root INFO =================================================
03/31/18 19:27:34,336 root INFO starting execution
The output in the console:
2018-03-31 19:27:34,335,335 root INFO =================================================
2018-03-31 19:27:34,336,336 root INFO starting execution
Traceback (most recent call last):
File "/home/user/4me/Technologie/0010-install/pycharm/2017-12-12-ultimate-edition/pycharm-2017.3/helpers/pydev/pydevd.py", line 1668, in <module>
main()
File "/home/user/4me/Technologie/0010-install/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/4me/Technologie/0010-install/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/4me/Technologie/0010-install/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/examples/my_script/attic.py", line 24, in <module>
pass
KeyboardInterrupt