My new favorite log4j conversion pattern

log4j is a powerful logging utility for the Java programmers. Until now, I let other developers configure it. However, I was not satisfied with the configuration in my current project and took a deeper look.

The log4j pattern

Our logs were cumbersome, always displaying the full package, which is not always interesting. Dates varied from a log to another. So I looked for a standard conversion pattern I could use whenever I have to log something with log4j, and after some diving in the documentation, here is what I came up with:

An example of output

Some explanation

  • %d{ISO8601} – displays the date and time in the ISO format (see earlier post about this norm).
  • %-5p – the log level (such as DEBUG, INFO, WARN, …), formatted to occupy five columns (I like when things keep in line).
  • %t – the name of the thread logging this line.
  • %c{1} – the name of the class calling the logger (the {1} argument is there to restrict the number of packages displayed; {2} would display one package level before the class name, {3} would display two package levels, … and %c without the brackets would display the full package and class names).
  • %M – the method.
  • %L – the line number.
  • %m – the message to log.
  • %n – begin a new line.

There you are.

Compare implementations of a method in Java

This post is history. Read the latest version.

 

Often at work, I look at the code from the legacy of my project and wonder: was this coded this way for a purpose? Is it optimization? And doubt is upon me. My way seems better, but what if it is not? Well, let us just have a quick comparison!

In this article, I will give you the comparison class I created for myself. Quite simple, focused on time (not the best indicator, maybe, but a clue nonetheless). You just have to supply two methods, configure how you want to run it, and run it. Interested?
Continue reading Compare implementations of a method in Java

Game of Thrones has returned

We welcome you to the house Keyboard Playing.

JoinTheRealm_sigil_KeyboardPlaying

Sorry for this parenthesis, but since Game of Thrones is back (even though we will have to wait some time to have it here in France), I just wanted to try out http://www.jointherealm.com/ (a webpage HBO created so you can make your own Game of Thrones like coat of arms) and wished to share the result.

As for the series itself, the season premiere established a new piracy record, and the contract for season 4 has been signed. Quite impatient to see that…

Writing numeric dates…

Numeric dates can be a pain. Look at it: in France, today is 27 février 2013, which turns to 27/02/2013. In the United States, it is February 27th, 2013, which gives 02/27/2013. Sometimes, you do not know which format you are reading: is 02/03/2013 February 3rd or March 2nd?

Well, I discovered only today there is a global ISO standard for writing numeric dates:

XKCD - ISO 8601
XKCD – ISO 8601

This is the format I thought was the most logical when I looked for one at the beginning of my programming learning. As for a number, you go for the most rough value, and you gradually become more precise. You first write the largest unit (the year, or the hour in the case of time), and you go step by step to smaller units (month/minutes, then days/seconds, then time/fractions of seconds).

This is so logical. I mean, we already do it for time everywhere, or even decimal numbers, why is it not applied to dates?

Plus, have you ever tried to sort dates saved as strings? With this format, you can just sort them alphabetically. It is quite convenient, is it not?

For more information about the rules for writing date and time according to the ISO 8601, I suggest you take a look at the corresponding wiki page.

Typing every day…

Good things about a keyboard is to use it regularly. Don’t you enjoy it?

More seriously, I used to have a diary. A leather-bound notebook. Thing is, the times when I wrote were quite random, and a year could pass without me writing anything. Why? Maybe because it was much of a constraint for something I would rarely or never look back to. Plus, no matter how I liked the use of a good ol’ fountain pen, it was quite a hassle and did not fit mobility.

So I searched to go mobile and checked what was available on the internet. This is how I met 750words. But what is it?

Basically, it is a website which encourages you to write at least 750 words every day (now you get the name). How can they? Well, streak points, badges, … Looks stupid, maybe, but I know my weak points, and I know from video games I like to collect such awards, so I just dived in.

Streak on 750words

What features can you expect besides these awards? Well, first, for geeks, Markdown is fully supported.

For others, each day, a graph is drawn, analyzing the content of your writing. Well, analysis by automated dictionaries and algorithms are more or less accurate, but I think you can’t help and watch what today’s graphs are like, or how you did since you began on 750words.

It has been two weeks since I began on 750words and I have not broken the streak yet. For now, I use it as a diary only, but I guess time will come when I have nothing more interesting to write. So how not to break the streak? Some know I would like to be able to write, and I am hoping for the exercise of writing everyday to make it easier. Only imagination is the weak point right now, but this could become a challenge for short stories, and may lead to even better after. Let us wait and see.

Just a final word: if you are looking for an online diary but do not like the constraint of having to write everyday, 750words’ author publicizes about neat-looking Penzu, which has the only brick I lack for total mobility with 750words: mobile apps.

How to implode iterables in Java

The PHP implode function can be quite useful, but no equivalent exist in Java that I know of. Luckily, you can quite easily implement your own.

Implode arrays

This is actually pretty straightforward. Here is what you need to do:

Yeah, I declared this array so that it can contain just any kind of objects. No problem when passing StringBuilder.append(Object), since it will use String.valueOf(Object), returning either the null string if object is null, or the .toString() of the instance.

But arrays in PHP behave more like lists, so why not extend this method to Java Lists?

Implode lists

Using an Iterator is great. It avoids using the .size() to count the elements of the list, and does not require an explicit loop with the .get(int).

Plus, since Iterator applies to all collections, you can extend the method…

Implode collections

Alright, now it works for lists and sets. Just remember the order of elements is not garanteed if you use this method with a set.

[2013/01/16: made methods null safe]

The Optimus keyboard might hit the market

I guess you remember about the Optimus keyboard, right? Each key is a screen, thus allowing to change the keys according to the application you use. Well, the people behind it were at CES 2013.

Optimus stand @ CES 2013

And what is the announcement, then? That they succeeded in making it cheaper, with the Optimus Popularis. The solution? Instead of making each key a screen, make each key transparent, and put a unique screen under the whole keyboard.

Now, how much cheaper does that make it? The announced price tag would be $700… It might hit the market this summer, we’ll see then if the success is here

For full advice, see news on the net. They are quite numerous about CES, and you might find some from people who actually tried the keyboard…

(Source: Clubic [FR])

KP Dock Icons released

I previously posted about a KP Dock Icons project. For those who have not seen the post from the newly created KeyboardPlaying Twitter user, I published the first batch of icons.

They are now 47 icons available on PNG Factory.

I am not quite done yet, but do not know when I will be able to go on. The roadmap and requests are still open on Trello.

I will initiate a project page as soon as I have some time left… Meanwhile, do not hesitate to comment here!