LINUX TIPS AND TRICKS --- November 03, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

--------------------------------------------------------------------------------

Writing to syslog
By Danny Kalev

This week I will discuss the syslog() function, which writes a message 
to syslog.

Daemons and user-written utilities that run in the background write 
messages to syslog frequently. An interactive program may also write to 
syslog so you can peruse the log afterwards, analyze the program's 
performance and detect bugs and errors in its execution. Logging is 
also an important security measure that enables you to track down 
suspicious or anomalous activity after it has taken place. The syslog.h 
header file declares the syslog() function as follows:

    void syslog(int priority, const char *msg, ...);

The first argument is a combination of the severity and facility of the 
message (the latter is optional). The severity codes are as follows:

    LOG_EMERG - A panic message. Normally broadcast to all users. 
    LOG_ALERT - A malfunction that should be fixed immediately
    LOG_CRIT - Critical condition, e.g., a broken network cable
    LOG_ERR - An error message
    LOG_WARNING - A warning condition
    LOG_NOTICE - A non-error condition that requires special attention
    LOG_INFO - Logs an informational message
    LOG_DEBUG - A debug-level message

You may also include the facility of the message, if appropriate. For 
example, the LOG_KERN facility code indicates a kernel message. 
Likewise, LOG_MAIL indicates a message originated by the mail 
subsystem, LOG_LOCAL0 indicates a local users, and so on (see syslog.h 
for a complete list of facility codes). The severity and facility 
values are combined using the bitwise OR operator. The result is the 
message's priority.

The remaining arguments of syslog() are a printf-style string that may 
contain format flags, and any other arguments required by the format, 
except that the special flag %m is replaced by the error message 
corresponding to the current errno value. Here is an example of using 
syslog() with two arguments:

     int main()
     {
      syslog(LOG_INFO, "main has started.\n");
      return 0;
     }

The above syslog() call adds the following entry to syslog:

     November 22 10:12:34 bustopher syslog: main has started.

In the following example, syslog takes four arguments, as required by 
its format string:

     int f(int stat, const char *op)
     {
      if (stat!=SUCCESS)
       syslog(LOG_WARN, "%s failed, errno: %d (%m)\n", op, errno);
      return 0;
     }


About the author(s)
----------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com. 

--------------------------------------------------------------------------------

RESOURCES

Illuminating shadow passwords 
What the software is, how to get it, how to use it

http://www.itworld.com/jlw/lintps_nl/lw-2000-07/lw-07-shadowpasswords_p.html

System logs and logging servers

http://www.itworld.com/jlw/lintps_nl/lw-1999-07/lw-07-ramparts-7.html

Why aren't you logging? 
New logging feature from Sun is not only easy to install and safe to 
use, it's free

http://www.itworld.com/jsw/lintps_nl/swol-09-2000/swol-0922-supersys.html

Simple shared logging 
Using the cron daemon to create a shared log

http://www.itworld.com/jsw/lintps_nl/swol-12-1997/swol-12-unix101.html

--------------------------------------------------------------------------------

COMMUNITY DISCUSSIONS

Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Moderated by Danny Kalev.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/175!skip=111

Ask questions, offer solutions, and tell your tales in this lively 
discussion of the good, bad, and ugly sides of managing Unix systems. 
Moderated Sandra Henry-Stocker.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b677/266!skip=215

--------------------------------------------------------------------------------

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
  Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account Executive at:
  dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz, Eastern
  Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager at: paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com

--------------------------------------------------------------------------------

PRIVACY POLICY
http://www2.itworld.com/CDA/ITW_Privacy_Policy

Copyright 2000 ITworld.com, Inc., All Rights Reserved.

http://www.itworld.com
