It often happens that we need to gather logs from several sources. Centralising them in one simple server place is the purpose of the log.jod.li initiative. The logs are then visible almost instantaneously on the log.jod.li page or can be retrieved in a format similar to CSV (raw logs). For more details, please visit https://log.jod.li .
This page aims at showing how to use log.jod.li logs centralisation solution using Python code.
Let’s say you have some Python code that may not always run as expected. You want to track the unwanted outcomes and centralise their reading on log.jod.li . You could read your Python logs from Python dedicated solutions but you would not be able to see the logs at the same place from your Python programs and those written in completely different technologies. So, if you wish to centralise your logs from Python program, you can use a simple GET call to a URL containing:
- The server name to send the log to the right location. This will be en.log.jod.li .
- The user name you created on log.jod.li .
- The user key you received in your email box when you created your user. Make sure nobody else gets access to your key.
- The log message that you want to record on the log.jod.li server. The maximum length of the message is 1700 characters. It must be passed url encoded so that the string is interpreted correctly by the server.
- Optionally the application name so that you can filter your logs more easily. Max 32 characters.
- Optionally the client guid so that you can filter on a given client. Max 32 characters.
Here is an example of URL:
Here, the user is “toto”, the key is “1234567890123456”, the application name is “AppName”, the client guid is “ClientGUID” and the log message is “testing”.
Send log with a GET call
What we can do to make use of log.jod.li in this case is using a simple function to call a URL similar to the one mentioned above. Here is the function and the call to it:
import urllib2 import urllib def log( message ): "sends the message to the centralised log through a HTTP GET call" contents = urllib2.urlopen("https://en.log.jod.li/?api=a&u=toto&k=1234567890123456&c=ClientGUID&a=AppName&m="+urllib.quote_plus(message)).read() log("Message sent through GET")
Here is the outcome of the call on log.jod.li web page:
2018-09-22 11:58:09 ClientGUID AppName Message sent through GET
Here is the outcome in the raw CSV format:
2018-09-22 11:58:09,ClientGUID,AppName,Message sent through GET
This is a demonstration that you can log your messages by simply calling a url from Python code. This is especially useful if you have several technologies for which you need to read logs.