How to manage logs

Q I have a few scripts that I run, and I want to generate debug logs that I can occasionally turn on and off. Do you have any suggestions for me?

A Have you considered using Syslog to generate and manage your logs for you? PHP, C and Perl all contain a library for sending Syslog messages to the configured log host. From there, you can configure Syslog to log specific messages to a separate file and update Logrotate to rotate them for you. Firstly, add a new selector (left-hand side) and destination log file (right -hand side) in /etc/syslog.conf and restart Syslog. The selector is made up of two items: the facility and severity. The facility can only be one of local1-local7 not already defined.In this case, I have chosen local3 and want to log all messages.

# newprog
local3.* /var/log/newprog.log
root$ service syslog restart

If I wanted to only log error (err) messages and worse, syslog.conf would instead contain

# newprog
local3.err /var/log/newprog.log

Now confirm that Syslog is working by running the following and checking the output of the log file:

$ logger -p local4.err test message
$ tail -f /var/log/newprog.log
Jan 31 1  1:46:40 host user: test message

Once you have confirmed Syslog is working, you can now configure Logrotate to log rotate your new log file using the previously defined Logrotate rules by updating the configuration file to include the new log file. Under Red Hat Enterprise Linux, you will need to update /etc/logrotate.d/syslog, adding /var/log/newprog.log' to the first line of the config file. Now all you need to do is call Syslog within your code, using the selector that you previously added to syslog.conf (remember we used local3) providing a severity level. At a later stage, you can then turn off partial logging by adding a higher severity to the Syslog configuration as described above. More information on Syslog and adding Syslog calls to PHP, C, Perl and Bash can be found in the following documentation. Man pages: Sys::Syslog (3pm) Unix::Syslog (3pm) logger (1) syslog (2) I'd also recommend you look at http://php.net/syslog.

Back to the list