Friday, March 02, 2018

First Class May Not Like Me

Spoiler alert: white people problems!

I sometimes fly business class. This is a luxury, sometimes a decadence, but it's one of the few I allow myself and I shop around for reasonable prices. Over the last few years British Airways has become my go-to airline for business flights between Europe and San Francisco, a trip I make at least twice a year. And I hate flying, and it softens the blow. Woe is me, etc.

For the current trip there was a relatively good price on First Class tickets. "Whoa," thought I, "First must certainly be better than Business, and I've never tried it, so..." I was, frankly, more interested in checking out the First Lounge on my six-hour layover. I wondered what it might have in store.

TL;DR: I still wonder.

Flurries in Albion

I packed, I futzed around, and I slept my customary three hours before an early morning flight. I got a cab, I went out to Budapest's Ferihegy Airport, which the right-wing nationalists have renamed after an epically philandering musician with excellent hair. Everyone still calls it Ferihegy.

And upon checking in -- for flights to the US one does not check in online -- I was informed that the flight was three hours late. "Mechanical?" (As if I had the conviction to not fly when there are mechanical failures.) No, the plane arrived late the night before, and the law mandates a reasonable rest period for the crew. Which I wholly support, though it seems within the realm of the technically possible for a Flag Carrier to send a text message when they know twelve hours in advance that the flight will be three hours late. I talked with other passengers: nobody was informed. Not even an e-mail.

I understood what was really up when we got to London. But first the waiting, which I hope is less boring in the telling.

Budapest Ferihegy has two "business" lounges: one in the Schengen area, i.e. flights to the rest of Europe, and one past the passport control for those bound for non-European destinations such as Delaware, Bamoko, or London. The former lounge should be avoided at all costs unless you absolutely, positively can not pay for a drink, even on credit, at one of the numerous kiosks. The latter is acceptable as long as you're not hungry and you have a good understanding how the global services octopoid Celebi manages expectations.

Having bought my Economist (cash) and my first drink and sandwich (apology voucher) in the normal Schengen area, I got my passport stamped with a B for Brexit and headed over to said Celebi lounge. Some bubbly, cookies with jam, a nap, and the useful knowledge that the other passengers had not been informed of the nature of the delay. I felt a miniscule sense of superiority, surrounded by my economic betters, in knowing exactly why we were all swilling the "Hex" bubbly and dozing our best two-minute dozes. After all, had my ex-in- laws not believed I was a spy?

We boarded exactly three hours behind schedule and proceeded to wait another twenty minutes for a runway slot. Celebi, expectations, see also. The flight itself was uneventful and the crew quite pleasant, which was unsurprising considering the rest they'd gotten. When we got to London Heathrow we had to wait another 15 minutes or so before landing. At this point the people with tight but feasible connections started getting nervous.

Landing was uneventful, and there was the lightest dusting of snow on the edges of the runway, as if the Fairy of Winter had stopped by for a coffee but only had time for a ristretto and a kiss on the cheek before heading off to Finland.

This, it turns out, counts as a severe weather event in London. Apparently Old Blighty can absorb an unlimited quantity of rain, but a few millimeters of snow grind it to a screeching, if mostly polite, halt.

The aircraft reached its standing position. The jet bridge failed to budge. Within a few minutes our Captain engaged the microphone to inform us that the jet bridge was broken and we would use the stairs, as soon as a set of stairs could be obtained, which surely would be within minutes.

Minutes passed.

After another ten or so we were given to know that we had a staircase and needed only to attach it to the aircraft and we could be on our way. Those who had not already rebooked their onward flights, and those who had rebooked to short connections, breathed a sigh of optmistic relief.

For another ten minutes passed, during which our fearless Captain went so far as to open his window (who knew this was possible?) and shout at the ground crew. We were again informed: apparently another plane needed the stairs first. Heathrow is a busy place. We're next in line.

Some minutes after that the story shifted slightly: we need two staircases, presumably one fore and one aft; the second has to be brought from Terminal 5. Having made this connection a half dozen times before, I knew that the mandatory bus ride to Terminal 5 takes about ten minutes without a rolling staircase in tow. I started to worry for my own connection.

