'Is there a way on python logging or graypy handling socket error
import logging
import graypy
my_logger = logging.getLogger('test_logger')
handler = graypy.GELFUDPHandler('localhost-rng', 12201)
my_logger.addHandler(handler)
my_logger.info('Hello Graylog.')
So my problem here is that if I put host that is non existant or not setup I get an exception which looks something like this:
--- Logging error --- Traceback (most recent call last): File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/logging/handlers.py", line 632, in emit self.send(s) File "/Users/stackoverflow/PycharmProjects/stackoverflow-project/venv/lib/python3.8/site-packages/graypy/handler.py", line 611, in send super(GELFUDPHandler, self).send(s) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/logging/handlers.py", line 690, in send self.sock.sendto(s, self.address) socket.gaierror: [Errno 8] nodename nor servname provided, or not known Call stack: File "/Users/stackoverflow/PycharmProjects/stackoverflow-project/some_projects/module_logger/mb_logging.py", line 78, in logger.info("HI") File "/Users/stackoverflow/PycharmProjects/stackoverflow-project/some_projects/module_logger/mb_logging.py", line 62, in debug self.root.debug(message) Message: 'HI' Arguments: ()
So I can see that there is socket library error. My problem is that I don't it to be sdout error (or whatever it's called). I want to catch this error with try, except block, but I just cant seem to do it, probably because it UDP thing something like that?
what I tryed:
try:
logger.debug("HI")
except:
# this never works even if there is exception
print('Exception')
I expect Exception block to work. Maybe I could even create a decorator around info that for example could ping to server if it response or something like that?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
