Skip to content

Commit 7d4e9f5

Browse files
committed
Add spec compliant default logger
1 parent 86e4a4c commit 7d4e9f5

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/libmongoc/src/mongoc/mongoc-structured-log-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef MONGOC_STRUCTRURED_LOG_PRIVATE_H
2323
#define MONGOC_STRUCTRURED_LOG_PRIVATE_H
2424

25+
#define MONGOC_STRUCTURED_LOG_DEFAULT_LEVEL MONGOC_STRUCTURED_LOG_LEVEL_WARNING;
26+
2527
typedef void (*mongoc_structured_log_build_message_t) (
2628
mongoc_structured_log_entry_t *entry);
2729

src/libmongoc/src/mongoc/mongoc-structured-log.c

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static bson_mutex_t gStructuredLogMutex;
4040
static mongoc_structured_log_func_t gStructuredLogger =
4141
mongoc_structured_log_default_handler;
4242
static void *gStructuredLoggerData;
43+
static FILE *log_stream;
4344

4445
static BSON_ONCE_FUN (_mongoc_ensure_mutex_once)
4546
{
@@ -124,14 +125,92 @@ mongoc_structured_log (mongoc_structured_log_level_t level,
124125
mongoc_structured_log_entry_destroy (&entry);
125126
}
126127

128+
static mongoc_structured_log_level_t
129+
_mongoc_structured_log_get_log_level_from_env (const char *variable)
130+
{
131+
const char* level = getenv (variable);
132+
133+
if (!level) {
134+
return MONGOC_STRUCTURED_LOG_DEFAULT_LEVEL;
135+
} else if (!strcasecmp (level, "trace")) {
136+
return MONGOC_STRUCTURED_LOG_LEVEL_TRACE;
137+
} else if (!strcasecmp (level, "debug")) {
138+
return MONGOC_STRUCTURED_LOG_LEVEL_DEBUG;
139+
} else if (!strcasecmp (level, "info")) {
140+
return MONGOC_STRUCTURED_LOG_LEVEL_INFO;
141+
} else if (!strcasecmp (level, "notice")) {
142+
return MONGOC_STRUCTURED_LOG_LEVEL_NOTICE;
143+
} else if (!strcasecmp (level, "warn")) {
144+
return MONGOC_STRUCTURED_LOG_LEVEL_WARNING;
145+
} else if (!strcasecmp (level, "error")) {
146+
return MONGOC_STRUCTURED_LOG_LEVEL_ERROR;
147+
} else if (!strcasecmp (level, "critical")) {
148+
return MONGOC_STRUCTURED_LOG_LEVEL_CRITICAL;
149+
} else if (!strcasecmp (level, "alert")) {
150+
return MONGOC_STRUCTURED_LOG_LEVEL_ALERT;
151+
} else if (!strcasecmp (level, "emergency")) {
152+
return MONGOC_STRUCTURED_LOG_LEVEL_EMERGENCY;
153+
} else {
154+
MONGOC_ERROR ("Invalid log level %s read for variable %s", level, variable);
155+
exit (EXIT_FAILURE);
156+
}
157+
}
158+
159+
static mongoc_structured_log_level_t
160+
_mongoc_structured_log_get_log_level (mongoc_structured_log_component_t component)
161+
{
162+
switch (component) {
163+
case MONGOC_STRUCTURED_LOG_COMPONENT_COMMAND:
164+
return _mongoc_structured_log_get_log_level_from_env ("MONGODB_LOGGING_COMMAND");
165+
case MONGOC_STRUCTURED_LOG_COMPONENT_CONNECTION:
166+
return _mongoc_structured_log_get_log_level_from_env ("MONGODB_LOGGING_CONNECTION");
167+
case MONGOC_STRUCTURED_LOG_COMPONENT_SDAM:
168+
return _mongoc_structured_log_get_log_level_from_env ("MONGODB_LOGGING_SDAM");
169+
case MONGOC_STRUCTURED_LOG_COMPONENT_SERVER_SELECTION:
170+
return _mongoc_structured_log_get_log_level_from_env ("MONGODB_LOGGING_SERVER_SELECTION");
171+
default:
172+
MONGOC_ERROR ("Requesting log level for unsupported component %d", component);
173+
exit (EXIT_FAILURE);
174+
}
175+
}
176+
177+
static void
178+
_mongoc_structured_log_initialize_stream ()
179+
{
180+
const char* log_target = getenv("MONGODB_LOGGING_PATH");
181+
bool log_to_stderr = !log_target || !strcmp (log_target, "stderr");
182+
183+
log_stream = log_to_stderr ? stderr : fopen (log_target, "a");
184+
if (!log_stream) {
185+
MONGOC_ERROR ("Cannot open log file %s for writing", log_target);
186+
exit (EXIT_FAILURE);
187+
}
188+
}
189+
190+
static FILE*
191+
_mongoc_structured_log_get_stream ()
192+
{
193+
if (!log_stream) {
194+
_mongoc_structured_log_initialize_stream ();
195+
}
196+
197+
return log_stream;
198+
}
199+
127200
static void
128201
mongoc_structured_log_default_handler (mongoc_structured_log_entry_t *entry,
129202
void *user_data)
130203
{
204+
mongoc_structured_log_level_t log_level = _mongoc_structured_log_get_log_level (mongoc_structured_log_entry_get_component (entry));
205+
206+
if (log_level < mongoc_structured_log_entry_get_level (entry)) {
207+
return;
208+
}
209+
131210
char *message =
132211
bson_as_json (mongoc_structured_log_entry_get_message (entry), NULL);
133212

134-
fprintf (stderr,
213+
fprintf (_mongoc_structured_log_get_stream (),
135214
"Structured log: %d, %d, %s\n",
136215
mongoc_structured_log_entry_get_level (entry),
137216
mongoc_structured_log_entry_get_component (entry),

0 commit comments

Comments
 (0)