Another ten minutes and we had our stairs: one case of them, attached at the aft. Everyone out into the bus!

The bus was completely normal, and when the ground crew said it was full and could only take two more people -- of whom I was one -- I felt a guilty gratitude. When I boarded the bus I saw it could easily take another 10-15 people, and in the end that's what it did. We all fit, and entered the intentional labyrinth of Heathrow, having sacrificed a four and a half hours to the incomprehensible phenomenon of snow -- Snow! -- in Albion.

Holy Grail Lounge

Having made it into the maze, a maze I fortunately knew from previous trips, it was really down to time. One needs an hour, minimum, to get from arrival at Terminal 3 to the departure gate at Terminal 5. I had 45 minutes and a burning desire to see the First Lounge.

At this point I realized that seeing the First Lounge would require that I encounter almost no resistance at the various waypoints: the many walkways and stairs and escalators, the trans-terminal bus, the security check, and the mini-rail within Terminal 5. In the best case, with a little hustle, I figured I might have 15 minutes clear in the lounge, enough to look around and use the private washroom and throw back a Gordon's & Fever Tree. My plan was to make it with at least 10 minutes clear and overstay the printed boarding time by another 10. Hey, First, they'll wait for a bit.

Things worked out differently, but in the end it wasn't so bad.

First, I made all the connections with minimum friction. There was a long line for the Terminal 5 bus, and the two busses available filled fast, but despite a little chaos I was on the second. (I'm sure there were those behind me who missed connections, thank you LHR.)

Second, having been very diligent, I knew from the info-screens and also from Big Brother Google that the flight was on time and I should go to the general vicinity of the C Gates. This I did.

Having arrived with about 10 minutes clear, I started looking for the lounges. No luck. A helpful gentleman selling apothecary goods told me there were none, I'd have to go to the B Gates. Whence I had come, more or less, on the little tram.

I tried that but failed to find the way, owing to difficult signage combined with fatigue. I ended up, frustrated, back at the C Gates, looking around in vain. Finally I noticed the flight was delayed by 30 minutes, a new development since I'd gone looking for the tram. Sir Apothecary told me in detail what I'd done wrong, and off I went on foot (no tram from C to B!) to seek the coveted First Lounge. Now I had, I thought, at least 20 minutes clear.

In B-Land I found the lounge... but only the Business Lounge, which I knew well and do still like. I asked for help. I learned:

  1. There is only one First Lounge, seek it not amongst the B's and C's!
  2. BA requires your presence 20 minutes before takeoff, not 60.

The lovely receptionist told me to chill out and come back in 30 minutes to check the flight status.

In 30 minutes in the Business Lounge you have exactly one G&T (Latin Strength) and a relaxed visit to the Washroom, hallowed be thy name.

After which, the lovely receptionist suggested I'd just hit boarding if I left right now. First Lounge remained a Grail.

Leave I did, and I caught the boarding wave just in time to be "randomly" selected for additional security screening. Among 10 "randomly" selected passengers there was exactly one woman, who happens to be sitting next to me in First as I write this. We computer folk would strongly question the cryptographic strength of that rand function.

3... 2... 3... 2...

Now, a little tired, having been travelling since 4am Albion Time, at about 15:00 I boarded. Immediately I was taken by the arm, addressed by name, and shown to a window seat. I had reserved an ailse seat, as the windows were (and generally are) all reserved before they start dropping the prices for ruffians like me.

I mentioned the ailse reservation to my concierge, and he said British Airways apologized for the trouble today. Things started looking up. I settled in. I instagrammed.

And then along came the rightful occupant of the seat, an older and presumably richer California ex-hippy type, who was very kind but hey, it was her seat and First on British is always full. She probably owns Burning Man and 0.2% of Google, and yet she was totally cool about it.

I was unceremoniously relocated back to my viewless accommodation. Though viewless is not quite the word: there is the beautiful securitized neighbor typing away on her beat-up Intel Inside PC; there is the Running Man on the Exit sign (running where?); there is a TV with a hundred channels, and mint tea, and wine until you give up or pass out.

Meanwhile... the snow. Or let's just say the cold.

Departure was 15:30.

Security checks (blame the Americans) added at least 20.

Then various parts needed to be de-iced.

Through all of this I was given top-shelf bubbly, and eventually I dozed off. I awoke at 17:00, yes 5pm, to our accelleration. For a brief delirious moment I was sure we'd not reach escape velocity, but we took off without incident.

Two hours late. Or, if you like, seven hours late.

Since then it's been a pleasant ride. I learned that Plane Pyjamas are a Thing. I learned that in First all the staff remembers your name, just like in the fanciest business hotels. (How do they do that? Could it be monetized?)

And of course I was reminded why I fly British Business even when the lightest snow flurry puts their Queen and her Corgis in a twist. The seats are nice, the flight crew is for the most part awesome, and they are bottomlessly indulgent to those of us who wish to keep the terror of air travel at bay through the magic of a strong and steady buzz.

But still, dude, you couldn't manage an SMS at midnight?

As we cruise over Calgary I look back on the experience. There's no telling which of the early screwups were British Airways' fault, versus Heathrow's, versus London's, versus the Act of God that resulted in a little ice and that light dusting of snow. On the plane, the divider between my sleep pod and my neighbor's didn't work. The in-flight entertainment system didn't work for half the First Class passengers (a faulty cable perhaps). The noise-canceling headphones only canceled noise when plugged into the entertainment system and not when plugged into my phone. They ran out of water bottles, though not potable water, so that may actually be a positive thing.

Is First worth any more money than Business on BA? Without the First Lounge experience I can't really say. I only paid 100 EUR more in the end thanks to BA's weird habit of charging for seat reservations in Business. For that small difference I'd say the larger sleep-pod is probably worth it, but I get the impression First is more malfunction-prone than Business. Or maybe the world was just having one of those days.

Note to Future Self in any case: download some classic binge-watchables on iTunes next time I fly British, and bring my own noise-canceling headphones.

Monday, April 03, 2017

A Thought on Airlines

I recently bought a ticket on AirBerlin, which is my preferred airline for traveling between my home in Berlin and my favorite destination of Budapest.

It was a horrible experience.  Their preferred payment system, the security of which seems questionable, failed to process my payment (from my Berlin bank no less); it was impossible to return to a booking in progress if the browser window closed (hello, cookies?); and in the middle of retrying this, literally within minutes, they raised the price of one of the flights.  And that's just the technical part.  It was impossible to figure out which fare class would actually be cheapest once luggage and seats were added; it wasn't obvious who operates the flight (Alitalia, says SeatGuru, which would be a first for this route); and in general things were designed to keep the customer in the dark.

Unfortunately, there's nothing particularly unique about that.  Airlines are in general hostile to transparency, and constantly try to squeeze you for an extra five Euros here, an extra $19.95 there.  (Here at least the cheapos like WizzAir are honest: pay for the good seat or your trip will suck.  Pay for your booze, but they'll be happy to sell it to you.  Ryan and Easy, however, remain below contempt.)

