views:

543

answers:

3

I'm building a C++ Linux application and I need to log its activity. What are the existing Linux facilities to create logs? What would you recommend?

+9  A: 

The historic one is syslog(3). From C:

#include <syslog.h>
openlog("myprogram", LOG_PID, LOG_LOCAL0);
syslog(LOG_INFO, "data %d %s", 3, "example");

From Perl:

use Sys::Syslog;
openlog "myprogram", "pid", "local0";
syslog 'info', 'data %d %s', 3, 'example';

From shell:

logger -p local0.info -t myprogram -- data 3 example

The syslogd daemon can be configured to put log files in different places (files, tty, other machines) depending on the facility (here LOG_LOCAL0) and the priority (here LOG_INFO)

kmkaplan
Can syslog write to a separate file or it all goes to the syste log?
Jack
It goes to system log. It can also log to the console with LOG_CONS.
kmkaplan
+4  A: 

How about log4cxx?

Alex Reitbort
Seems like Swiss-army knife logger...
Jack
Yep, it's nice and easy to set up and use, although I used port to .Net of this logger (log4net).
Alex Reitbort
+1  A: 

In addition to what Alex has said, why would you need a Linux specific logger?

Amit
It's not like I need Linux-specific logger. I just wanted to know what alternatives to manually logging to a file do I have on Linux platform.
Jack
Thanks for clarifying. 'log4cxx' is the answer
Amit