Apps logs best practices

Hi,
I am on the personal plan right now, and I want to start tracking as much as I can about visitors right now.
Is there a way to show more data in the logs, like visitor IPs, device type, browser, screen resolution, etc? Can the data be downloaded as a file?
Also, since the logs show console output, what are some best practices to show a lot of good data from your app?

App logs cannot be downloaded and there are issues searching on them.

I would create a table to store whatever info you want to store, and a scheduled task to delete old lines nightly or weekly.

You can create your own add_to_log function and use that instead of using the print that logs to the app log. This will give you the flexibility of storing your info in columns rather than one long line of text, will allow you to download it, you will be able to share the same log table with multiple apps, and much more.

I have one log table that I share with many apps.
I have one logger app that allows to filter and dig through the log table.
The logger app has one nightly scheduled task that preserves the latest 200,000 lines.
The logger app has http endpoints that allow other tools like vba macros to log other events, so I can use the one stop shop logger app to monitor all my tools.

5 Likes

You have access to basic client info that the browser sends on the server. This includes IP address and approximate location. I use this function to convert it into a dict I can store in a simple object column:

def dump_to_json_dict(obj):
    result = {}

    for k,v in obj.__dict__.items():
        if not hasattr(v, '__dict__'):
            try:
                temp = json.dumps(v)
            except Exception as e:
                v = str(v)

            result[k] = v
            continue

        result[k] = dump_to_json_dict(v)

    return result

And I call it like this:

client_info=dump_to_json_dict(anvil.server.context.client)
5 Likes

Anytime that I run

print(anvil.server.context.client)

all I get as a result is

{'type': 'browser', 'location': None, 'ip': None}

You’re probably calling it from the client. Call it from the server to get the full info.

That worked.
Do you then add that data to a table?

Yes, in a simple object column