Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Windows compatibility
#11
Hmmm Thanks for the tip-off Da
I will look into the "import logging" module to trap warnings/errors/critical events.

Astrikor
Reply
#12
My attempts at logging have not been very successful.
I have the following code which concludes with an intentional attempt to print a divide-by-zero result to test the logger:

import os, logging, logging.handlers

handler = logging.handlers.WatchedFileHandler(
    os.environ.get("LOGFILE", "E:/Faults.log"))
formatter = logging.Formatter(logging.BASIC_FORMAT)
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", "DEBUG"))
root.addHandler(handler)
logging.debug("Logger check")

print(1/0)
Running this gives the following Console result:

Error:
Traceback (most recent call last): File "E:\FaultLogger.py", line 12, in <module> print(1/0) ZeroDivisionError: division by zero
But the Fault.log file only records :

Output:
DEBUG:root:Logger check
but the debug does not recognise print(1/0) as a ZeroDivisionError.

It seems simple, but something is clearly wrong.

Any suggestions welcome!

Thanks
ASTRIKOR
buran write Aug-10-2021, 09:18 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#13
It looks there is some misunderstanding how logging works.
print(1/0) produce unhandled error and program ends with the traceback you show.

You need to handle the error with try/except/else/finally block (note, not all 4 are mandatory) and only if you want you can log something, e.g.

import os, logging, logging.handlers
 
handler = logging.handlers.WatchedFileHandler(os.environ.get("LOGFILE", "E:/Faults.log"))
formatter = logging.Formatter(logging.BASIC_FORMAT)
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", "DEBUG"))
root.addHandler(handler)
logging.debug("Logger check")
try:
    print(1/0)
except ZeroDivisionError:
    logging.critical('Division by zero')
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
I used the Division-by-zero as an example to test the logger.
I expected that by setting level to DEBUG it would automatically register higher levels.
How to achieve that?

Astrikor
Reply
#15
Yes, if the level is set to debug, anything logged at higher levels will appear. As pointed out above, you have to log what you need at whatever level is appropriate. It's not the logging library's job to know what is important to you!
buran likes this post
Reply
#16
Again, there is misunderstanding - the fact that you have set a logger, DOES NOT mean that it will catch/handle automatically any error.
Error handling and logging are two different things.

the fact that you set this particular WatchedFileHandler handler to DEBUG level means that any logging will be done to this handler (because it will handle DEBUG level and above). Imagine you have another handler (e.g. console handler) set to different level, e.g. info, in which case it will handle any logs at level INFO and above, but not DEBUG - i.e. DEBUG level will be written in the file, but not to console.

Again, having a logger is NOT error handling (although you will certainly write to log if you get error). You can handle the errors, without logging them/anything and also you can have a logger and not handle any error (like you do in your code).

read https://docs.python.org/3/tutorial/errors.html and https://realpython.com/python-exceptions/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
Going back to my original query, the program hangs with Red "Close" button in the Tkinter window. I am told that this is not caused by Windows, but has to be a coding issue.

However, without any knowledge of what the fault might be, I need a debugger which will find any kind of fault in real time.

Incidentally, all potential errors are dealt with in the code using the try/except procedure. The Red Button problem is a rare occurrence of something else.

I had hoped that the logging module might achieve this, especially when I understood that setting a low level would record all higher faults.

My simple FaultLogger.py example was an attempt to check this out.

Not sure where to go from here.....

Astrikor
Reply
#18
Without seeing any code (which you claim is proprietary) it's difficult to tell. It's clearly problem with your [Tkinter] code and as @DeaD_EyE suggested it's probably problem with blocking the main threat.
(Aug-10-2021, 10:55 AM)Astrikor Wrote: Incidentally, all potential errors are dealt with in the code using the try/except procedure.
You can try to add logging to your code and log any errors dealt in the code with try/except by adding logging in the except block
However I am not sure this will help. You may be blocking the main threat without raising an error
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#19
Thanks for your help guys

I'll keep trying!

Astrikor
Reply
#20
Thanks for this information! Glad to be here.
employee monitoring software
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  deactivate compatibility pop up with excel files Krszt 0 1,883 Mar-19-2019, 06:39 AM
Last Post: Krszt
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,785 Jul-07-2017, 08:59 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020