The strange thing in all this is that I expect my flight to be pleasant, professionally flown, and more or less on time.

And it's like that almost every time.

We buy a ticket from a company that obviously hates its customers, and we then put our lives in that same company's hands.  And somehow this works.

Wednesday, November 09, 2016

Ten Quiet Predictions for a Trump Presidency

It’s done: in an upset that should have surprised no one, Donald Trump has been elected President of the United States. As to how this happened and why, the pundit classes have been so busy dissecting the cause and the method that they didn’t look up to see the oncoming train. The train is upon us now, lumbering forward in its orange sputtering of nonsequitur trutherisms.

For the moment, I’m not very interested in how we got here; as an American from the sticks, I accept that he speaks for the majority. Most of that majority really does stand behind him, and a few (like Spineless Paul Ryan, and most of the Religious Right) are mere opportunists who have nonetheless ceded their voice to the Orange King. He is their voice. For real this time.

(I read somewhere that the Republican Party had created the monster that is Trump’s base; and Trump had stolen their monster for his own uses. This is probably true, but it’s also true that this kind of talk inspired the monster to don a Deplorable Me t-shirt and get out to vote.)

It’s early. Hillary has conceded, so there is no second-guessing Trump’s victory. (And in at least one point I’m relieved: I will not have to see a second attempt at dynastic leadership just yet, at least not until Ivanka runs).

