Python logging to remote devices using Matrix / Element
in Blog
Sometimes it is useful to be able to send (logging) information to another remote device from the machine that is running your code. For example you might want to notify yourself
- that your code finished running
- that an error occurred and you need to check it
- about the progress, since you’re excited about results
- …
Using Matrix and Element is a good choice here, I think. You can easily create an account by only providing a name and a password - no other information required. Since Element is cross-platform (i.e. Web, Android, iOS, macOS, Windows & Linux), you can send and retrieve encrypted messages from and to anywhere.
To be able to use Matrix from within Python, we need two things:
- your account credentials a.k.a. token
- the room id of a (public or private) room that you want to send your messages to
- create a room (
Rooms
→Add room
) and - retrieve its id (
room settings
→Advanced
→Internal room ID
)
- create a room (
Then you can use the following code
Note that having your matrix token is equivalent to having your user name and password, thus, you should not publish it anywhere and best you read it from an external source. Also keep the room id private if the messages are only meant for you.
If you know better or other ways for sending quick information across devices from your code, please let me know :)