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)
3
  • 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. Commented Jul 22, 2015 at 2: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. Commented Jul 22, 2015 at 3:00
  • @Evert, I found my stupid mistake. Thx for your help! Commented Jul 23, 2015 at 1:28

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.