FreeBSD Manual Pages
MONGOC_UNSTRUCTURED_LOG(3) libmongoc MONGOC_UNSTRUCTURED_LOG(3)
This is the original logging facility that supports freeform string messages
originating from the driver itself or from application code. This has been
retroactively termed "unstructured logging". See Structured Logging for the
newer standardized logging facility.
typedef enum {
MONGOC_LOG_LEVEL_ERROR,
MONGOC_LOG_LEVEL_CRITICAL,
MONGOC_LOG_LEVEL_WARNING,
MONGOC_LOG_LEVEL_MESSAGE,
MONGOC_LOG_LEVEL_INFO,
MONGOC_LOG_LEVEL_DEBUG,
MONGOC_LOG_LEVEL_TRACE,
} mongoc_log_level_t;
#define MONGOC_ERROR(...)
#define MONGOC_CRITICAL(...)
#define MONGOC_WARNING(...)
#define MONGOC_MESSAGE(...)
#define MONGOC_INFO(...)
#define MONGOC_DEBUG(...)
typedef void (*mongoc_log_func_t) (mongoc_log_level_t log_level,
const char *log_domain,
const char *message,
void *user_data);
void
mongoc_log_set_handler (mongoc_log_func_t log_func, void *user_data);
void
mongoc_log (mongoc_log_level_t log_level,
const char *log_domain,
const char *format,
...);
const char *
mongoc_log_level_str (mongoc_log_level_t log_level);
void
mongoc_log_default_handler (mongoc_log_level_t log_level,
const char *log_domain,
const char *message,
void *user_data);
void
mongoc_log_trace_enable (void);
void
mongoc_log_trace_disable (void);
This abstraction can be used for logging in your application, or you
can integrate the driver with an existing logging system.
MACROS
To make logging a little less painful, various helper macros are pro-
vided. See the following example.
#undef MONGOC_LOG_DOMAIN
#define MONGOC_LOG_DOMAIN "my-custom-domain"
MONGOC_WARNING ("An error occurred: %s", strerror (errno));
CUSTOM LOG HANDLERS
The default log handler prints a timestamp and the log message to std-
out, or to stderr for warnings, critical messages, and errors. You can
override the handler with mongoc_log_set_handler(). Your handler func-
tion is called in a mutex for thread safety.
For example, you could register a custom handler to suppress messages
at INFO level and below:
void
my_logger (mongoc_log_level_t log_level,
const char *log_domain,
const char *message,
void *user_data)
{
/* smaller values are more important */
if (log_level < MONGOC_LOG_LEVEL_INFO) {
mongoc_log_default_handler (log_level, log_domain, message, user_data);
}
}
int
main (int argc, char *argv[])
{
mongoc_log_set_handler (my_logger, NULL);
mongoc_init ();
/* ... your code ... */
mongoc_cleanup ();
return 0;
}
Note that in the example above mongoc_log_set_handler() is called be-
fore mongoc_init(). Otherwise, some log traces could not be processed
by the log handler.
To restore the default handler:
mongoc_log_set_handler (mongoc_log_default_handler, NULL);
DISABLE LOGGING
To disable all logging, including warnings, critical messages and er-
rors, provide an empty log handler:
mongoc_log_set_handler (NULL, NULL);
TRACING
If compiling your own copy of the MongoDB C driver, consider configur-
ing with -DENABLE_TRACING=ON to enable function tracing and hex dumps
of network packets to STDERR and STDOUT during development and debug-
ging.
This is especially useful when debugging what may be going on inter-
nally in the driver.
Trace messages can be enabled and disabled by calling mon-
goc_log_trace_enable() and mongoc_log_trace_disable()
NOTE:
Compiling the driver with -DENABLE_TRACING=ON will affect its per-
formance. Disabling tracing with mongoc_log_trace_disable() signifi-
cantly reduces the overhead, but cannot remove it completely.
AUTHOR
MongoDB, Inc
COPYRIGHT
2009-present, MongoDB, Inc.
1.30.4 Nov 01, 2025 MONGOC_UNSTRUCTURED_LOG(3)
MACROS | CUSTOM LOG HANDLERS | DISABLE LOGGING | TRACING | AUTHOR | COPYRIGHT
Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=mongoc_unstructured_log&sektion=3&manpath=FreeBSD+Ports+15.0>
