Libreria muy sencilla para logging

de http://stackoverflow.com/questions/6508461/logging-library-for-c

Archivo log.h


#ifndef LOG_H
#define LOG_H

void log(const char* tag, const char* message);

#endif /* LOG_H */

Archivo log.c


#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void log(const char* tag, const char* message) {
   time_t now;
   time(&now);
   printf("%s [%s]: %s\n", ctime(&now), tag, message);
}

Syslog


man syslog
	#include <syslog.h>
	void openlog(const char *ident, int option, int facility);
	void syslog(int priority, const char *format, ...);
	void closelog(void);

Ejemplo sencillo de uso: hello_syslog.c


#include <stdio.h>
#include <unistd.h>
#include <syslog.h>

int main(void) {

 openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
 syslog(LOG_INFO, "A different kind of Hello world ... ");
 closelog();

 return 0;
}

Syslog, pero en otro archivo:

To log into a different file you'll need to specify the file in the syslog configuration.

De http://stackoverflow.com/questions/10952515/c-c-syslog-to-custom-file-not-var-log-syslog-but-var-log-mylog-ubuntu-12

In my folder /etc/rsyslog.d there are two files:


20-ufw.conf and 50-default.conf

I added a file:


sudo nano /etc/rsyslog.d/30-mycustomname.conf

Archivo:


# Log QSD Centro generated log messages to file
if $programname == 'centro' then /var/log/centro.log
# Uncomment the following to stop logging anything that matches the last rule.
& ~

Then I check that the file /var/log/centro.log does not exist


sudo rm -f /var/log/centro.log

Then I restart the service


sudo service rsyslog restart