Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there any way to make a log file for maintaining some data in /var/log/ with the help of some library function or system call in c language in linux. And I also want to know the standards that we should follow to write and process log. Thanks

share|improve this question
    
Please note that programming questions are generally off-topic here; you should ask them on Stack Overflow. An explanation of the API is ok here, but explanations of how to use it in C or other programming language normally belongs on Stack Overflow. –  Gilles Apr 25 '11 at 22:02
add comment

4 Answers

up vote 13 down vote accepted

The standard way to log from a C program is syslog.

Start by including the header file:

#include <syslog.h>

Then early in your program, you should configure syslog by calling openlog:

openlog("programname", 0, LOG_USER);

The first argument is the identification or the tag, which is automatically added at the start of each message. Put your program's name here.

The second argument is the options you want to use, or 0 for the normal behavior. The full list of options is in man 3 syslog. One you might find useful is LOG_PID, which makes syslog also record the process id in the log message.

Then, each time you want to write a log message, you call syslog:

syslog(LOG_INFO, "%s", "Message");

The first argument is the priority. The priority ranges from DEBUG (least important) to EMERG (only for emergencies) with DEBUG, INFO, and ERR being the most commonly used. See man 3 syslog for your options.

The second and third arguments are a format and a message, just like printf.

Which log file this appears in depends on your syslog settings.

With a default setup, it probably goes into /var/log/messages.


You can set up a custom log file by using one of the facilities in the range LOG_LOCAL0 to LOG_LOCAL7.

You use them by changing:

openlog("programname", 0, LOG_USER);

to

openlog("programname", 0, LOG_LOCAL0);

or

openlog("programname", 0, LOG_LOCAL1);

etc.

and adding a corresponding entry to /etc/syslog.conf, e.g.

local1.info /var/log/programname.log

and restarting the syslog server, e.g.

pkill -HUP syslogd

The .info part of local1.info above means that all messages that are INFO or more important will be logged, including INFO, NOTICE, ERR (error), CRIT (critical), etc., but not DEBUG.


Or, if you have rsyslog, you could try a property-based filter, e.g.

:syslogtag, isequal, "programname"    /var/log/programname.log

Or, if you are planning on distributing your software to other people, it's probably not a good idea to rely on using LOG_LOCAL or an rsyslog filter.

In that case, you should use LOG_USER (if it's a normal program) or LOG_DAEMON (if it's a server), write your startup messages and error messages using syslog, but write all of your log messages to a file outside of syslog. For example, Apache HTTPd logs to /var/log/apache2/* or /var/log/httpd/*, I assume using regular open/fopen and write/printf calls.

share|improve this answer
    
Thanks.This is quite useful info. But would you please explain more on log_local0-7 option. For eg. I want to write my data( that have <self> word in log line) in self.log file. and some logs in other files. –  Sushant Jain Apr 25 '11 at 6:53
    
@Sushant Jain: What Linux distribution and version are you targeting? Do you have old syslog (ksyslogd) or rsyslog? rpm -qa | grep syslog or dpkg -l '*syslog*' will probably tell you which. –  Mikel Apr 25 '11 at 7:22
    
log_local* are, frankly, relics from back when sysklogd was the only game in town for syslog software. Nowadays software like syslog-ng, rsyslog and dsyslog exist and have much more sophisticated filtering capabilities than just service/level. –  Shadur Apr 25 '11 at 14:30
    
Thanks a lot. My problem has been solved. I have one more query about parsing data stored in log file. Is there any good way to do this it. Actually I am trying to make a system statistics tool. So i need it. –  Sushant Jain Apr 27 '11 at 5:12
    
@Sushant Jain. Glad to help. Please post your log parsing question as a separate question so that people can see it and provide proper answers. –  Mikel Apr 27 '11 at 7:49
add comment

You will want to #include <syslog.h>, then use the syslog() functions to send data to whatever system logging program is active.

See the man page here.

share|improve this answer
    
syslog() function writes data into /var/log/syslog. whereas I want to write logs in my own file. –  Sushant Jain Apr 25 '11 at 6:24
    
As Mikel explains below you can tag your log messages in such a way that syslog understand what program you are, then configure syslog to write your programs log messages to a different file. –  Caleb Apr 25 '11 at 6:31
add comment

There are lots of possibilities, what is your plan? Do you just need an option to log from command line? Take a look at logger (included in bsdutils). Just type:

usr@srv % logger test

and it will log something like this to your /var/log/syslog:

Apr 25 07:55:15 localhost usr: test

see also man logger. Depending on your logging daemon you can sort these messages to specific files or filter them by priority.

There are also some solutions for different programming languages, so please tell me what do you want to do ;-)

share|improve this answer
    
Actually I want to do it using c language. and I have got info in mikel post. –  Sushant Jain Apr 25 '11 at 6:54
add comment

Filtering by program name is written differently from what is mentioned above, for recent versions of rsyslog (version on my machine is 5.8.6) as shown below:

if $programname == 'popa3d' then /var/log/popa3d.log

For more info, look here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.