syslog support

This commit is contained in:
Jonah H. Harris
2010-12-09 11:10:21 -05:00
parent d51ebef509
commit e1a586ee69
4 changed files with 74 additions and 8 deletions

View File

@ -188,11 +188,13 @@ struct redisCommand readonlyCommandTable[] = {
/*============================ Utility functions ============================ */
void redisLog(int level, const char *fmt, ...) {
const int syslogLevelMap[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING };
const char *c = ".-*#";
time_t now = time(NULL);
va_list ap;
FILE *fp;
char *c = ".-*#";
char buf[64];
time_t now;
char msg[REDIS_MAX_LOGMSG_LEN];
if (level < server.verbosity) return;
@ -200,15 +202,16 @@ void redisLog(int level, const char *fmt, ...) {
if (!fp) return;
va_start(ap, fmt);
now = time(NULL);
strftime(buf,64,"%d %b %H:%M:%S",localtime(&now));
fprintf(fp,"[%d] %s %c ",(int)getpid(),buf,c[level]);
vfprintf(fp, fmt, ap);
fprintf(fp,"\n");
fflush(fp);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now));
fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
fflush(fp);
if (server.logfile) fclose(fp);
if (server.syslog_enabled) syslog(syslogLevelMap[level], "%s", msg);
}
/* Redis generally does not try to recover from out of memory conditions
@ -743,6 +746,9 @@ void initServerConfig() {
server.saveparams = NULL;
server.loading = 0;
server.logfile = NULL; /* NULL = log on standard output */
server.syslog_enabled = 0;
server.syslog_ident = zstrdup("redis");
server.syslog_facility = LOG_LOCAL0;
server.glueoutputbuf = 1;
server.daemonize = 0;
server.appendonly = 0;
@ -813,6 +819,11 @@ void initServer() {
signal(SIGPIPE, SIG_IGN);
setupSigSegvAction();
if (server.syslog_enabled) {
openlog(server.syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT,
server.syslog_facility);
}
server.mainthread = pthread_self();
server.clients = listCreate();
server.slaves = listCreate();