You can take control of how logs are handled by defining a custom output function. In this mode, zlog is responsible for generating the log path and message dynamically based on your configuration, but you are responsible for the actual output, rotation, and cleanup actions.
To implement user-defined output:
- Define a placeholder name in the
[rules] section of your configuration file. - Register a C function to that name using
zlog_set_record(). - The function must accept a
zlog_msg_t * pointer.
Note: Implementing complex rotation logic (like size-based or time-based rotation) in a custom output function can be difficult in multi-process or multi-threaded environments.
# 1. Define in configuration file
[formats]
simple = "%m%n"
[rules]
my_cat.* $myoutput, " mypath %c %d";simple
# 2. Set the function in C
#include "zlog.h"
int output(zlog_msg_t *msg)
{
printf("[mystd]:[%s][%s][%ld]\n", msg->path, msg->buf, (long)msg->len);
return 0;
}
int main(int argc, char** argv)
{
int rc;
zlog_category_t *zc;
rc = zlog_init("test_record.conf");
if (rc) {
return -1;
}
// Register the custom output function
zlog_set_record("myoutput", output);
zc = zlog_get_category("my_cat");
if (!zc) {
zlog_fini();
return -2;
}
zlog_info(zc, "hello, zlog");
zlog_fini();
return 0;
}