Maybe I’m missing something, or perhaps this is really a feature request. Not sure, so I will start in Q&A…
While the current interactive access to App Logs is fine for development and some monitoring, it’s not great for being notified of errors over time. ie in the weeks and months, after your actual users start to use an app.
And sometimes (like last week and over the weekend) changes within the Anvil platform itself cause new and unforeseen errors, which could be interesting to both developers and anvil staff. (database transactions)
Is there currently a way to access the App Logs data from inside an App, via a database query, or via remote API calls?
If there is, please tell me more, or point me to the place where it’s documented!
If not, then this would become a feature request.
This would be a very key feature to have an inbuilt logging feature, a bit better than what Anvil currently offers. I once encountered a similar situation in one of my apps and I resolved it by creating my own logging system.
So I created a data table that logs all the interactions in the system, including errors.
The table had just four columns:
Date
Time
IP address and
Message
** Date and time in UTC.
based on my need at the time. So if an exception ever occurred, I would log it in the database, including the kind of exception and the reason.
I’ll call the app_log function whenever logging was required.
So if a user ever reported an error, I could filter through the logs and know exactly what went wrong.
This is not a very effective solution but it did the job for me. One way to improve this would be to add a column for error codes. So whenever a user encounters an error, the app would display an error message with an error code. And using that code, the user can report the error or use it for reference. There’s lots of room for improvements.
#Example one
@anvil.server.http_endpoint("/user/delete/", methods='POST')
def delete_user( **params):
try:
##Do something here that might raise an exception, for example
quotient = 10 / 0 //This will raise a division by zero exception
except Exception as ex:
##Logging the error using the above app_log function
ip_address = anvil.server.request.remote_address
app_log(ip_address, "ERROR: " + str(ex))
##If you want to get notified when this error occurs, you can send yourself an email here
anvil.email.send(to="noreply@email.com", # Change this to your email address!
subject="(URGENT) ERROR NOTIFICATION",
text="Put your text here")
return anvil.server.HttpResponse(500, "Oops, an internal server error occurred")
#Example two
def divide_two_numbers(num1, num2):
try:
#If num2 is zero, this will raise an exception
quotient = num1 / num2
return quotient
except Exception as ex:
##Logging the error using the above app_log function
ip_address = "10.34.56.43"
app_log(ip_address, "DIVIDE BY ZERO ERROR: " + str(ex))
##If you want to get notified when this error occurs, you can send yourself an email here
anvil.email.send(to="noreply@email.com", # Change this to your email address!
subject="(URGENT) ERROR NOTIFICATION",
text="Put your text here")
return None
Some have suggested that log entries be written in JSON format. That way, you can leverage Python’s JSON support to easily get it back as a Python dict. That is, the parts of the log entry are handily available by name.
With Anvil, it can be even easier. If the app_tables.logs table has a column of type SimpleObject, then Anvil will take care of converting between JSON (database) and dict (Python) formats for you. As far as your code is concerned, you are storing and retrieving log entries simply as dict values.