The Republican Party – the Trump Party, as it frankly ought to be renamed, its logo painted in gold, with a 5% members’ discount at select properties – will also control both houses of Congress, and most likely the Supreme Court with the ideologue of their choice by mid-January.

That means they will have to govern, or at least try. This will not be easy, especially after so long in cushy obstructionism. The likes of Ryan are a little too used to writing budgets without using math, and taking responsibility will be hard. But I do believe they will take action, because without action the Donald will bore and worry and fear for his popularity; and if there’s one thing we now know for certain, it’s that the Orange King can eat Ryan and his covey of twerps for lunch.

And now to my predictions. Like most predictions, they will most likely not age well. But here we go all the same.

1. Trump will return to a limited pragmatism.

He will certainly remain an oafish, bigoted conspiracy theorist, but as far as he can he will avoid outright confrontation with half the country.

Instead he will try for things he can win without protest, or win despite protest, or look good losing.

Ivanka will be chief of staff and unofficial COO.

2. Environmental protections will be gutted to the point of meaninglessness.

Fracking ho! This will be the price of the Koch brothers’ support, but Trump will pay it gladly as time and again his base has come out in self-defeating opposition to government regulation of industry.

In the short term, the poor will pay the price as they usually do. Long-term this means the worst for climate change, but the worst was coming anyway.

3. Black Lives will Matter Less.

This one is obvious: after all we’re talking about the Birther President.

The government will no longer support any efforts towards racial equality. The Justice Department will no longer pursue vote-suppression investigations. There will be laws against recording police actions on video.

These things will be to the great satisfaction of the white supremacists, but Trump will appoint a few token People of Color in his adminstration and swear he’s not a racist.

4. Assad will win in Syria, with Russia’s help, and with some partition.

Trump will wash his hands of Syria, but look like a statesman of sorts for brokering a deal between Turkey and Russia that will give most of Syria back to Assad while carving out a small zone for the anti-Assad forces loyal to Turkey. The Kurds may or may not be sold out – largely depending on whether Trump learns anything about them before making the deal.

The deal may well be proposed by Russia, but giving Trump credit will be part of the deal.

5. Trump will play chicken with Mexico and Canada on trade, and win.

I think NAFTA will be an easy target for Trump. It’s insanely unpopular with his base, who blame it above all else for the gutting of American factory jobs. And while it certainly benefits the US as such, that benefit accrues mostly to big business.

His argument will be that NAFTA was a lousy deal for the USA, and that Mexico and to a lesser extent Canada are the big winners. He will demand concessions, maybe even a whole new treaty. And he will get his concessions, particularly from Mexico.

The big question is whether they will be symbolic concessions or substantive ones. If they are substantive, we might see it helping agriculture in the US, but I have a hard time picturing it doing anything for manufacturing.

6. Trump will play chicken with China on trade, and lose.

Bolstered by the popularity of his NAFTA re-do, Trump will try to “get a better deal” with China. China will play him like a Guzheng and extract serious political concessions in return for meaningless trade platitudes, then use the cudgel of their dollar reserves to prevent anything substantive changing to their disadvantage.

Trump will find a way to spin this in his favor, but the financial press will give him a very hard time for it.

7. US isolationism will be taken advantage of across the globe.

China, Russia, Iran, Pakistan, Saudi Arabia, North Korea, and Poland – just to name a few – will all do things at home and with (or to) their close neighbors that would previously have gotten them in deep trouble with America. Trump will let it slide.

