0

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)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
PaleNeutron
  • 2,543
  • 4
  • 25
  • 43
  • 1
    Without knowing how you have configured the logger, it's hard to tell whether you should get any output at all? As BrianO indicates, `logging.debug` is by default not logged. And you mention a log file, but the default logging output is `sys.stderr`, not a file. –  Jul 22 '15 at 02:57
  • There is also quite a difference between interrupting a process using control-C and a processing being killed. I don't know about Windows (which you appear to be using), but on e.g. *nix, there is a variety of ways to kill a process (that is, a variety of signals can be send to a process that may stop that process). You'll need to be more clear whether you're only talking about control-C (which can be intercepted using `except KeyboardInterrupt`) or other interrupts. –  Jul 22 '15 at 03:00
  • @Evert, I found my stupid mistake. Thx for your help! – PaleNeutron Jul 23 '15 at 01:28

2 Answers2

1

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)
BrianO
  • 1,496
  • 9
  • 12
0

For the CTRL + C event use a try-except to catch the KeyboardInterrupt exception.

cdonts
  • 9,304
  • 4
  • 46
  • 72
  • @PaleNeutron You're defining the `handler` function but what about the previous `ctypes` calls and all that stuff? Also, why are you using `_thread` instead of `thread`? You can't do anything if your process is killed. – cdonts Jul 22 '15 at 00:34