0

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
  • you can make a signal handler for Ctrl+C and log it(code frame) into log file. for example: https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python – CSJ Mar 31 '18 at 18:02

2 Answers2

0

Actually all your logs are being saved in your file. The reason the console output is different from your file is that the part after traceback is not part of your log but an uncaught exception (In this case caused by your interrupt).

If you want to save that to your file as well you need to surround your while loop with try except block and print the exception traceback to both console and tour file.

e.g

import traceback

...

try:
    // while loop
except KeyboardInterrupt as e:
    // print traceback.format_exc and write it to your log file
except Exception as e:
    // print traceback.format_exc and write it to your log file
bezirganyan
  • 404
  • 6
  • 16
0

The try..except block can easily capture KeyboardInterrupt exceptions. Here's what has worked:

logging.info("=================================================")
logging.info("starting execution")

try:
    while True:
        pass
except KeyboardInterrupt:
    logging.exception('KeyBoardInterrupt captured!')
    raise

#Output:

03/31/18 14:25:20,344 root INFO =================================================
03/31/18 14:25:20,344 root INFO starting execution
03/31/18 14:25:22,003 root ERROR KeyBoardInterrupt captured!
Traceback (most recent call last):
  File "bla.py", line 25, in <module>
    pass
KeyboardInterrupt
amanb
  • 5,276
  • 3
  • 19
  • 38