3

Heroku is a hosting platform whose logs can be accessed via the heroku logs command line:

$ heroku logs
2014-07-17T06:24:02.216403+00:00 app[web.1]: Started
2014-07-17T06:24:02.216410+00:00 app[web.1]: Stopped
2014-07-17T06:24:02.216408+00:00 app[web.1]: Paused

As you can notice above, the chronological order is not respected.
Heroku is happy with that and gives no particular advice.

Is there any tool or bash script that I can use to have Heroku logs sorted?
The logs are pretty small, 1500 lines max.

Free, Open source, runs in Linux bash.

3 Answers 3

3

You could try this:

heroku logs > heroku.txt
sort -n -t\  -k1 heroku.txt

The -n is sorting numbers in order, -t\ bit is using a space as a separator, -k1 is using column one (the timestamp).

Without putting the logs in a separate file I assume you could use:

heroku logs | sort -n -t\  -k1

Similarly, to sort in reverse order (-nr):

heroku logs | sort -nr -t\  -k1
4
  • That works! I actually need chronological order, so could you please change to heroku logs | sort -n -t\ -k1? Thanks!
    – Nicolas Raoul
    Jul 17, 2014 at 8:41
  • No problem :) Changed that for you. Jul 17, 2014 at 8:46
  • 2
    heroku logs | sort would work just as well. The date format has the property that lexicographic order matches chronological order, thanks to the fields being fixed-width and in order of decreasing significance. Aug 4, 2014 at 14:41
  • You're quite right - never noticed that! Learn something new every day.. Aug 4, 2014 at 15:27
3

I wrote a little script to make live logs (heroku logs -t) sorted.

http://pastebin.com/8Lby3bMg

You can run with:

heroku logs -t | python delayed_sort.py

If you want to keep the coloring:

script -q -c "heroku logs -t" -a /dev/null | python delayed_sort.py

The script simply holds a buffer of the last 100 log lines. When the buffer gets full the line with the oldest timestamp is popped out (using heap-queue for efficiency...). The buffer is also flushed out when there are no new log lines for 1 second.

There is still a tiny chance that the lines will come out unsorted: a line with a timestamp that is older than the 100 preceding lines, or a line with an old timestamp after a 1 second communication pause.

3
  • Great! Maybe the script could call heroku logs by itself, so that I just have to invoke the script only, without using pipes?
    – Nicolas Raoul
    Jul 23, 2014 at 3:04
  • The script would work for any pipe of logs that contain timestamps. It could be a nice heroku-cli-plugin ( devcenter.heroku.com/articles/using-cli-plugins ), but I think you would have to convert the code to Ruby or something. I simply created a bash script in my PATH called heroku-logs, with the script -q ... line.
    – Oren
    Jul 23, 2014 at 8:04
  • This script doesn't work on Mac OSX due to select.poll() not being supported. See: stackoverflow.com/questions/19740471/…
    – tadasajon
    Aug 5, 2014 at 19:23
0

As suggested by Gilles, this is the easiest and works flawlessly:

heroku logs | sort

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.