Output a timestamp with each line in a Maven log

Maven is a powerful build tool for Java and it tends to spit out a large amount of logs, requiring you to scroll back in your output window or console to look at what’s happening. If you’re running it regularly, for example whilst building tests then it’s easy to scroll back slightly too far and look at the results from a previous run by accident.

An easy way to avoid this is to configure Maven to output a timestamp on each log line. Just open up your MAVEN_HOME/conf/logging/simplelogger.properties and change the dateTimeFormat like this:

org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss.S

Not only will this make it easier to spot if you’re looking at the correct log lines but you’ll also be better able to see how long each stage is taking (although for real measurements here you’ll want a profiler).

Debugging

I do not like debugging. I prefer good logging.

The log4j manual quotes Brian W. Kernighan and Rob Pike from their “truly excellent book” The Practice of Programming:

As personal choice, we tend not to use debuggers beyond getting a stack trace or the value of a variable or two. One reason is that it is easy to get lost in details of complicated data structures and control flow; we find stepping through a program less productive than thinking harder and adding output statements and self-checking code at critical places.

Clicking over statements takes longer than scanning the output of judiciously-placed displays. It takes less time to decide where to put print statements than to single-step to the critical section of code, even assuming we know where that is. More important, debugging statements stay with the program; debugging sessions are transient.

There are times when a debugger can be really helpful, but in my experience they are normally used as a fallback for a poorly documented system with an unclear flow of logic, or overly large methods with poor test coverage.

Internet rabbit rebuild – step 1

Back in Christmas 2006, I was lucky enough to get everything I asked for, and one of those things was one of the first commercially available Internet of Things devices – a Nabaztag.

A picture of my Nabaztag with other Christmas presents

This was a beautifully moudled piece of plastic designed to look like a rabbit. It connected to your wifi and triggers could control its LEDs, its individually rotating ears or play sound through the speaker. I had great fun with it but, in what would be a salutary lesson, the fact that it was proprietary hardware, talking over a proprietary protocol to a proprietary server was soon a problem when not only was my model made obsolete by newer models but then the company going bust. Suddenly, I had a great-looking paperweight.

Some keen nerds reverse-engineered the protocol and wrote their own servers (like NabAlive, NabaztagLives and OpenJabNab and there are a whole host of libraries listed here), but they’re not all straightforward to set up and there isn’t as much support for the first version of the Nabaztag.

All this means that mine has been in the loft for the best part of a decade, but commodity hardware is now affordable enough, and low-effort enough (no soldering for me!) that I thought I’d try and bring my internet bunny back to life, in particular after being inspired by Roy Tanck’s attempt at doing the same thing by replacing the insides with a Raspberry Pi.

Step 1 was to take it apart. There are some triangular screws on the bottom which came out pretty easily using one end of some needle-nosed pliers and then the rest is standards phillips-head. It’s impressive to see how far electronics manufactoring has come in the last decade – the wireless in the original rabbit was provided by a full-sized PCMCIA card!

A picture of the front of the Nabaztag's main PCB


Once the case was off, I removed the electronics and motors from a central plastic frame, and my next step is going to be to prototype replacing them using a Raspberry Pi Zero W with a Blinkt attachment.

Configuring the VS Code integrated terminal to use Bash instead of cmd.exe on Windows

Visual Studio has an integrated terminal and by default on Windows this loads cmd.exe – the standard, and fairly rubbish Windows command prompt.

I normally use cmder as my default terminal on Windows –
this is basically the excellent conemu tabbed console emulator, with a few customisations.

If you try and set VS Code’s integrated terminal to the cmder executable, it will open a new terminal window, rather than be integrated, so you need to point it at the bash.exe executable instead.

After installing cmder to to C:\cmder the bash binary is in C:\cmder\vendor\git-for-windows\bin and the setting you need to add to VS Code is this:

"terminal.integrated.shell.windows": "C:\\cmder\\vendor\\git-for-windows\\bin\\bash.exe"

This will now give you bash when you open the integrated terminal! On one of my computers the default prompt came out ugly, so I changed it like this:

Open the integrated terminal and type

vi ~/.bashrc

Then enter insert mode by pressing ‘i’ and pasting in this:

export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\e[0m\]\n\$ "

Save the file by pressing Escape, then typing ‘wq’ and pressing Enter.

