Search code examples
pythonloggingio

How can I save my log file in Python when the process is killed


I am learning the logging module in Python.

However, if I log like this

logging.basicConfig(filename='mylog.log',format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG)

while 1:
    logging.debug("something")
    time.sleep(1)

and interrupt the process with control-C event(or the process is killed), nothing I can got from the log file.

Can I save the most logs whatever happens?

————

EDIT

the question seem become more complex:

I have imported scipy, numpy, pyaudio in my script, and I got:

forrtl: error (200): program aborting due to control-C event

instead of KeyboardInterrupt

I have read this question: Ctrl-C crashes Python after importing scipy.stats

and add these line to my script:

import _thread
import win32api
def handler(dwCtrlType, hook_sigint=_thread.interrupt_main):
    if dwCtrlType == 0: # CTRL_C_EVENT
        hook_sigint()
        return 1 # don't chain to the next handler
    return 0 # chain to the next handler

then:

try:
    main()
except KeyboardInterrupt:
    print("exit manually")
    exit()

Now, the script stops without any info if I use ctrl+C. print("exit manually") does not appear. Of course, no logs.

Solved

A stupid mistake! I run the script when working directory is System32 and want to find log in the script's path.

After I change the route like this, all is well.

logging.basicConfig(filename=os.path.dirname(sys.argv[0])+os.sep+'mylog.log',format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG)

Solution

  • When you log using logging.debug, logging.info, ..., logging.critical, you're using the root logger. I assume you're not doing anything to configure logging that you haven't shown, so you're running with the out-of-the-box, default configuration. (This is set up for you by the first call to logging.debug, which calls logging.basicConfig()).

    The default logging level of the root logger is logging.WARNING (as mentioned in e.g. https://docs.python.org/3/howto/logging.html#logging-basic-tutorial). Thus, nothing you log with logging.debug or logging.info will appear :) If you change logging.debug to logging.warning (or .error or .critical), you will see logging output.

    For your code to work as is, set the logging level of the root logger to logging.DEBUG before the loop:

    import logging
    import time
    
    # logging.getLogger() returns the root logger
    logging.getLogger().setLevel(logging.DEBUG)
    
    while 1:
        logging.debug("something")
        time.sleep(1)