diff --git a/src/module.c b/src/module.c index a71d442c..dff7feb3 100644 --- a/src/module.c +++ b/src/module.c @@ -2772,17 +2772,35 @@ void RM_EmitAOF(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) { * Logging * -------------------------------------------------------------------------- */ -/* Produces a log message to the standard Redis log. */ -void RM_Log(RedisModuleCtx *ctx, int level, const char *fmt, ...) -{ +/* Produces a log message to the standard Redis log, the format accepts + * printf-alike specifiers, while level is a string describing the log + * level to use when emitting the log, and must be one of the following: + * + * * "debug" + * * "verbose" + * * "notice" + * * "warning" + * + * If the specified log level is invalid, verbose is used by default. + * There is a fixed limit to the length of the log line this function is able + * to emit, this limti is not specified but is guaranteed to be more than + * a few lines of text. + */ +void RM_Log(RedisModuleCtx *ctx, const char *levelstr, const char *fmt, ...) { va_list ap; char msg[LOG_MAX_LEN]; size_t name_len; + int level; - if ((level&0xff) < server.verbosity) return; if (!ctx->module) return; /* Can only log if module is initialized */ - name_len = snprintf(msg, sizeof(msg),"%s: ", ctx->module->name); + if (!strcasecmp(levelstr,"debug")) level = LL_DEBUG; + else if (!strcasecmp(levelstr,"verbose")) level = LL_VERBOSE; + else if (!strcasecmp(levelstr,"notice")) level = LL_NOTICE; + else if (!strcasecmp(levelstr,"warning")) level = LL_WARNING; + else level = LL_VERBOSE; /* Default. */ + + name_len = snprintf(msg, sizeof(msg),"<%s> ", ctx->module->name); va_start(ap, fmt); vsnprintf(msg + name_len, sizeof(msg) - name_len, fmt, ap); @@ -2791,7 +2809,6 @@ void RM_Log(RedisModuleCtx *ctx, int level, const char *fmt, ...) serverLogRaw(level,msg); } - /* -------------------------------------------------------------------------- * Modules API internals * -------------------------------------------------------------------------- */ diff --git a/src/modules/API.md b/src/modules/API.md index 24768c5b..021b2aa1 100644 --- a/src/modules/API.md +++ b/src/modules/API.md @@ -1117,9 +1117,19 @@ handling is performed by Redis itself. ## `RM_Log` - void RM_Log(RedisModuleCtx *ctx, int level, const char *fmt, ...); + void RM_Log(RedisModuleCtx *ctx, const char *levelstr, const char *fmt, ...); + +Produces a log message to the standard Redis log, the format accepts +printf-alike specifiers, while level is a string describing the log +level to use when emitting the log, and must be one of the following: + +* "debug" +* "verbose" +* "notice" +* "warning" + +If the specified log level is invalid, verbose is used by default. +There is a fixed limit to the length of the log line this function is able +to emit, this limti is not specified but is guaranteed to be more than +a few lines of text. -Produce a log message into the standard Redis log. All standard Redis logging -configuration applies here. Messages can only be logged after a module has -initialized, and are prefixed by the name of the module. Log level is -specified using the REDISMODULE_LOG_* macros. diff --git a/src/redismodule.h b/src/redismodule.h index aa43a736..f376c36c 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -68,13 +68,6 @@ #define REDISMODULE_POSITIVE_INFINITE (1.0/0.0) #define REDISMODULE_NEGATIVE_INFINITE (-1.0/0.0) -/* Logging levels */ -#define REDISMODULE_LOG_DEBUG 0 -#define REDISMODULE_LOG_VERBOSE 1 -#define REDISMODULE_LOG_NOTICE 2 -#define REDISMODULE_LOG_WARNING 3 - - /* ------------------------- End of common defines ------------------------ */ #ifndef REDISMODULE_CORE @@ -187,7 +180,7 @@ RedisModuleString *REDISMODULE_API_FUNC(RedisModule_LoadString)(RedisModuleIO *i char *REDISMODULE_API_FUNC(RedisModule_LoadStringBuffer)(RedisModuleIO *io, size_t *lenptr); void REDISMODULE_API_FUNC(RedisModule_SaveDouble)(RedisModuleIO *io, double value); double REDISMODULE_API_FUNC(RedisModule_LoadDouble)(RedisModuleIO *io); -void REDISMODULE_API_FUNC(RedisModule_Log)(RedisModuleCtx *ctx, int level, const char *fmt, ...); +void REDISMODULE_API_FUNC(RedisModule_Log)(RedisModuleCtx *ctx, const char *level, const char *fmt, ...); /* This is included inline inside each Redis module. */ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) __attribute__((unused));