His political intuition on this point will turn out to be right: most Americans will prefer to not care who’s killing whom on the other side of the world.

8. Obamacare will be abolished, but the pre-existing-condition protection will remain.

This point, and the next, are where I see a glimmer of optimisim around the Trump presidency. I think he has enough populist sense to not take away something as important as the pre-existing condition protection. (For any international readers: pre-Obamacare you could not buy individual insurance in America that covered any illness or injury you already had.)

I also think Ivanka will push him to do this, and unlike any pre-Trumpian Republican he will not have any problem sticking it to the (obscenely profitable) insurance industry, nor will Spineless Paul Ryan be able to stand up to him. On anything, really.

9. The tax code will be reformed.

Over the objections of the business elite, and to the consternation of the entrenched Republicans in Congress, Trump will force through a simplified tax code that even he can explain to the people. It won’t be great for anyone but the rich but it will “seem fair” at first glance.

As with the “something better” for Obamacare he will face stiff opposition from his own ranks, but he will bulldoze them, and also get some unexpected support from left-leaning Democrats if the tax is not completely regressive.

This, together with the NAFTA deal, will be his signature achievement.

If he gets the Trump Party in line early, don’t rule out a flat tax on income, possibly with a lower rate for investment income. This kind of regressive tax plays well with the middle class because it “seems fair,” and it also buys a lot of loyalty from the upper-middle class that might not be with you ideologically. It worked in Hungary and I’m sure Orbán will suggest it to Trump at some point.

10. The poor will get poorer, etc; victory will be declared.

Chaos, unpredicatbility, and amateurish mistakes will exacerbate the problems you’d already expect from the normal, growth-killing Republican policies.

The economy won’t tank unless there is a major unforeseen catastrophe, but it will be sluggish at best. That part of Trump’s base that could be called the economic losers of globalization will be even worse off than they are now, and except for the very rich nobody will be much better off.

But Trump will have a few big wins to offset his big losses, and the Democratic Party will be split as usual between the business-establishment wing and the Sanders leftists.

2020 is his to win, looking at it from here. But to do it he has to be a little bit lucky with global events – no new wars, no economic meltdown – and he has to avoid the temptation to stock his administration with characters from the clown car of his recent campaign.

While I’m sure there will be pressure to appoint, say, Rudy Giuliani as Attorney General, I hope that Trump the Opportunist at least recognizes that he’s now the Winner and can choose from a more competent class of sycophants.

Thursday, April 14, 2016

Interesting JSON Benchmarks Go.

These days lots of people are buiding microservices, and microservices usually involve HTTP API’s, which in turn usually exchange data as JSON.

Not long ago somebody pointed out that a lot of effort goes into generating and parsing that JSON. It would be unwise to simply ignore this part of your system’s design.

Since it’s very easy to benchmark things in Go, I decided to do a quick comparison of JSON encoding strategies.

Go JSON Encoding

The normal way to generate JSON in Go is to use the encoding/json package, and feed your struct into the MarshalJSON function. This function will take anything and try to convert it to JSON. If your struct, or anything in it, has its own MarshalJSON function then that is used, otherwise it’s examined using reflection.

Reflection is (supposed to be) expensive, so I wanted to see how much I might save by making my own JSON encoder for a struct. The main point being that I already know what the struct is made of, so I can save the encoder the trouble of examining it.

Benchmarked Variations

I started with several, er, structurally identical structs:

  1. A naïve one, with no MarshalJSON function of its own.
  2. A hinted one, with field names provided.
  3. A smart one, with its own proper MarshalJSON function.
  4. A fake one, which returns previously set data from its MarshalJSON.

The point of the fake one, of course, is to isolate the overhead of the actual JSON encoding.

