Live data from Hacker News

How To Write Good Log Messages

trottercashion.com

1–10 of 57 posts

Re: How To Write Good Log Messages

#3
time is expressed in seconds? My computer does about 3 billion things every second. I think milliseconds or preferably microseconds would be much better. Different domains have different needs, though, I'm sure. Also, I'd prefer the log specify the time zone it thinks it's in, rather than just convert to UTC automatically.

pid - yes, knowing the process id is good, but knowing the thread is also useful.

version - outputting this on every single log line is probably overkill. Ditto for host name.

Re: How To Write Good Log Messages

#5
I reviewed my some of my code and figured out that I'm one of this terrible persons, which haven't spent enough time to show an useful output. I should fix this now ;) A useful log output can be a real time saver. I also seen applications which just showed "an error occurred", so I had still no idea what causes this error message.

Re: How To Write Good Log Messages

#6

time is expressed in seconds? My computer does about 3 billion things every second. I think milliseconds or preferably microseconds would be much better. Different domains have different needs, though, I'm sure. Also, I'd prefer the log specify the time zone it thinks it's in, rather than just convert to UTC automatically. pid - yes, knowing the process id is good, but knowing the thread is also useful. version - out…

Thanks for the feedback. I'm with you on the milliseconds and threads bits. As for hostname / version... how are you getting those into your logs? I've found having version in the logs very useful (and wished I had it when it wasn't there).

Re: How To Write Good Log Messages

#7
I personally have found a lot of difficulty with JSON-based logs, because tools like `grep` and `cut` don't suffice; I usually have to write a more complex regex with `sed` in order to extract useful information I want.

Anyone have a good utility for managing JSON in log files?

Re: How To Write Good Log Messages

#8
One of the hardest things in ops is trying to see what the app is doing without benefit of the code to review.

I know what the app does, I know the process flows involved but 'failed to covert X at MyClass:44' doesn't have enough context for me.

Also log levels - most developers over use warning and error levels and under uses debug and trace in my experience. It makes filtering out the noise harder.

Rolling log files - roll on an event (new day, process bounced etc.) not on reaching a 10Mb limit or an equally arbitrary 1k lines limit.

Use a header in the logfile, contain at least the version number of the code running, the user and host it is running on.

Lastly stack traces, if they even have a use case at all in production, should not be in the main logfile.

Re: How To Write Good Log Messages

#9
post #7

I personally have found a lot of difficulty with JSON-based logs, because tools like `grep` and `cut` don't suffice; I usually have to write a more complex regex with `sed` in order to extract useful information I want. Anyone have a good utility for managing JSON in log files?

Moving away from using regex to parse JSON, you can use Jsawk and Underscore-cli to manipulate JSON like Awk or JavaScript.

https://github.com/micha/jsawk/ https://github.com/ddopson/underscore-cli

Re: How To Write Good Log Messages

#10

time is expressed in seconds? My computer does about 3 billion things every second. I think milliseconds or preferably microseconds would be much better. Different domains have different needs, though, I'm sure. Also, I'd prefer the log specify the time zone it thinks it's in, rather than just convert to UTC automatically. pid - yes, knowing the process id is good, but knowing the thread is also useful. version - out…

> Also, I'd prefer the log specify the time zone it thinks it's in, rather than just convert to UTC automatically.

But my computer thinks it's clock is in UTC, and it only converts to a timezone for human readable dates.

Post reply on HN