Better Scripts #3 - use logger
When you write scripts, especially scripts that will be executed unattended by cron or a similar facility, in most cases you want some way of getting error messages. Sometimes email is the way to go, sometimes you can just redirect output to a text file. Solaris and most other *NIX operating systems are shipped with a tool to log directly to syslog, logger(1).
In the most simple form you run the command like this
$ logger this is my test message
The default syslog facility and level used by logger is user.notice and unfortunately Solaris doesn’t log that anywhere by default so the first test case is pretty much useless ;-). By changing the facility and level we can get it logged in one of the standard log files. You use the -p flag to specify that.
$ logger -p daemon.notice this is the second test message
This will now show up in /var/adm/messages.
Syslog also has a number of locally defined facilities as well, local0-local7, that you can freely use for your own application. This can be very useful for logging your own application output to a separate log file. There are a few steps we need to go through before we can accomplish this but they are all very easy.
1. First we need to create the log file. We do that with touch.
# touch /var/adm/myapplication.log
2. Then you need to append the following line to the end of /etc/syslog.conf. Please make sure you are not using spaces to separate the two entries, syslogd requires you to use tabs. You can read more about that in syslog.conf(4) (this is something that usually causes a two hour debug session at the first encounter for a new sysadmin
).
local0.notice /var/adm/myapplication.log
3. We now need to tell syslogd to reread the configuration file. Run the following command as root.
# svcadm restart svc:/system/system-log:default
We are now ready to log. The final step is to actually log something.
$ logger -p local0.notice my first local0 message
You should see the following message in your /var/adm/myapplication.log
Jul 14 02:38:32 solaris-devx nickus: [ID 702911 local0.notice] my first local0 message
Now you can use this command from any of your scripts to get logs to a syslog. The great thing about syslog is that when you want to extend this it is very easy. You can use syslog to forward the logs to another server, feed them to Splunk or do other fancy things with them. In general it is better to use the OS provided tools rather than inventing your own (IMHO).
To read more in my Better Scripts series please have a look here.
Do you need system administration assistance? If you like what you are reading please consider subscribing to the RSS feed. If you have feedback or if you find the article useful please leave a comment below.


Nice write-up—I’ve been using logger for a while and am surprised how many people “come up” with other methods to log their “applications.”
Bink, most people prefer to live in a NIH world :-). Sometimes it is nice to invent something over and over again as a learning experience but I agree, I don’t see why you would do it for simple tasks like logging.
turbulene training fitness techniques…
turbulene training fitness techniques free workout guide…
The article is good and precise.
A lot of people do not understand the importance of /var/adm/message as all system messages are logged there. Also it is also possible to log the message to a loghost.