All of these, when set up with a bit of standard fake data, generate the following JSON:

   "Id" : 123,
   "Stuff" : [
   "Desc" : "Something with \"quotes\" to untangle.",
   "Time" : "1970-01-01T01:16:40+01:00",
   "Insiders" : {
      "One" : {
         "Id" : 321,
         "Name" : "Eenie"
      "Two" : {
         "Id" : 421,
         "Name" : "Meenie"

Surprising Results

Here are the benchmark results for this little experiment, as run on a MacBook Pro (Mid 2014) with 2.8 GHz i5, 8 GB RAM, under Go 1.6.1.

BenchmarkNaïveJsonMarshal-4               300000          4299 ns/op
BenchmarkHintedJsonMarshal-4              300000          4293 ns/op
BenchmarkSmartJsonMarshal-4               200000          6490 ns/op
BenchmarkSmartJsonMarshalDirect-4         300000          4149 ns/op
BenchmarkFakeSmartJsonMarshal-4          1000000          2299 ns/op
BenchmarkFakeSmartJsonMarshalDirect-4   10000000           115 ns/op

In the “Direct” benchmarks, the struct’s own MarshalJSON function is called without going through encoding/json, i.e. without any sanity-checking.

I expected to see a lot of overhead from the reflection, i.e. the unknown struct being examined. Instead I found that using your own MarshalJSON function is actually slower because json.MarshalJSON (sensibly enough) validates the JSON output for you, lest it accidentally return invalid JSON itself.

Also, the hinting doesn’t make much of a difference, but it can make your JSON output prettier and more predictable: one usually uses it to have lowercase and/or underscore_separated key names in JSON objects, and to omit null objects in order to compact the JSON.

Using the numbers above we can very crudely estimate:

  • Custom encoding with validity checks is about 50% slower.
  • Custom encoding without validity checks is about 3.5% faster.
  • Best-case custom encoding with validity checks is about 50% faster.

In order to use the custom encoding without validity checks, you have to do all the encoding in a non-idiomatic way. This makes your codebase more fragile, because a new collaborator can’t just step in and do the obvious thing without undoing your optimizations.

It would be interesting to see how these numbers scaled with more complex structs, in particular deeper nested objects.

Based on these benchmarks, which I admit are oversimplified, I recommend avoiding custom MarshalJSON functions unless you absolutely need them for handling unusual data structures. If you want them for speed, make sure to benchmark your implementation before making a final decision.

Source Code

Wednesday, April 06, 2016

I Am Possibly Legend

Recently I finally – finally! – got around to seeing the 2007 Will Smith SciFi vehicle I Am Legend. Apparently it was a remake, of sorts, of Omega Man, which I saw as a teenager and barely remember.

Spoiler Alert! Just in case you haven’t seen it.

Here’s the really big problem. The Zombies aren’t trying to kill Will Smith because he’s the savior of the human race, they’re trying to kill him because he’s a serial killer, responsible for hundreds of disappearances, tortures, and grissly murders. He’s more than a bad guy, he’s Zombietown’s own Josef Mengele.

He abducts the Zombie King’s girlfriend/daughter/wife/buddy/chess-partner (we aren’t told which), drawing the Zombie King out of his lair at risk of death by exposure. Our Hero then performs sick medical experiments on her, fully expecting her to die of them, in the name of “curing” her. She dies.

Then, the Zombie King – proving, by the way, they’re not zombies at all in the traditional film sense – comes up with an elaborate trap to capture Lt. Col. Mengele. After that fails, the Zombie King directs an army that almost kills Our Hero, but the Lt. Col. is saved by another NonZombie who shows up out of nowhere.

It should be said that Our Hero is at that point basically suicidal, because the Zombie King’s dogs zombified the Hero’s dog in their failed capture attempt, and the dog was Our Hero’s best and only friend, there being no other NonZombies around, and to top it off Hero had to kill Dog to prevent the zombification.

But! But all along our Hero makes audio notes, such as the audience is privy to, in which he says things about the Zombies that are demonstrably not true. Mostly, that they are actually zombies, and not merely Very Different Humans.

The only part of this I don’t understand is whether the filmmakers were trying to gloss over the murderous aspect, or whether they were through it trying to point out the futility of all life, of our folly before the gods. Because – spoiler alert – in the end the Good Dr. Lt. Col. Hero’s life’s work of abduction, torture and murder succeeds, and ends the plague of zombism.

Well, if you believe the narrator it does. Considering how reliable the narration is up to that point, I am much more inclined to believe there was a successful genocide launched against the Zombies. And for that matter I am specifically disinclined to believe they ate all the nice folks in the first place.

Saturday, March 12, 2016

Many Servers in One, with Go.

I’m still fiddling with a web server idea, currently trying my umpteenth version of it in Go. One of the things I want to do is serve a bunch of different things from one binary; they would usually live behind Nginx anyway, and I like the simplicity of serving a bunch of (somewhat) dynamic, small sites from a single program.

So, how does one do it? Why, one does it with Goroutines!

I had expected that to be the answer, but even then I was surprised at the simplicity of this solution. You don’t even need channels, since http.ListenAndServe is a blocking call if it’s not in a Goroutine.

Here’s the code, which should be self-explanatory.

Friday, January 08, 2016

How to Test Logging in Go

Google’s Go language has a very rich and at times utterly infuriating standard library. One of the things it includes is Logging. Since there are six million ways to log (choose one!) we shouldn’t get too worked up about the ways in which the log package doesn’t log the right way for, well probably for anyone outside of Google. That’s not the point.

The point, and it’s a happy one today, is that a mere ounce of prevention will get you easily testable logging with Go’s standard log package. Caveat lector: I have no idea whether this works with Go prior to version 1.5.

Your Ounce of Prevention

Especially when hacking together quick utilities or packages, it’s always tempting to do this:

package logdemo

import "log"

func Log() {
    log.Println("woo hoo")

However, that will be very hard to test. Take the time to set up a logger, and expose it. Ideally you can do this with a struct:

package logdemo

import "log"

type LoggingThing struct {
    Logger *log.Logger
func New(name string) *LoggingThing {
    prefix := "thing-" + name + " "
    flags := log.Ldate | log.Ltime | log.Lmicroseconds
    return &LoggingThing{Logger: log.New(os.Stdout, prefix, flags)}
func (lt *LoggingThing) Log(msg string) {

Now you have something that can be messed with in your unit tests, and is also much easier to debug should the need arise.

But if you really can’t deal in structs, at least set up a package variable for the logger. This too can be manipulated.

package logdemo

import "log"

var logger_prefix = "global "
var logger_flags = log.Ldate | log.Ltime | log.Lmicroseconds
var Logger = log.New(os.Stdout, logger_prefix, logger_flags)

func Log(msg string) {

Your Pound Sterling of Cure

Now that we have an exposes Logger (two, really), we only need to know one thing: that a Logger can have its Output set after the fact, and that the Output needs only to be an io.Writer, which as you may recall simply requires that it implement this:

type Writer interface {
        Write(p []byte) (n int, err error)

In our test package, we can set up a simple struct to catch logs instead of writing them:

type LogCatcher struct {
    Logs []string
    Last string

func (lc *LogCatcher) Write(p []byte) (n int, err error) {
    s := string(p)
    lc.Logs = append(lc.Logs, s)
    lc.Last = s
    return len(p), nil

That will give us raw logs, but they may include prefixes and timestamps. Prefixes, if they are used at all, are quite useful; but timestamps are very hard to test for, since you then have to match log entries with regular expressions instead of simple strings.

Fortunately, that too can be overridden. Our overrides then look something like this:

thing := logdemo.New("one")

catcher := &LogCatcher{}

// If you want to test the prefix and output format you can of course
// do that separately.  For testing the written logs it's asking a
// lot, but we can override!

// Here we log, and catch it!
assert.Equal("here\n", catcher.Last, "caught first log")

Note that I am using the excellent assert package in order to make unit testing sane. (Again with the rich but infuriating standard library…)

If you’ve read this far and you aren’t totally lost then you can imagine how this continues. But you don’t have to imagine, because the code is there for you on GitHub under biztos/go-demos, specifically as
logdemo.go and logdemo_test.go.

Congratulations, now your logging will not prevent you from achieving the coveted (and utterly spurious) 100% code-coverage metric!