I would strongly suggest using syslog for logging errors as its more standard way of logging.
you can look at the example implemenation here (bio3d.colorado.edu/tor/sadocs/misc/syslog.html)
Also unix network programming book by richard stevens also contains good explanation of using syslog