logging exceptions in Python 3 - python-3.x

I have the following :
### running with Python 3
import logging
logger = logging.getLogger(__name__)
logger.setLevel('DEBUG')
logger.exception('this is an exception')
The output is:
this is an exception
NoneType: None
What do i need to do to get rid of that "NoneType: None" output?

stdlib logging is trying to append the exception info, but you don't have any exception info because you're not logging from within an except: block. The NoneType: None ultimately come from the result of a sys.exc_info() call, logging doesn't bother to check whether they're valid or not.
Lame workaround:
logger.exception('this is an exception', exc_info=False)
Slightly less lame:
logger.error('this is an exception...haha not really')
Best:
try:
# risky code
except Exception:
logger.exception('this is an exception')
If you have an exception instance that was caught earlier, and you want to log it, and you're not in an except block, this will do the right thing:
logger.exception('this is an exception', exc_info=myerror)

Related

cbpro coinbase pro api Exception has occurred: KeyError 'open' in Python

In cbpro coinbase pro api I get this Error every once in a while.
Exception has occurred: KeyError 'open'
So, in order to try to go around it, I first tried to put in in a try: Exception: but I would still get the error, thus I tried to build a while as seen below.
try:
get24h = None
while get24h == None:
get24h = public_client.get_product_24hr_stats(product_id=symbol)
try:
open = None
while open == None:
open = get24h['open'] # HERE THE ERROR GETS TRIGGERED: Exception has occurred: KeyError 'open'
except ValueError as e:
print(f"ERROR GETTING 'open': {e}")
except ValueError as e:
print(f"ERROR GETTING get24h: {e}")
The thing is that since 'open' is not == None and I don't know what open is equal to when the error gets triggered, then I don't manage to find a way to avoid this from happened.
Furthermore, the code stops whenever I get this error, which is annoying
I can see why it's happening: I print the get24h right before I try to get the 'open' and I'm surpassing the 'Public rate limit'
get24h: {'message': 'Public rate limit exceeded'} open: None
Is there a way to go around this limit?
Now it's getting worse, because every time I load it, after just a few symbols, I get the error, so the code breaks, and I don't get to the results. This was working on and off until now, but I would manage to get the results several times per day without an error ( every 3 runs, one would fail), but now I don't even manage to make one to fully load.

python 3.9 logging module. How to limit stack trace output from loging.debug()

