How to display country code information from Apache log

Are you looking for solution to display country code along with Apache log information for your analysis? This tutorial is going to shed you some lights on how to accomplish it. Apache log is a great resource that you can use it to study the traffics connected to your Apache server. The log provides you lots of useful information, such as IP address, useragent, HTTP status, and so on, but not for the country code information. For this, we are going to use IP2Location application to get the country code information for display together with Apache log information.

Basically for this tutorial, we are going to turn the below Apache log display (using tail command)

Apache log display

to prepend with the country code information. For example, to display FR in front of data row.
Apache log display

Below are the steps to produce the above result:

  1. You need to install the IP2Location application. You can download the application using the below command. You may also visit to find out the details.
  2. Unzip the tar file.
    tar -zxvf ip2location-7.0.0.tar.gz
  3. Run the below commands to install the IP2Location application
    cd ip2location-7.0.0
    make install
  4. Download the IP2Location DB1 data file. I will recommend you to use the commercial IP2Location DB1 package as it provides you high data accuracy. However, for this tutorial, I will going to use the IP2Location DB1.LITE for the tutorial in case you have no access to the commercial version.
    Download the DB1.LITE using the below command

  5. Unzip the file and you shall get the IP2LOCATION-LITE-DB1.BIN file in the folder.
  6. Create a bash script to prepend and named it as Open the bash script for editing. You can use any text editor for editing.
  7. Copy the following code and paste into the
    while read -a row; do
    	# do ip to location lookup. This command assume the db1.bin file located at the same folder of this script
    	country_code="$(ip2location --datafile IP2LOCATION-LITE-DB1.BIN --ip ${row[0]} --noheading --field countryshort --format TAB)"
    	#appending tab to recreate the apache log string
    	for item in "${row[@]}"
    	# prepend country code data to the apache string
    	echo $country_code$output_str

    The above code read all Apache log data from the input, then process them recursively line by line and run the ip2location command to retrieve the countryshort information. Note: countryshort contains the 2-characters country code following the ISO3166 standard. Finally, the script will output the Apache log data prepended with countryshortinformation. In addition to the country code information, you may prepend other geolocation data such as region, city, and so on using the ip2location command. Type man ip2location to learn more.

  8. Save the bash script.
  9. Run the bash script using the below command. Note: access.log was the example of the Apache log file.
    bash add-country-code < access.log
  10. You shall see the country code information prepended on the display.
    Apache log display

Happy coding!

Was this article helpful?

Related Articles