Downloading Sources of Required Libraries with Scala and SBT

This one is mostly a reminder to myself, as I keep forgetting what needs to be done, to download the sources of required Scala libraries into Eclipse.

Assumption One: You are already using SBT to build your project.

Assumption Two: You are already using sbteclipse to create eclipse project files.

And then: Add EclipseKeys.withSource := true to your build.sbt (more about Settings)

ISO-3166-1 Country List as SQL Script

How often do you need a drop-down in your application that displays all countries? And in most cases you need it in more than one language, right. I’ve been there a couple of times and finally decided to publish an sql script that could be used to create a countries table and fill it with all countries according to ISO-3166-1.

It took me some time to scan different sources and create my own countries.sql. And that was the main reason for setting up this post, I couldn’t find a page or blog post that had a ready-to-apply SQL script. When I was looking for the spanish translations, I finally stumbled upon Mega Database and its wonderful lists of everything. So you can either go to their page and get your own export of the data, or simply download the script I have attached to this post.

Thanks to http://www.mega-db.com.ar for the lists and enjoy displaying those country drop-downs.

Faster logging with SLF4J

Logging in Java has a long and obscure history. There are numerous APIs for logging, and there is for sure no answer which one is the best. Today, I started a project from scratch, which gave me the opportunity to try out SLF4J (Simple Logging Facede 4 Java). In most of my projects I have been using  Log4J so far. The switch was rather simple, anyways I would like to write down the most important steps here.

Why SLF4J

  • Log4J was initially created by Ceki Gülcü. After Log4J Ceki created SLF4J, so I guess he put everything he learned from Log4J into SLF4J.
  • SLF4J provides a nice way of creating short and easy to read logging statements in the code, that have only minor resource consumption if logging is turned off.
  • Some commonly used open-source projects use SLF4J compared with some of its provider bridges, in seldom cases this might struggle using the bridged logging APIs.
  • Last but not least: Why not? As previously mentioned, there is no best solution for logging, so why not trying alternatives.

How does it work

As the name suggests SLF4J is a logging facade that needs an actual logging implementation to be able to log anything at all. All the widespread logging solutions can be used by SLF4J. So you might use Log4J or JCL or even the Simple Implementation to do logging with SLF4J. In any case you will use the same API calls in your code (thus facade in the name).

Get the JARs

If you configure your JARs by copying them into some lib folder (I hope nobody is actually doing that any longer!), just go to their web page and download the latest versions of slf4j-api and the slf4j-{your favorite provide}. Don’t forget to put the JAR of your provider into the lib folder as well.

If you use maven or ivy, here is the sample configuration for SLF4J backed by Log4J. Please note, that the provider JAR will be automatically retrieved, so do not put it into your maven dependencies.

<properties>
    <slf4j.version>1.6.0</slf4j.version>
</properties>
...
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>${slf4j.version}</version>
</dependency>

Let’s log

Logging with SLF4J is pretty straight forward. Just have a look at the example below. The second logging statement shows how the API should be used to avoid to String conversion for large objects if logging is turned off (or the level is lower). Check the SLF4J FAQ for further details on performance of (non) logging. If you are wondering if you should declare your logger as a static or instance variable, make sure to look at that consideration.

final static Logger logger = LoggerFactory.getLogger(Slf4jTest.class);

public void someMethod(HeavyObject object) {
    logger.debug("This is a simple debug message");
    logger.debug("HeavyObject is {}", object);
}

Does anyone not know about Stack Overflow?

Over the last month I have noticed that during research in the Internet, which in most cases mean copying some stack-trace or error messages into Google and hit enter, more and more results are from a page called stack overflow. So what is stack overflow and why it is worth to have a look?

In one sentence Stack Overflow is a answer and question platform for programming related topics, so nothing special… but… there is something more. In the words of the authors: “The only unusual thing we do is synthesize aspects of Wikis, Blogs, Forums, and Digg/Reddit in a way that is somewhat original.” Indeed it does make it original. As I said in the last month I stumbled upon that page more and more. Right now it is my first source to be looked up. There are 615,130 questions right now, which reflects a growth of about 100.000 questions in the last 10 weeks. The tagging systems makes it easy to find content and the reputation system motivates people to create content.

Overall the page (and the philosophy behind it) has a big potential. I havent been that amazed by a page since wikipedia went famous. So far I am already in the addicted state and have to be careful not to spend to much time on answering questions, adding comments or re-tagging content. So if you are interested as well, have a look! But you did already know the site, didnt you?

http://stackoverflow.com