Exit the terminal by typing ‘exit’ (rather than just hiding it by using the VS Code menu or keyboard shortcut.

Re-open it and tada! a much improved integrated terminal!

Requesting a URL with Python

After a year at the bottom of my drawer, I have busted out my Pimoroni Flotilla. It has a Python API and so I figured this was as good a time as any to use Python3 for the first time in many years.

Part of the kit is an LED matrix. I want to use this to display how many people there are in space right now.

There is a great website which will answer this question (howmanypeopleareinspacerightnow.com) which also has a JSON endpoint. It turns out there are lots of ways of getting this information from Python. Here are three I tried.

requests

Requests calls itself “HTTP for Humans”, and is mostly short and concise, but I need to send some extra headers with the request because of some user-agent filtering on the server.


import requests

url = 'http://www.howmanypeopleareinspacerightnow.com/peopleinspace.json'
headers = {'user-agent': 'space-requestor/0.1'}

response = requests.get(url, headers=headers)

print(response.status_code) # 200
print(response.text) # response body as text

inspacenow = response.json() # response body as JSON object

print(inspacenow["people"][0]["name"]) # 'Peggy Whitson'

urllib3

urllib3 describes itself as a ‘powerful, sanity-friendly HTTP client’ and it’s more verbose than I’d like for my simple case, but feels like it might scale into a larger application quite well.


import urllib3
import json

url = 'http://www.howmanypeopleareinspacerightnow.com/peopleinspace.json'

http = urllib3.PoolManager()
response = http.request('GET', url)

print(response.status) # 200
print(response.data) # response body as byte string

inspacenow = json.loads(response.data.decode('utf-8')) # response body as JSON object

print(inspacenow["people"][0]["name"]) # Peggy Whitson

unirest

Unirest is a collection of eight HTTP client libraries for multiple programming languages, but supporting near-identical request idioms, which makes it easy to use if you are using multiple languages yourself.

Sadly, the Python library, which I’ve used before very happily, does not work with Python3.

If it worked, the main call would have looked something like response = unirest.get(url), which is a brevity I deeply appreciate!

Conclusion

Requests seems very popular, and seems like a good solid choice for HTTP requests in Python3 applications. It’s what I’ll be using for my humans in space monitor!

The surprise of Spring Boot

I am a long time Java programmer. It was what I used to write my final year project at university, and it was my first, second and third jobs.

That third job lasted more than a decade and, alongside a huge amount of other things, I got to deliver a number of projects in both PHP and Ruby. PHP was interesting and I’d happily work in it again, but Ruby was fun.

Terse, expressive, powerful and (mostly) lightweight in nature, it feels like a blessing of a language.

The time came to change job again though, and although I looked for some Ruby roles, I’d been a bit too hands-off to be able to be seriously considered and so I got another Java role. But! What a surprise! Whilst I wasn’t looking, Java frameworks learned some lessons from their “convention over configuration” brethren, and those lessons are mostly reflected in Spring Boot by Pivotal Software. Here is a complete Hello World web application in Spring Boot:

@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        "Hello World!"
    }

}

This is borrowed from the Quick Start example, and is in Groovy, so there’s no other requirement for imports, classpaths etc. Just use the Spring Boot command line tool to run it, and you’re done!

The Getting Started guides cover a wealth of activities to do almost anything you can imagine, but the framework is perfect for writing microservice-based applications with very little configuration required beyond adding some annotations to your classes and methods.

It makes writing Java a pleasure again, and an experience far from the heavyweight EJB and Spring2 I was recoiling from a few years ago!

Resolution #1

Weigh myself every day. Nothing about losing weight, just about recording it.

That was one of my New Year’s resolutions. Let’s see how it’s going:

A chart of my weight on each of the days in January 2017
My weight in January 2017

I missed four days out of a possible 31, which means I had an 87% success rate, which I think is pretty good going for a New Year’s resolution 🙂

I am simply recording my weight on a normal set of bathroom scales at roughly the same time each day and immediately adding the data to the Withings app on my phone, which I already had because I have a Withings Activité. I am a pretty steady 69kg, with small peaks after each weekend.

Whether this brings about any change in diet or exercise is yet to be seen. In the meantime, I’m just going to keep recording the data.

Full fat

8 years ago The Guardian proudly announced that they were providing full-text feeds of, well, everything:

Today guardian.co.uk rolled out a major upgrade to the RSS feeds. Our feeds now contain the full content of each article so that you can take guardian.co.uk with you wherever you prefer to get your news.

Fast-forward back to today and not only do they not provide the full content of articles in their feeds (those clickthroughs and ad impressions being all-important), but not even their developer blog has been spared. This is pretty disappointing.

My first thought was to run it through the nice full-text RSS feed creator at fivefilters.org but it looks like The Guardian have asked them to not allow this for their domain (damn those ads!). Luckily there are still tools out there which will convert their truncated feed into full text for me, so I was saved from having to write the relevant dozen lines of code myself, although maybe that was the point, eh developer blog?

Government Digital Service developer docs

GDS has an absolute boat-load of repositories on GitHub, and no clear way to access or browse them.

One of those repositories, govuk-developers, is an ordered index to those repositories, allowing you to find exactly what you’re looking for. This is a static site generated with Middleman and served from Heroku, except that you need a GDS login to be able to access it (presumably for cost reasons).

In the description for that repo it says This project is being rebuilt and is undergoing rapid change. but there haven’t been any changes for nearly a week, so I’ve forked it.

It’s now running in my Heroku account with no username and password (there’s a prompt, but just hit <ENTER>), so you too can browse the GDS repos and docs in a useful way.

Reading feeds again

When Google Reader went away I raged and sulked and tried half a dozen services touted as replacements, but eventually I installed FeedDemon on the computer I used the most often and read my feeds daily, there.

A few weeks ago I found the Newsfold app feed reader for Android, logged back in to my Feedly account and imported the OPML from FeedDemon.

Since then I have been happily checking my feeds every day on my phone, more or less like I used to with the Google Reader mobile app. I only have a dozen or so going at the moment, but hopefully this will slowly step up and I will actually know what’s going on the world again without having to suffer Twitter.