Is it possibile to show the level (LOG_INFO, LOG_ERROR, ...) information passed to the "syslog" function into each log line?
Thank you! Antonio
You can write a wrapper function like this
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
int
mysyslog(int __level, const char *const syslogformat, ...)
{
va_list args;
char *format;
size_t length;
char *level;
ssize_t result;
va_start(args,syslogformat);
level = "?";
switch (__level)
{
case LOG_ALERT:
level = "LOG_ALERT";
break;
case LOG_INFO:
level = "LOG_INFO";
break;
case LOG_EMERG:
level = "LOG_EMERG";
break;
case LOG_CRIT:
level = "LOG_CRIT";
break;
case LOG_ERR:
level = "LOG_ERR";
break;
case LOG_WARNING:
level = "LOG_WARNING";
break;
case LOG_NOTICE:
level = "LOG_NOTICE";
break;
case LOG_DEBUG:
level = "LOG_DEBUG";
break;
}
length = strlen(syslogformat) + strlen(level) + 2;
format = malloc(length + 1);
result = snprintf(format, length + 1, "%s: %s", level, syslogformat);
if ((result < 0) || (result >= length + 1))
return -1;
vsyslog(__level, format, args);
va_end(args);
return 0;
}
int main(void)
{
mysyslog(LOG_NOTICE, "This is a log message `%d' with an integer as parameter\n", 100);
return 0;
}
Just use a template for your destination file in which you specifie how your log message will be displayed/written; something like this shows only syslog level and message:
template t_example { template("${LEVEL}" "${MSG}"); };
destination d_example { file("/var/log/example.log" template(t_example) ); };