How to save information from the apps log in a data table

Hi
I have a data table in which I store information about how my users interacts with my app.
It would be nice to include information about nationality of the user in this table.
In the app logs I can see session id and browser nationality, but I haven’t been able to find out how I can access this information and store is in my data table.
Does anybody know how to do this?
Thanks.
Casper

I use this: Country Info API | BigDataCloud

I am well below the 10,000 monthly queries, so I’m in the free plan.

I think we are meant to take his topic title literally.

Thanks for your response.
Yes, I was a bit confused by the answer from Stefano. I don’t want additional country information. I just want to store the information that is already shown in the apps log in a data table. Is that possible?

I didn’t say anything about how to store info in a data table because I thought you already were doing it.

I told you what I use to get the only thing you miss, the nationality info.


Here are more details:

You can’t read from the app log.

The app log has a little info about the time of the request and about where it comes from. Plus it has all you print to it.

You can add to your table whatever you print to your log. It looks like you are already doing this.

You will need another way to find the part you miss, the country info.

All you need to do is to sign up with a service like BigDataCloud, ask for the location of the requesting IP address and add it to your table. Now your table and the app log are identical:

ip = anvil.server.context.client.ip
response = requests.get(f'https://api.bigdatacloud.net/data/ip-geolocation-with-confidence?ip={ip}&localityLanguage=en&key={BIGDATACLOUD_KEY}')
data = response.json()
app_tables.my_log.add_row({
  'city': data['location']['city'],
  'country': data['country']['name'],
})

I store the info of every IP address in a global dictionary. My apps are used by a few users, so a handful of requests per day are enough to add what’s missing to the local dictionary. Then all the requests coming from an IP address only require a dictionary lookup to find where they are coming from.


The app log also contains info about app failures (not mentioned in the original post), and they are impossible to get.

You could get some of them (not all) by managing all the errors and getting the info about the error and the traceback as described in this post.

Thanks a lot. I think this is exactly what I’m looking for. Sorry for not getting it in your first reply.
I’m really a newbie when it comes to this stuff.

1 Like

Sometimes you add too much detail and they look at you like “who do you think I am”.
Sometimes you don’t add enough and…
:slight_smile:

2 Likes

It all seems to work really well after using BigDataCloud. However, after implementing this in a server module, when I go directly to the webpage (chessart.net) I get a privacy error, see below. The error is there in google Chrome but not in Microsoft Edge. Have you experienced this?

Yes, I only do this on the server side.

Requests to other domains from the client often trigger security problems and I stay away from them.

If you really need to do it on the client, try to do some research on CORS on Google and on this forum.

Can’t you use anvil.server.context.client.location to get the location data?

The results are a bit… weird.
<Location:{'city': 'Newtown', 'subdivision': 'Pennsylvania', 'latitude': 40.2673, 'longitude': -74.9487, 'country_code': 'US', 'country': 'United States'}>

These are my results, and they are about 60 miles away from where I really am, and also in the wrong state.

4 Likes

I didn’t know anvil.server.context.client.location existed!

That’s easier!

3 Likes