App Logs visibility

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.

regards
Mark

1 Like

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.

    Find below a code snippet:

    ###This method logs usage data
    def app_log(ip_address, message):
      current_date = datetime.datetime.today().strftime('%Y-%m-%d')
      current_time = datetime.datetime.today().strftime('%H:%M')
      
      app_tables.logs.add_row(date=current_date, time=current_time, ip_address=ip_address, message=message)
      return
    

    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.

    I hope this helps.

    3 Likes

    This is awesome, I am quite new with the exception, would you mind showing how to log it in the database with the app_log function? Thank you

    Sure @Tony.Nguyen, find the code below.

    #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
    

    To read more about catching exceptions in python, read this https://www.w3schools.com/python/python_try_except.asp

    1 Like

    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.

    1 Like

    Thanks @Agobin, really appreciate that.