Simple (syslog) logging with Ruby

Here’s a tip that took me a while to figure out, but is really easy in retrospect. So easy in fact that I just assumed I was being dense for not knowing it, and everyone else just “knew” this.

However, after a smart friend tweeted about needing a simple logging method, and not getting any replies that nailed it, I thought I’d post this up.

First, require syslog

require 'syslog'

Then, make a call to Syslog.open, with a block and send a message

Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.warning "Danger!" }

In this case I went with a warning level message. Syslog also supports:

  • crit CRITICAL system level events (like “System is going down…”)
  • emerg emergency
  • alert
  • err
  • warning
  • notice
  • info
  • debug

These map directly to the syslog levels you can read about in “man syslog”

It seems that debug and info messages don’t make it to the os x console app, so I use warning.

Here’s the code all together:

And that’s IT! Pretty simple way to get logging into any of your scripts. Don’t want it in main syslog? Just edit your syslog configuration and divert all messages from your app into a dedicated log file (without needing to touch your script).

Teaching the Rails.

On August 18th, I’m going to be teaching an intermediate/advanced 4 day rails course with David A. Black in Edison, NJ. If you know enough Rails to be dangerous, sign up and we’ll make you deadly.

Signup

Visit RubyPal for more information and to sign up today!

Teaching

In a previous life, I was very heavily into Martial Arts – TaeKwonDo mostly. From the ages of 12-18, I taught almost every day. Young, old, strong, weak, I trained them all. While there is a lot about the martial arts industry I don’t miss – teaching isn’t one of them.

Now I’m being given an oppertunity to get back to teaching. Not only that, it’s teaching something I’m passionate about, and am good at!

YOU!

If you sign up (at RubyPal), you WILL learn, and you WILL have a good time.

Use rails at work? Get your boss to send you. Work for yourself? Write it off. Already an expert in Rails? Come heckle me ;)

Thanks

Without this great community, I wouldn’t even be in this position, hopefully, one of my students will be the next “Rails Rockstar” and I can give back to all the awesome people I’ve met over the past 3 years.