Centralise Java logs using log.jod.li

log.jod.li

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 .

Java situation

This page aims at showing how to use log.jod.li logs centralisation solution using Java code.

Let’s say you have some Java 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 Java logs from Java dedicated solutions but you would not be able to see the logs at the same place from your Java programs and those written in completely different technologies. So, if you wish to centralise your logs from Java 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:

https://en.log.jod.li/?api=a&u=toto&k=1234567890123456&a=AppName&c=ClientGUID&m=testing

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 java.io.*;
import java.net.*;

public class LogToJodli
{
	public static void main(String[] args) throws Exception {
		log("Message sent through GET");
	}

	public static String log(String message) throws Exception {
		StringBuilder result = new StringBuilder();
		URL url = new URL("https://en.log.jod.li/?api=a&u=toto&k=1234567890123456&c=ClientGUID&a=AppName&m=" + URLEncoder.encode(message, "UTF-8"));
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		String line;
		while ((line = rd.readLine()) != null) {
			result.append(line);
		}
		rd.close();
		return result.toString();
	}
}

Here is the outcome of the call on log.jod.li web page:

2018-09-22 10:55:49	ClientGUID	AppName	Message sent through GET

Here is the outcome in the raw CSV format:

2018-09-22 10:55:49,ClientGUID,AppName,Message sent through GET

Conclusion

This is a demonstration that you can log your messages by simply calling a url from Java code. This is especially useful if you have several technologies for which you need to read logs.