Python Forum
Windows compatibility - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Windows compatibility (/thread-34473.html)

Pages: 1 2


RE: Windows compatibility - Astrikor - Aug-04-2021

Hmmm Thanks for the tip-off Da
I will look into the "import logging" module to trap warnings/errors/critical events.

Astrikor


RE: Windows compatibility - Astrikor - Aug-10-2021

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


RE: Windows compatibility - buran - Aug-10-2021

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')



RE: Windows compatibility - Astrikor - Aug-10-2021

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


RE: Windows compatibility - ndc85430 - Aug-10-2021

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!


RE: Windows compatibility - buran - Aug-10-2021

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/


RE: Windows compatibility - Astrikor - Aug-10-2021

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


RE: Windows compatibility - buran - Aug-10-2021

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


RE: Windows compatibility - Astrikor - Aug-10-2021

Thanks for your help guys

I'll keep trying!

Astrikor


RE: Windows compatibility - Luckk - Aug-10-2021

Thanks for this information! Glad to be here.
employee monitoring software