I currently am using the python logging module to log some of the potential errors/exceptions. However, there is an exception that I catch and output with log.debug which I do not want the whole stack trace (it's massive).
We have reviewed the exception and are fine just noting that it has happen, it does not affect the system or flow of the program.
except sql_a.exc.IntegrityError:
self.log.debug('insertion of non-unique row', df.iloc[[row]],
stack_info=False, exc_info=False)
Regarding the code snippet above, I have tried using exc_info=False and/or stack_info=False. However I am still outputting the whole stack trace after the except gets caught. I have read the documentation on exc_info and am assuming I am applying it correctly?
If exc_info does not evaluate as false, it causes exception information to be added to the logging message.
https://docs.python.org/3/library/logging.html#logging.Logger.debug
Any idea what I am doing wrong here? I went through a few stackoverflow posts on logging exceptions however I haven't really found what I am looking for/solved my issue, apologies if the question is repeated previously.
Edit:
handleError(record)
This method should be called from handlers when an exception is encountered
during an emit() call. If the module-level attribute raiseExceptions is False, > exceptions get silently ignored.
https://docs.python.org/3/library/logging.html#logging.Handler.handleError
I have also tried setting the attribute (handleError) to false but I still get a full stacktrace output to stdout/stderr.
stream_handler.raiseExceptions = False
handler.raiseExceptions = False
I found the reason for this error, because I haven't found a similar question/answer I am going to leave it up here. (Until someone decides to delete it, no hard feelings)
self.log.debug('insertion of non-unique row', df.iloc[[row]],
stack_info=False, exc_info=False)
The issue is because I wasn't following the format of parameters accepted by the logging.debug() module.
debug(msg, *args, **kwargs)
https://docs.python.org/3/library/logging.html#logging.Logger.debug
If I correctly format the parameters everything is good, for consistency sake I will just leave my current working example below:
self.log.debug('insertion of non-unique row:\n{}'.format(df.iloc[[row]]),
stack_info=False, exc_info=False)

Azure Function Python Exceptions not logging correctly to App Insights

I am seeing some unexpected behaviour when using logging.error(..) in a Python Azure Function.. essentially, the error is getting logged as a trace, which seems a bit odd.. it's the same w. logging.exception(..) too.
Some simple sample code:
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
logging.error('[Error] I am an error.')
return func.HttpResponse()
When this is executed, the following winds up in Application Insights.. why is this not available under exceptions?
Query for ref:
union *
| where operation_Id == 'ca8f0c7d7798a54996b486940641b159'
| project operation_Id, message, severityLevel, itemType, customDimensions.['LogLevel']
Any clues as to what I am doing wrong?? 👀
I am seeing some unexpected behaviour when using in a Python Azure
Function.. essentially, the error is getting logged as a trace, which
seems a bit odd.. it's the same w.
too.logging.error(..)logging.exception(..)
These will be classified as trace in Application Insights, the only manifestation is log level (at least Application Insights is like this). If you need this to classify it as 'exceptions' in Application Insights, you need to use below code(and don't deal with it, once you use try-except and other processing, you will enter the trace again):
raise Exception('Boom!')
But this will stop the function, so it doesn't make much sense. I suggest you search for ‘trace’ and set filters to extract exception or error levels.
Currently, the only way to log messages to exceptions table in application insights, is that put the logger.exception() method in the try except code block. Like below:
try:
result = 1/0
except Exception:
logger.exception("it is an exception 88 which is in the try block...")
then in application insights, you can find the messages in exceptions table:
There is an issue about this, let us wait for the feedback from MS.

Raise Exception from response message

How to raise an exception from the response message e.g {'status': "insufficient credit"}
try:
new_order = api.request(r)
except as :
print(new_order['status'])
else:
print(new_order)
The response message has different key/values when successful/unsuccessful.
I would need to know what api.request is to answer your question exactly.
Without know what it is, I would suggest using traceback with the error, which is in the standard python library.
for example:
import traceback
try:
new_order = api.request(r)
except Exception as err:
print(err)
traceback.print_exc()
else:
print(new_order)
Also, if api.request is raising an exception, it wouldn't be related to any "status message" they send. If they are telling you about the error, then api.result will likely return an object/list with the status code and/or error message in it, and not raise an exception on your server (which I don't think is possible for them to do).
Think of it like this: your server side code executes api.request, which is probably either a GET or POST request to the api's server. If there was an error in your request, they will send you a message saying what the problem is (I assume). No where in this process will an exception be raised.
If the request is sent to their server, there shouldn't be any exceptions raised on your server.
an exception is raised when you call raise 'some exception here', or when there is a syntax error/other serious error. In either case, your code execution is immediately stopped, and your server would respond to your client with a 500 internal service error.

Python logging, supressing output

Here is a simple example that gets a new logger and attempts to
import logging
log = logging.getLogger("MyLog")
log.setLevel(logging.INFO)
log.info("hello")
log.debug("world")
If I call logging.basicConfig(level=logging.INFO) right after importing, "hello" will print, but not "world", (which seems strange since I set the level to debug).
How can the logging API be adjusted so all built-in levels are printed to the stdout?
If you call basicConfig with level X none of the log messages that is not covered by X will be printed.
You called logging.basicConfig(level=logging.INFO) Here, logging.INFO doesn't cover logging.DEBUG.
May be you wanted other way round?
logging.basicConfig(level=logging.DEBUG)
This prints both info and debug output:
import logging
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger("MyLog")
log.setLevel(logging.DEBUG)
log.info("hello")
log.debug("world")

Resources